Related
I'm trying to learn cv2 in python 2.7, but when I run my code, in the specific part of it:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
img = cv2.imread('2015-05-27-191152.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
it returns this:
File "face_detection.py", line 11, in <module>
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: /home/arthurckl/Desktop/opencv-3.0.0-rc1/modules/objdetect/src/cascadedetect.cpp:1595: error: (-215) !empty() in function detectMultiScale
I tried to search the answer here but the best i could find is that I must be loading the face_cascade the wrong way... Any help?
I had the same issue.
I didn't need to download anything else to solve this. CV2 had everything I needed.
Instead of trying to figure out where the .xml files are and hard coding the values, I used a property given by cv2.
From OP
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
Becomes
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
The XML or file is missing or the path to it is incorrect or the create_capture path is incorrect.
The paths in the opencv sample look like this:
cascade_fn = args.get('--cascade', "../../data/haarcascades/haarcascade_frontalface_alt.xml")
nested_fn = args.get('--nested-cascade', "../../data/haarcascades/haarcascade_eye.xml")
cam = create_capture(video_src, fallback='synth:bg=../data/lena.jpg:noise=0.05')
I ran the same code. There are two things to note here.
1. Give the entire path of the .xml files.
2. Give a key press event instruction at the end.
Add this block of code at the end and run your file, worked for me:
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
cv2.imwrite('messigray.png',img)
cv2.destroyAllWindows()
For example, my code looked like
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_eye.xml')
img = cv2.imread('lena.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
#faces = face_cascade.detectMultiScale(gray)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
cv2.imwrite('messigray.png',img)
cv2.destroyAllWindows()
My output looked like this:
You just need to add proper path of the haarcascade_frontalface_default.xml file i.e. you only have to add prefix (cv2.data.haarcascades)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
The XML file is missing, you can get the file from the GitHub repository and place it in the same directory as your project. Link to the folder on GitHub is here. Just download the file named haarcascade_frontalface_default.xml.
Actually, the file exists on your system. Just go to the site-packages folder of your python installation folder and check the cv2/data folder for the file
Use the entire file path and use "\\" instead of "\" in the xml file path.
The file path should be as follows:
face_cascade = cv2.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml')
instead of:
cascade_fn = args.get('--cascade', "..\..\data\haarcascades\haarcascade_frontalface_alt.xml")
no need to change the code
download that .xml file , then put the path of that file
it will solve the error (100%)
If you are using Anaconda you should add the Anaconda path.
new_path = 'C:/Users/.../Anaconda/Library/etc/haarcascades/'
face_cascade = cv2.CascadeClassifier(new_path + 'haarcascade_frontalface_default.xml')
This error means that the XML file could not be found. The library needs you to pass it the full path, even though you’re probably just using a file that came with the OpenCV library.
You can use the built-in pkg_resources module to automatically determine this for you. The following code looks up the full path to a file inside wherever the cv2 module was loaded from:
import pkg_resources
haar_xml = pkg_resources.resource_filename(
'cv2', 'data/haarcascade_frontalface_default.xml')
For me this was '/Users/andrew/.local/share/virtualenvs/foo-_b9W43ee/lib/python3.7/site-packages/cv2/data/haarcascade_frontalface_default.xml'; yours is guaranteed to be different. Just let python’s pkg_resources library figure it out.
classifier = cv2.CascadeClassifier(haar_xml)
faces = classifier.detectMultiScale(frame)
Success!
On OSX with a homebrew install the full path to the opencv folder should work:
face_cascade = cv2.CascadeClassifier('/usr/local/Cellar/opencv/3.4.0_1/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/usr/local/Cellar/opencv/3.4.0_1/share/OpenCV/haarcascades/haarcascade_eye.xml')
Take care of the version number in the path.
Probably the face_cascade is empty. You can check if the variable is empty or not by typing following command:
face_cascade.empty()
If it is empty you will get True and this means your file is not available in the path you mentioned.
Try to add complete path of xml file as follows:
r'D:\folder Name\haarcascade_frontalface_default.xml'
"\Anaconda3\Lib\site-packages\cv2\data\" I found the xml file in this path for Anaconda
You can solve this problem by placing XML in the same directory in which your main python file (from where you tried to include this file) was placed. Now the next step is to use full path. For example
This will not work
front_cascade = cv2.CascadeClassifier('./haarcascade_eye.xml')
Use full path, now it will work fine
front_cascade = cv2.CascadeClassifier('/Users/xyz/Documents/project/haarcascade_eye.xml')
I found this in some other answer but eventually worked for me when I added the two answers.
import cv2
from matplotlib import pyplot as plt
import numpy as np
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml")
img = cv2.imread('image1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
You may find such kind of errors when you did not define the complete path of your XML file.
Try this one if you are using opencv3.1.0 in raspberrypi 3:
faceCascade = cv2.CascadeClassifier('/home/pi/opencv-3.1.0/data/haarcascades/haarcascade_frontalface_default.xml')
Your XML file was not found. Try using absolute paths like:
/path/to/my/file (Mac, Linux)
C:\\path\\to\\my\\file (Windows)
the error may be due to, the required xml files has not been loaded properly. Search for the file haarcascade_frontalface_default.xml by using the search engine of ur OS get the full path and put it as the argument to cv2.CascadeClassifier as string
Please do not copy paste the content of xml file, because once you paste it to notepad it will be saved a s text file. So directly download the file from the given source.
I ran into the same problem. but wrote the correct location.
face_cascade = cv2.CascadeClassifier('./model/haarcascade_frontalface_default.xml')
I figured out that i need to declare the full path to remove the error.
face_cascade = cv2.CascadeClassifier('C:/pythonScript/Facial-Emotion-Detection/model/haarcascade_frontalface_default.xml')
The error occurs due to missing of xml files or incorrect path of xml file.
Please try the following code,
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
I had the same problem with opencv-python and I used a virtual environment.
If it's your case, you should find the xml files at:
/home/username/virtual_environment/lib/python3.5/site-packages/cv2/data/haarcascade_frontalface_default.xml
/home/username/virtual_environment/lib/python3.5/site-packages/cv2/data/haarcascade_eye.xml
Please be sure that you're using the absolute path. Otherwise, it won't work.
The main idea of the solution as above mentioned: find the right path of the .xml file and use it to access the file correctly.
In my case, I installed the opencv in anoconda env, first direct to path of Anoconda, then
find the path of .xml file by using:
$ find . -name 'haarcascade_eye.xml' (for example search the haarcascade_eye.xml file in current dir (.))
Then use the return path:
eye_cascade = cv2.CascadeClassifier(path + 'haarcascade_eye.xml')
I faced a similar issue. It seems correcting the path to XML makes this error to go away.
It seems to be file path issue. I changed code like this and it worked.
haar_face_filename = "D:\Sandbox\Github\Faces\haar_face.xml"
haar_cascade = cv.CascadeClassifier(haar_face_filename)
I had the same issue and was trying to use open cv in a springboot application where my xml files and images are in resources folder.
Trying to give path starting from src or a absolute path like C:\a\b.xml did not work.
Creating the file path dynamically with project root path worked.
String classifierPath = System.getProperty("user.dir") + "/src/main/resources/haarcascades/haarcascade_frontalface.xml";
// similarly for image paths
Path needs to start with /, eg. /file.xml.
I'm trying to learn cv2 in python 2.7, but when I run my code, in the specific part of it:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
img = cv2.imread('2015-05-27-191152.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
it returns this:
File "face_detection.py", line 11, in <module>
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: /home/arthurckl/Desktop/opencv-3.0.0-rc1/modules/objdetect/src/cascadedetect.cpp:1595: error: (-215) !empty() in function detectMultiScale
I tried to search the answer here but the best i could find is that I must be loading the face_cascade the wrong way... Any help?
I had the same issue.
I didn't need to download anything else to solve this. CV2 had everything I needed.
Instead of trying to figure out where the .xml files are and hard coding the values, I used a property given by cv2.
From OP
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
Becomes
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
The XML or file is missing or the path to it is incorrect or the create_capture path is incorrect.
The paths in the opencv sample look like this:
cascade_fn = args.get('--cascade', "../../data/haarcascades/haarcascade_frontalface_alt.xml")
nested_fn = args.get('--nested-cascade', "../../data/haarcascades/haarcascade_eye.xml")
cam = create_capture(video_src, fallback='synth:bg=../data/lena.jpg:noise=0.05')
I ran the same code. There are two things to note here.
1. Give the entire path of the .xml files.
2. Give a key press event instruction at the end.
Add this block of code at the end and run your file, worked for me:
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
cv2.imwrite('messigray.png',img)
cv2.destroyAllWindows()
For example, my code looked like
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_eye.xml')
img = cv2.imread('lena.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
#faces = face_cascade.detectMultiScale(gray)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
cv2.imwrite('messigray.png',img)
cv2.destroyAllWindows()
My output looked like this:
You just need to add proper path of the haarcascade_frontalface_default.xml file i.e. you only have to add prefix (cv2.data.haarcascades)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
The XML file is missing, you can get the file from the GitHub repository and place it in the same directory as your project. Link to the folder on GitHub is here. Just download the file named haarcascade_frontalface_default.xml.
Actually, the file exists on your system. Just go to the site-packages folder of your python installation folder and check the cv2/data folder for the file
Use the entire file path and use "\\" instead of "\" in the xml file path.
The file path should be as follows:
face_cascade = cv2.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml')
instead of:
cascade_fn = args.get('--cascade', "..\..\data\haarcascades\haarcascade_frontalface_alt.xml")
no need to change the code
download that .xml file , then put the path of that file
it will solve the error (100%)
If you are using Anaconda you should add the Anaconda path.
new_path = 'C:/Users/.../Anaconda/Library/etc/haarcascades/'
face_cascade = cv2.CascadeClassifier(new_path + 'haarcascade_frontalface_default.xml')
This error means that the XML file could not be found. The library needs you to pass it the full path, even though you’re probably just using a file that came with the OpenCV library.
You can use the built-in pkg_resources module to automatically determine this for you. The following code looks up the full path to a file inside wherever the cv2 module was loaded from:
import pkg_resources
haar_xml = pkg_resources.resource_filename(
'cv2', 'data/haarcascade_frontalface_default.xml')
For me this was '/Users/andrew/.local/share/virtualenvs/foo-_b9W43ee/lib/python3.7/site-packages/cv2/data/haarcascade_frontalface_default.xml'; yours is guaranteed to be different. Just let python’s pkg_resources library figure it out.
classifier = cv2.CascadeClassifier(haar_xml)
faces = classifier.detectMultiScale(frame)
Success!
On OSX with a homebrew install the full path to the opencv folder should work:
face_cascade = cv2.CascadeClassifier('/usr/local/Cellar/opencv/3.4.0_1/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/usr/local/Cellar/opencv/3.4.0_1/share/OpenCV/haarcascades/haarcascade_eye.xml')
Take care of the version number in the path.
Probably the face_cascade is empty. You can check if the variable is empty or not by typing following command:
face_cascade.empty()
If it is empty you will get True and this means your file is not available in the path you mentioned.
Try to add complete path of xml file as follows:
r'D:\folder Name\haarcascade_frontalface_default.xml'
"\Anaconda3\Lib\site-packages\cv2\data\" I found the xml file in this path for Anaconda
You can solve this problem by placing XML in the same directory in which your main python file (from where you tried to include this file) was placed. Now the next step is to use full path. For example
This will not work
front_cascade = cv2.CascadeClassifier('./haarcascade_eye.xml')
Use full path, now it will work fine
front_cascade = cv2.CascadeClassifier('/Users/xyz/Documents/project/haarcascade_eye.xml')
I found this in some other answer but eventually worked for me when I added the two answers.
import cv2
from matplotlib import pyplot as plt
import numpy as np
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml")
img = cv2.imread('image1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
You may find such kind of errors when you did not define the complete path of your XML file.
Try this one if you are using opencv3.1.0 in raspberrypi 3:
faceCascade = cv2.CascadeClassifier('/home/pi/opencv-3.1.0/data/haarcascades/haarcascade_frontalface_default.xml')
Your XML file was not found. Try using absolute paths like:
/path/to/my/file (Mac, Linux)
C:\\path\\to\\my\\file (Windows)
the error may be due to, the required xml files has not been loaded properly. Search for the file haarcascade_frontalface_default.xml by using the search engine of ur OS get the full path and put it as the argument to cv2.CascadeClassifier as string
Please do not copy paste the content of xml file, because once you paste it to notepad it will be saved a s text file. So directly download the file from the given source.
I ran into the same problem. but wrote the correct location.
face_cascade = cv2.CascadeClassifier('./model/haarcascade_frontalface_default.xml')
I figured out that i need to declare the full path to remove the error.
face_cascade = cv2.CascadeClassifier('C:/pythonScript/Facial-Emotion-Detection/model/haarcascade_frontalface_default.xml')
The error occurs due to missing of xml files or incorrect path of xml file.
Please try the following code,
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
I had the same problem with opencv-python and I used a virtual environment.
If it's your case, you should find the xml files at:
/home/username/virtual_environment/lib/python3.5/site-packages/cv2/data/haarcascade_frontalface_default.xml
/home/username/virtual_environment/lib/python3.5/site-packages/cv2/data/haarcascade_eye.xml
Please be sure that you're using the absolute path. Otherwise, it won't work.
The main idea of the solution as above mentioned: find the right path of the .xml file and use it to access the file correctly.
In my case, I installed the opencv in anoconda env, first direct to path of Anoconda, then
find the path of .xml file by using:
$ find . -name 'haarcascade_eye.xml' (for example search the haarcascade_eye.xml file in current dir (.))
Then use the return path:
eye_cascade = cv2.CascadeClassifier(path + 'haarcascade_eye.xml')
I faced a similar issue. It seems correcting the path to XML makes this error to go away.
It seems to be file path issue. I changed code like this and it worked.
haar_face_filename = "D:\Sandbox\Github\Faces\haar_face.xml"
haar_cascade = cv.CascadeClassifier(haar_face_filename)
I had the same issue and was trying to use open cv in a springboot application where my xml files and images are in resources folder.
Trying to give path starting from src or a absolute path like C:\a\b.xml did not work.
Creating the file path dynamically with project root path worked.
String classifierPath = System.getProperty("user.dir") + "/src/main/resources/haarcascades/haarcascade_frontalface.xml";
// similarly for image paths
Path needs to start with /, eg. /file.xml.
I'm following this tutorial using windows 10 and pycharm
https://www.datacamp.com/community/tutorials/tensorflow-tutorial
Below is my code so far
As suggested, I installed scimage using pip install scikit-image
Because pip install scimage doesnt work and tells me to install scikit-image
This worked for installing tensorflow, so I know its installing in the correct directory, but it wont run because it wont recognize import scimage
Why wont it recognize this? Am I supposed to import something different?
I tried import scikit-image as scimage it didn't find it either.
# Import `tensorflow`
import tensorflow as tf
import numpy as np
import os
import scimage
# Initialize two constants
x1 = tf.constant([1,2,3,4])
x2 = tf.constant([5,6,7,8])
# Multiply
result = tf.multiply(x1, x2)
# Print the result
print(result)
def load_data(data_directory):
directories = [d for d in os.listdir(data_directory)
if os.path.isdir(os.path.join(data_directory, d))]
labels = []
images = []
for d in directories:
label_directory = os.path.join(data_directory, d)
file_names = [os.path.join(label_directory, f)
for f in os.listdir(label_directory)
if f.endswith(".ppm")]
for f in file_names:
images.append(skimage.data.imread(f))
labels.append(int(d))
return images, labels
ROOT_PATH = "C:\\Users\\dm\\PycharmProjects\\test"
train_data_directory = os.path.join(ROOT_PATH, "Training")
test_data_directory = os.path.join(ROOT_PATH, "Testing")
You need to use:
import skimage
Trying to run tesseract on python, this is my code:
import cv2
import os
import numpy as np
import matplotlib.pyplot as plt
import pytesseract
import Image
# def main():
jpgCounter = 0
for root, dirs, files in os.walk('/home/manel/Desktop/fotografias etiquetas'):
for file in files:
if file.endswith('.jpg'):
jpgCounter += 1
for i in range(1, 2):
name = str(i) + ".jpg"
nameBW = str(i) + "_bw.jpg"
img = cv2.imread(name,0) #zero -> abre em grayscale
# img = cv2.equalizeHist(img)
kernel = np.array([[0,-1,0], [-1,5,-1], [0,-1,0]])
img = cv2.filter2D(img, -1, kernel)
cv2.normalize(img,img,0,255,cv2.NORM_MINMAX)
med = np.median(img)
retval, threshold_manual = cv2.threshold(img, med*0.6, 255, cv2.THRESH_BINARY)
cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
print(pytesseract.image_to_string(threshold_manual, lang='eng', config='-psm 11', nice=0, output_type=Output.STRING))
the error im getting is the following:
NameError: name 'Output' is not defined
Any idea why I'm getting this?
thank you!
Add.
from pytesseract import Output
The problem is you have installed original pytesseract package (downloaded using pip) and referring documentation of madmaze GitHub version, actually both are different.
I suggest uninstalling the present version and cloning the GitHub repo and installing the same, by following this steps:
Uninstall present version:
pip uninstall pytesseract
Clone madmaze/pytesseract GitHub repo by either using git:
git clone https://github.com/madmaze/pytesseract.git
or download it directly by clicking here
Get to the root directory of the cloned repo and run:
pip install .
In python code, how can I efficiently save a certain page of a PDF as a JPEG file?
Use case: I have a Python flask web server where PDFs will be uploaded and JPEGs corresponding to each page are stored.
This solution is close, but the problem is that it does not convert the entire page to JPEG.
The pdf2image library can be used.
You can install it simply using,
pip install pdf2image
Once installed you can use following code to get images.
from pdf2image import convert_from_path
pages = convert_from_path('pdf_file', 500)
Saving pages in jpeg format
for page in pages:
page.save('out.jpg', 'JPEG')
Edit: the Github repo pdf2image also mentions that it uses pdftoppm and that it requires other installations:
pdftoppm is the piece of software that does the actual magic. It is distributed as part of a greater package called poppler.
Windows users will have to install poppler for Windows.
Mac users will have to install poppler for Mac.
Linux users will have pdftoppm pre-installed with the distro (Tested on Ubuntu and Archlinux) if it's not, run sudo apt install poppler-utils.
You can install the latest version under Windows using anaconda by doing:
conda install -c conda-forge poppler
note: Windows versions upto 0.67 are available at http://blog.alivate.com.au/poppler-windows/ but note that 0.68 was released in Aug 2018 so you'll not be getting the latest features or bug fixes.
I found this simple solution, PyMuPDF, output to png file. Note the library is imported as "fitz", a historical name for the rendering engine it uses.
import fitz
pdffile = "infile.pdf"
doc = fitz.open(pdffile)
page = doc.load_page(0) # number of page
pix = page.get_pixmap()
output = "outfile.png"
pix.save(output)
doc.close()
Note: The library changed from using "camelCase" to "snake_cased". If you run into an error that a function does not exist, have a look under deprecated names. The functions in the example above have been updated accordingly.
The fitz.Document class supports a context manager initialization:
with fitz.open(pdffile) as doc:
...
The Python library pdf2image (used in the other answer) in fact doesn't do much more than just launching pdttoppm with subprocess.Popen, so here is a short version doing it directly:
PDFTOPPMPATH = r"D:\Documents\software\____PORTABLE\poppler-0.51\bin\pdftoppm.exe"
PDFFILE = "SKM_28718052212190.pdf"
import subprocess
subprocess.Popen('"%s" -png "%s" out' % (PDFTOPPMPATH, PDFFILE))
Here is the Windows installation link for pdftoppm (contained in a package named poppler): http://blog.alivate.com.au/poppler-windows/.
Using pypdfium2 (v3):
python3 -m pip install "pypdfium2>=3,<4"
import pypdfium2 as pdfium
# Load a document
filepath = "tests/resources/multipage.pdf"
pdf = pdfium.PdfDocument(filepath)
# render a single page (in this case: the first one)
page = pdf.get_page(0)
pil_image = page.render_to(
pdfium.BitmapConv.pil_image,
)
pil_image.save("output.jpg")
# render multiple pages concurrently (in this case: all)
page_indices = [i for i in range(len(pdf))]
renderer = pdf.render_to(
pdfium.BitmapConv.pil_image,
page_indices = page_indices,
)
for image, index in zip(renderer, page_indices):
image.save("output_%02d.jpg" % index)
Advantages:
PDFium is liberal-licensed (BSD 3-Clause or Apache 2.0, at your choice)
It is fast, outperforming Poppler. In terms of speed, pypdfium2 can almost reach PyMuPDF
Returns PIL.Image.Image, numpy.ndarray, bytes, or a ctypes array, depending on your needs
Is capable of processing encrypted (password-protected) PDFs
No mandatory runtime dependencies
Supports Python >= 3.6
Setup infrastructure complies with PEP 517/518, while legacy setup still works as well
Wheels are currently available for
Windows amd64, win32, arm64
macOS x86_64, arm64
Linux (glibc) x86_64, i686, aarch64, armv7l
Linux (musl) x86_64, i686
There is a script to build from source, too.
(Disclaimer: I'm the author)
There is no need to install Poppler on your OS. This will work:
pip install Wand
from wand.image import Image
f = "somefile.pdf"
with(Image(filename=f, resolution=120)) as source:
for i, image in enumerate(source.sequence):
newfilename = f.removesuffix(".pdf") + str(i + 1) + '.jpeg'
Image(image).save(filename=newfilename)
#gaurwraith, install poppler for Windows and use pdftoppm.exe as follows:
Download zip file with Poppler's latest binaries/dlls from http://blog.alivate.com.au/poppler-windows/ and unzip to a new folder in your program files folder. For example: "C:\Program Files (x86)\Poppler".
Add "C:\Program Files (x86)\Poppler\poppler-0.68.0\bin" to your SYSTEM PATH environment variable.
From cmd line install pdf2image module -> "pip install pdf2image".
Or alternatively, directly execute pdftoppm.exe from your code using Python's subprocess module as explained by user Basj.
#vishvAs vAsuki, this code should generate the jpgs you want through the subprocess module for all pages of one or more pdfs in a given folder:
import os, subprocess
pdf_dir = r"C:\yourPDFfolder"
os.chdir(pdf_dir)
pdftoppm_path = r"C:\Program Files (x86)\Poppler\poppler-0.68.0\bin\pdftoppm.exe"
for pdf_file in os.listdir(pdf_dir):
if pdf_file.endswith(".pdf"):
subprocess.Popen('"%s" -jpeg %s out' % (pdftoppm_path, pdf_file))
Or using the pdf2image module:
import os
from pdf2image import convert_from_path
pdf_dir = r"C:\yourPDFfolder"
os.chdir(pdf_dir)
for pdf_file in os.listdir(pdf_dir):
if pdf_file.endswith(".pdf"):
pages = convert_from_path(pdf_file, 300)
pdf_file = pdf_file[:-4]
for page in pages:
page.save("%s-page%d.jpg" % (pdf_file,pages.index(page)), "JPEG")
GhostScript performs much faster than Poppler for a Linux based system.
Following is the code for pdf to image conversion.
def get_image_page(pdf_file, out_file, page_num):
page = str(page_num + 1)
command = ["gs", "-q", "-dNOPAUSE", "-dBATCH", "-sDEVICE=png16m", "-r" + str(RESOLUTION), "-dPDFFitPage",
"-sOutputFile=" + out_file, "-dFirstPage=" + page, "-dLastPage=" + page,
pdf_file]
f_null = open(os.devnull, 'w')
subprocess.call(command, stdout=f_null, stderr=subprocess.STDOUT)
GhostScript can be installed on macOS using brew install ghostscript
Installation information for other platforms can be found here. If it is not already installed on your system.
Their is a utility called pdftojpg which can be used to convert the pdf to img
You can found the code here https://github.com/pankajr141/pdf2jpg
from pdf2jpg import pdf2jpg
inputpath = r"D:\inputdir\pdf1.pdf"
outputpath = r"D:\outputdir"
# To convert single page
result = pdf2jpg.convert_pdf2jpg(inputpath, outputpath, pages="1")
print(result)
# To convert multiple pages
result = pdf2jpg.convert_pdf2jpg(inputpath, outputpath, pages="1,0,3")
print(result)
# to convert all pages
result = pdf2jpg.convert_pdf2jpg(inputpath, outputpath, pages="ALL")
print(result)
One problem everyone will face that is to Install Poppler. My way is a tricky way,but will work efficiently.
1st download Poppler here.
Then extract it and in the code section just add poppler_path=r'C:\Program Files\poppler-0.68.0\bin' (for eg.) like below
from pdf2image import convert_from_path
images = convert_from_path("mypdf.pdf", 500,poppler_path=r'C:\Program Files\poppler-0.68.0\bin')
for i, image in enumerate(images):
fname = 'image'+str(i)+'.png'
image.save(fname, "PNG")
Here is a function that does the conversion of a PDF file with one or multiple pages to a single merged JPEG image.
import os
import tempfile
from pdf2image import convert_from_path
from PIL import Image
def convert_pdf_to_image(file_path, output_path):
# save temp image files in temp dir, delete them after we are finished
with tempfile.TemporaryDirectory() as temp_dir:
# convert pdf to multiple image
images = convert_from_path(file_path, output_folder=temp_dir)
# save images to temporary directory
temp_images = []
for i in range(len(images)):
image_path = f'{temp_dir}/{i}.jpg'
images[i].save(image_path, 'JPEG')
temp_images.append(image_path)
# read images into pillow.Image
imgs = list(map(Image.open, temp_images))
# find minimum width of images
min_img_width = min(i.width for i in imgs)
# find total height of all images
total_height = 0
for i, img in enumerate(imgs):
total_height += imgs[i].height
# create new image object with width and total height
merged_image = Image.new(imgs[0].mode, (min_img_width, total_height))
# paste images together one by one
y = 0
for img in imgs:
merged_image.paste(img, (0, y))
y += img.height
# save merged image
merged_image.save(output_path)
return output_path
Example usage: -
convert_pdf_to_image("path_to_Pdf/1.pdf", "output_path/output.jpeg")
I wrote this script to easily convert a folder directory that contains PDFs (single page) to PNGs really nicely.
import os
from pathlib import PurePath
import glob
# from PIL import Image
from pdf2image import convert_from_path
import pdb
# In[file list]
wd = os.getcwd()
# filter images
fileListpdf = glob.glob(f'{wd}//*.pdf')
# In[Convert pdf to images]
for i in fileListpdf:
images = convert_from_path(i, dpi=300)
path_split = PurePath(i).parts
fileName, ext = os.path.splitext(path_split[-1])
images[0].save(f'{fileName}.png', 'PNG')
Hopefully, this helps if you need to convert PDFs to PNGs!
I use a (maybe) much simpler option of pdf2image:
cd $dir
for f in *.pdf
do
if [ -f "${f}" ]; then
n=$(echo "$f" | cut -f1 -d'.')
pdftoppm -scale-to 1440 -png $f $conv/$n
rm $f
mv $conv/*.png $dir
fi
done
This is a small part of a bash script in a loop for the use of a narrow casting device.
Checks every 5 seconds on added pdf files (all) and processes them.
This is for a demo device, at the end converting will be done at a remote server. Converting to .PNG now, but .JPG is possible too.
This converting, together with transitions on A4 format, displaying a video, two smooth scrolling texts and a logo (with transition in three versions) sets the Pi3 to allmost 4x 100% cpu-load ;-)
from pdf2image import convert_from_path
import glob
pdf_dir = glob.glob(r'G:\personal\pdf\*') #your pdf folder path
img_dir = "G:\\personal\\img\\" #your dest img path
for pdf_ in pdf_dir:
pages = convert_from_path(pdf_, 500)
for page in pages:
page.save(img_dir+pdf_.split("\\")[-1][:-3]+"jpg", 'JPEG')
Here is a solution which requires no additional libraries and is very fast. This was found from: https://nedbatchelder.com/blog/200712/extracting_jpgs_from_pdfs.html#
I have added the code in a function to make it more convenient.
def convert(filepath):
with open(filepath, "rb") as file:
pdf = file.read()
startmark = b"\xff\xd8"
startfix = 0
endmark = b"\xff\xd9"
endfix = 2
i = 0
njpg = 0
while True:
istream = pdf.find(b"stream", i)
if istream < 0:
break
istart = pdf.find(startmark, istream, istream + 20)
if istart < 0:
i = istream + 20
continue
iend = pdf.find(b"endstream", istart)
if iend < 0:
raise Exception("Didn't find end of stream!")
iend = pdf.find(endmark, iend - 20)
if iend < 0:
raise Exception("Didn't find end of JPG!")
istart += startfix
iend += endfix
jpg = pdf[istart:iend]
newfile = "{}jpg".format(filepath[:-3])
with open(newfile, "wb") as jpgfile:
jpgfile.write(jpg)
njpg += 1
i = iend
return newfile
Call convert with the pdf path as the argument and the function will create a .jpg file in the same directory
For a pdf file with multiple pages, the following is the best & simplest (I used pdf2image-1.14.0):
from pdf2image import convert_from_path
from pdf2image.exceptions import (
PDFInfoNotInstalledError,
PDFPageCountError,
PDFSyntaxError
)
images = convert_from_path(r"path/to/input/pdf/file", output_folder=r"path/to/output/folder", fmt="jpg",) #dpi=200, grayscale=True, size=(300,400), first_page=0, last_page=3)
images.clear()
Note:
"images" is a list of PIL images.
The saved images in the output folder will have system generated names; one can later change them, if required.
This easy script can convert a folder directory that contains PDFs (single/multiple pages) to jpeg.
from PIL import Image
import pytesseract
import sys
from pdf2image import convert_from_path
import os
from os import listdir
from os import system
from os.path import isfile, join, basename, dirname
import shutil
def move_processed_file(file, doc_path, download_processed):
try:
shutil.move(doc_path + '/' + file, download_processed + '/' + file)
pass
except Exception as e:
print(e.errno)
raise
else:
pass
finally:
pass
pass
def run_conversion():
root_dir = os.path.abspath(os.curdir)
doc_path = root_dir + r"\data\download"
pdf_processed = root_dir + r"\data\download\pdf_processed"
results_folder = doc_path
files = [f for f in listdir(doc_path) if isfile(join(doc_path, f))]
pdf_files = [f for f in listdir(doc_path) if isfile(join(doc_path, f)) and f.lower().endswith('.pdf')]
# check OS type
if os.name == 'nt':
# if is windows or a graphical OS, change this poppler path with your own path
poppler_path = r"C:\poppler-0.68.0\bin"
else:
poppler_path = root_dir + r"\usr\bin"
for file in pdf_files:
'''
# Converting PDF to images
'''
# Store all the pages of the PDF in a variable
pages = convert_from_path(doc_path + '/' + file, 500, poppler_path=poppler_path)
# Counter to store images of each page of PDF to image
image_counter = 1
filename, file_extension = os.path.splitext(file)
# Iterate through all the pages stored above
for page in pages:
# Declaring filename for each page of PDF as JPG
# PDF page n -> page_n.jpg
filename = filename + '_' + str(image_counter) + ".jpg"
# Save the image of the page in system
page.save(results_folder + '/' + filename, 'JPEG')
# Increment the counter to update filename
image_counter += 1
move_processed_file(file, doc_path, pdf_processed)
from pdf2image import convert_from_path
PDF_file = 'Statement.pdf'
pages = convert_from_path(PDF_file, 500,userpw='XXX')
image_counter = 1
for page in pages:
filename = "foldername/page_" + str(image_counter) + ".jpg"
page.save(filename, 'JPEG')
image_counter = image_counter + 1