I have succefully installed a library to perform face recognition; I have tried to use this on a program but I get an attribute error.
# Import the face_recognition library
import face_recognition
import face_recognition_models
# Load a sample image
image = face_recognition.load_image_file(r'C:\Users\WAJIH\Desktop\code\face')
# Detect faces in the image
face_locations = face_recognition.face_locations(image, model="cnn")
# Print the number of faces detected
print(f"{len(face_locations)} faces detected")
out:
Traceback (most recent call last):
File "c:\Users\WAJIH\Desktop\Face-recognition-python-project-main\ss.py", line 1, in
import face_recognition
File "C:\Users\WAJIH\AppData\Local\Programs\Python\Python39\lib\site-packages\face_recognition_init_.py", line 7, in
from .api import load_image_file, face_locations, batch_face_locations, face_landmarks, face_encodings, compare_faces, face_distance
File "C:\Users\WAJIH\AppData\Local\Programs\Python\Python39\lib\site-packages\face_recognition\api.py", line 3, in
import PIL.Image
File "C:\Users\WAJIH\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL_init_.py", line 15, in
from . import _version
ValueError: source code string cannot contain null bytes
Related
I am trying to use the pytesseract tool on python, to extract text from the image I'm providing but I keep stumbling upon this error.
My code:
from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'/Users/khadraabdirahman/opt/anaconda3/bin/tesseract'
text_from_image = pytesseract.image_to_string(Image.open('testcopy.pdf'))
The error:
Traceback (most recent call last):
File "/Users/khadraabdirahman/PycharmProjects/pythonProject/testing.py", line 8, in <module>
text_from_image = pytesseract.image_to_string(Image.open('testcopy.pdf'))
File "/Users/khadraabdirahman/PycharmProjects/pythonProject/venv/lib/python3.7/site-packages/PIL/Image.py", line 3009, in open
"cannot identify image file %r" % (filename if filename else fp)
PIL.UnidentifiedImageError: cannot identify image file 'testcopy.pdf'
The file evidently exists, but imread() is unable to identify the image. Is there something I am missing? I'm still new to python and its respective libraries.
Code (Python 3.7.9)
import os
import time
import numpy as np
import matplotlib.pyplot as plt
from randomlyShiftChannels import *
from alignChannels import *
from utils import *
#Path to your data directory
data_dir = os.path.join('..', 'data', 'demosaic')
#List of images
image_names = ['balloon.jpeg','cat.jpg', 'ip.jpg',
'puppy.jpg', 'squirrel.jpg', 'pencils.jpg',
'house.png', 'light.png', 'sails.png', 'tree.jpeg'];
print('Evaluating alignment...')
for imgname in image_names:
# Read image
imgpath = os.path.join(data_dir, imgname)
print("File exists:", os.path.exists(imgpath))
img = imread(imgpath)
Output
File exists: True
Traceback (most recent call last):
File "D:\Spring2021\CS370\week 6\p2-release\code\evalAlignment.py", line 48, in <module>
img = imread(imgpath)
File "D:\Spring2021\CS370\week 6\p2-release\code\utils.py", line 14, in imread
img = plt.imread(path).astype(float)
File "C:\Users\dminn\AppData\Local\Programs\Spyder\pkgs\matplotlib\pyplot.py", line 2246, in imread
return matplotlib.image.imread(fname, format)
File "C:\Users\dminn\AppData\Local\Programs\Spyder\pkgs\matplotlib\image.py", line 1496, in imread
with img_open(fname) as image:
File "C:\Users\dminn\AppData\Local\Programs\Spyder\pkgs\PIL\Image.py", line 2944, in open
"cannot identify image file %r" % (filename if filename else fp)
UnidentifiedImageError: cannot identify image file '..\\data\\demosaic\\balloon.jpeg'
I faced the identical issue using Spyder (Spyder 5.0.0 and 5.0.1).
No errors appeared when running the script from the command line.
Found workaround for Spyder: use pil_to_array() instead of imread()
from PIL import Image
img = pil_to_array(Image.open(imgpath))
Try converting your path to an absolute path, there are reasons we call it good practice
Sample code:
import os
os.path.abspath("mydir/myfile.txt") #'C:/example/cwd/mydir/myfile.txt'
Till 1 hour back my opencv code was working perfectly fine. it could run my script without error. but I don't know why some time back suddenly it started giving me this error:
AttributeError: partially initialized module 'cv2' has no attribute 'imread' (most likely due to a circular import)
Traceback (most recent call last):
File "FACEDETECTION.PY", line 1, in <module>
import cv2
File "C:\Users\Lenovo\anaconda3\envs\CAM\lib\site-packages\cv2\__init__.py", line 5, in <module>
from .cv2 import *
ImportError: numpy.core.multiarray failed to import
So what could be the error?
update: Here is my code-
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe"
path = "text.png"
img = cv2.imread(path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
text = pytesseract.image_to_string(img)
print(text)
cv2.imshow("name", img)
cv2.waitKey(7000)
I have all packeges installed.
Heres the code:
from PIL import Image
import pytesseract
im = Image.open("sample1.jpg")
text = pytesseract.image_to_string(im, lang = 'eng')
print(text)
and the error is:
Traceback (most recent call last):
File "C:\Users\Usama Malik\Desktop\New folder (3)\tesseract-python-master\image_example.py", line 2, in <module>
import pytesseract
File "C:\Python27\lib\site-packages\pytesseract\__init__.py", line 1, in <module>
from .pytesseract import (
File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 9, in <module>
import Image
File "C:\Python27\lib\site-packages\PIL\Image.py", line 27, in <module>
from . import VERSION, PILLOW_VERSION, _plugins
ValueError: Attempted relative import in non-package
from PIL import Image
from pytesser import *
image_file = 'D:\plate.jpg'
im = Image.open(image_file)
text = image_to_string(im)
text = image_file_to_string(image_file)
text = image_file_to_string(image_file, graceful_errors=True)
print "=====output=======\n"
print text
it gets me error that
C:\Users\KEN\Anaconda2\python.exe C:/Users/KENIL/PycharmProjects/plate/ocr.py
Traceback (most recent call last):
File "C:/Users/KEN/PycharmProjects/plate/ocr.py", line 2, in <module>
from pytesser import *
File "C:\Users\KEN\PycharmProjects\plate\pytesser.py", line 6, in <module>
import Image
ImportError: No module named Image
even though i have installed ananconda properly as well as pillow screenshot of package installed
The code you posted doesn't have the error. From the error message it seems that the error is in the module pytesser.py, where you probably use Image (in line 6 :-) ) without importing it.