I am trying to create a face recognition software using OpenCV, but the code I found in the library is made in Python 2. Is there a Python 3 version of this?
Here's the link: https://github.com/thecodacus/Face-Recognition
I already have a folder for dataset and trainer.
import cv2
import numpy as np
from PIL import Image
import os
# Path for face image database
path = 'dataset'
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml");
# function to get the images and label data
def getImagesAndLabels(path):
imagePaths = [os.path.join(path,f) for f in os.listdir(path)]
faceSamples=[]
ids = []
for imagePath in imagePaths:
PIL_img = Image.open(imagePath).convert('L') # convert it to grayscale
img_numpy = np.array(PIL_img,'uint8')
id = int(os.path.split(imagePath)[-1].split('.')[1])
faces = detector.detectMultiScale(img_numpy)
for (x,y,w,h) in faces:
faceSamples.append(img_numpy[y:y+h,x:x+w])
ids.append(id)
return faceSamples,ids
print ("\n [INFO] Training faces. It will take a few seconds. Wait ...")
faces,ids = getImagesAndLabels(path)
recognizer.train(faces, np.array(ids))
# Save the model into trainer/trainer.yml
recognizer.write('trainer/trainer.yml') # recognizer.save() worked on Mac, but not on Pi
# Print the numer of faces trained and end program
print("\n [INFO] {0} faces trained. Exiting Program".format(len(np.unique(ids))))
Error:
Traceback (most recent call last):
File "/Users/user/Desktop/FacialRecognition/02_face_training.py", line 46, in <module>
faces,ids = getImagesAndLabels(path)
File "/Users/user/Desktop/FacialRecognition/02_face_training.py", line 36, in getImagesAndLabels
id = int(os.path.split(imagePath)[-1].split('.')[1])
ValueError: invalid literal for int() with base 10: 'User'
On that repository there's a dataSet directory, with a file named like:
In [665]: name='Face-Recognition/dataSet/face-1.1.jpg'
Applied to that name, your code sample does:
In [668]: os.path.split(name)
Out[668]: ('Face-Recognition/dataSet', 'face-1.1.jpg')
In [669]: os.path.split(name)[-1]
Out[669]: 'face-1.1.jpg'
In [670]: os.path.split(name)[-1].split('.')
Out[670]: ['face-1', '1', 'jpg']
In [671]: os.path.split(name)[-1].split('.')[1]
Out[671]: '1'
In [672]: int(os.path.split(name)[-1].split('.')[1])
Out[672]: 1
Apparently your file has a different name format, one that includes 'User' in a slot where this code expects a number.
You need to correct the file name, or change this parsing code.
The image name which you got in the dataset are as User.*somename*, so remove User from all the image names.
try to change format images is 'face.1.1.jpg'
then you can split the dot with this code
faceID = int(os.path.split(imagePath)[-1].split(".")[2])
This did the job for me:
Id=int(os.path.split(imagePath)[-1].split(".")[0])
Related
I try to extract some images from my pdf file, I used several methods but most of them were based on the Fitz library.
import fitz
import io
from PIL import Image
pdf_file = fitz.open("my_file_pdf.pdf")
for page_index in range(len(pdf_file)):
# get the page itself
page = pdf_file[page_index]
image_list = page.getImageList()
# printing number of images found in this page
if image_list:
print(f"[+] Found {len(image_list)} images in page {page_index}")
else:
print("[!] No images found on the given pdf page", page_index)
for image_index, img in enumerate(page.getImageList(), start=1):
print(img)
print(image_index)
# get the XREF of the image
xref = img[0]
# extract the image bytes
base_image = pdf_file.extractImage(xref)
image_bytes = base_image["image"]
# get the image extension
image_ext = base_image["ext"]
# load it to PIL
image = Image.open(io.BytesIO(image_bytes))
# save it to local disk
image.save(open(f"image{page_index+1}_{image_index}.{image_ext}", "wb"))
This code gives me the error :
AttributeError Traceback (most recent call last)
<ipython-input-1-e5b882e88684> in <module>
11 # get the page itself
12 page = pdf_file[page_index]
---> 13 image_list = page.getImageList()
14 # printing number of images found in this page
15 if image_list:
AttributeError: 'Page' object has no attribute 'getImageList'
However according to the documentation this is the way to use this function so where could the problem come from?
Instead of page.getImageList() try using page.get_images()
A list of attributes and methods belonging to the Page object is given at https://pymupdf.readthedocs.io/en/latest/page.html.
getImageList() is not included however get_images() is.
I've been trying to replicate the demo website from this repo using Streamlit.
But I'm stuck when I'm going to process the image with the model. The error message is AttributeError: 'str' object has no attribute 'name'. But in data.py or the code to read the image there is no 'name' attribute. Or I'm missing something here?
This is the snippet code
streamlitdemo.py
#st.cache()
def load_model():
gpu_ids=[]
model = create_model(gpu_ids)
model.eval()
return model
a = 'wave.jpg'
b = 'building.jpg'
c = 'test_samples/madoka.jpg'
def anime2sketch(img_input, load_size=512):
img, aus_resize = read_img_path(img_input.name, load_size)
model = load_model()
aus_tensor = model(img)
aus_img = tensor_to_img(aus_tensor)
image_pil = Image.fromarray(aus_img)
image_pil = image_pil.resize(aus_resize, Image.BICUBIC)
return image_pil
demo.py
.
.
.
def read_img_path(path, load_size):
"""read tensors from a given image path
Parameters:
path (str) -- input image path
load_size(int) -- the input size. If <= 0, don't resize
"""
img = Image.open(path).convert('RGB')
aus_resize = None
if load_size > 0:
aus_resize = img.size
transform = get_transform(load_size=load_size)
image = transform(img)
return image.unsqueeze(0), aus_resize
model.py
.
.
.
def create_model(gpu_ids=[]):
"""Create a model for anime2sketch
hardcoding the options for simplicity
"""
norm_layer = functools.partial(nn.InstanceNorm2d, affine=False, track_running_stats=False)
net = UnetGenerator(3, 1, 8, 64, norm_layer=norm_layer, use_dropout=False)
ckpt = torch.load('weights/netG.pth')
for key in list(ckpt.keys()):
if 'module.' in key:
ckpt[key.replace('module.', '')] = ckpt[key]
del ckpt[key]
net.load_state_dict(ckpt)
if len(gpu_ids) > 0:
assert(torch.cuda.is_available())
net.to(gpu_ids[0])
net = torch.nn.DataParallel(net, gpu_ids) # multi-GPUs
return net
But, when I'm hardcode the path with a/b/c variable, the model work properly. And I've already change read_img_path(img_input.name, load_size) to read_img_path(img_input, load_size) and I got FileNotFoundError: [Errno 2] No such file or directory: 'wave' error message.
This is the output when I'm hardcode the path
In that repo, the author already provide demo website but using Gradio. When I tried to run the demo code with Gradio it is work properly. I'm using the same code from the author, but I tweak it a little bit.
Thank you.
At this line recognizer = cv2.face.LBPHFaceRecognizer_create() i'm getting an error cv2.cv2 module has no attribute 'face'. I already tried uninstalling and reinstall opencv contrib but i still get the error.
import cv2
import numpy as np
from PIL import Image
import os
# Path for face image database
path = '/home/pi/FacialRecognitionProject/dataset/'
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier("/home/pi/FacialRecognitionProject/haarcascade_frontalface_default.xml");
# function to get the images and label data
def getImagesAndLabels(path):
imagePaths = [os.path.join(path,f) for f in os.listdir(path)]
faceSamples=[]
ids = []
for imagePath in imagePaths:
PIL_img = Image.open(imagePath).convert('L') # convert it to grayscale
img_numpy = np.array(PIL_img,'uint8')
id = int(os.path.split(imagePath)[-1].split(".")[1])
faces = detector.detectMultiScale(img_numpy)
for (x,y,w,h) in faces:
faceSamples.append(img_numpy[y:y+h,x:x+w])
ids.append(id)
return faceSamples,ids
print ("\n [INFO] Training faces. It will take a few seconds. Wait ...")
faces,ids = getImagesAndLabels(path)
recognizer.train(faces, np.array(ids))
# Save the model into trainer/trainer.yml
recognizer.write('/home/pi/FacialRecognitionProject/trainer/trainer.yml') # recognizer.save() worked on Mac, but not on Pi
# Print the numer of faces trained and end program
print("\n [INFO] {0} faces trained. Exiting Program".format(len(np.unique(ids))))
Hi i've a script to run image process on a image. But i'm trying to get a loop or another way to read multiple images from a file
e.g
C:\Users\student\Desktop\Don\program (opencv version)\Images\move1
move1 contains images named as frame1.jpg , frame2.jpg , frame3.jpg...
The script i'm using to run the image process is something like
img = cv2.imread('frame1.jpg')
mimg = cv2.medianBlur(img,15)
gimg = cv2.cvtColor(mimg,cv2.COLOR_RGB2GRAY)
ret,th1 = cv2.threshold(gimg, 160,255,cv2.THRESH_BINARY)
ret,th2 = cv2.threshold(th1, 160,255,cv2.THRESH_BINARY_INV)
cv2.imwrite('threshbinaryinv.jpg', th2)
My script above could only read images that i manually keyed in e.g 'frame1.jg'. Sorry i'm very new to python. Thanks!
EDIT
This the code i edited with you guys help.. still getting error as "Traceback (most recent call last):
File "C:\Users\student\Desktop\Don\program (opencv version)\prog.py", line 32, in
gimg = cv2.cvtColor(mimg,cv2.COLOR_RGB2GRAY) #convert RBG to Grayscale
cv2.error: D:\Build\OpenCV\opencv-3.3.1\modules\imgproc\src\color.cpp:11048: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor"
CODE
path_of_images = 'C:/Users/student/Desktop/Don/program (opencv version)/Images'
list_of_images = os.listdir(path_of_images)
for image in list_of_images:
img = cv2.imread(os.path.join(path_of_images, image))
mimg = cv2.medianBlur(img,15)
gimg = cv2.cvtColor(mimg,cv2.COLOR_RGB2GRAY)
ret,th1 = cv2.threshold(gimg, 160,255,cv2.THRESH_BINARY)
ret,th2 = cv2.threshold(th1, 160,255,cv2.THRESH_BINARY_INV)
cv2.imwrite('threshbinaryinv.jpg', th2)
You can use os.listdir() to get the names of all images in your specified path which is "C:\Users\student\Desktop\Don\program (opencv version)\Images". Then you can loop over the names of images like :
import os
import cv2
path_of_images = r"C:\Users\student\Desktop\Don\program (opencv version)\Images"
list_of_images = os.listdir(path_of_images)
for image in list_of_images:
img = cv2.imread(os.path.join(path_of_images, image))
"""Your code here"""
It can be done using a for loop and generating a new str file name and then processing it as:
IMG_FOLDER_PREFIX = "absolute/path/to/frame"
IMG_EXTENSION = ".jpg"
NUM_IMAGES = 10
for i in xrange(NUM_IMAGES):
image_path = IMG_FOLDER_PREFIX + str(i) + IMG_EXTENSION
img = cv2.imread(image_path)
# Other Image Processing.
A better way to iterate images would be os.listdir, glob, etc. but in that case you may have lesser control over the order of files traversed.
when i run this code error has occurred..
I did a lot of searching but I could not find a solution.
Traceback <most recent call last>:
File "ASL.py", line 24, in <module>
img1=img[100:500, 900:1300]
TypeError: 'NoneType' object has no attribute '__getitem__'
And this is ASL.py code!!
import cv2
import numpy as np
import util as ut
import svm_train as st
import re
model=st.trainSVM(17)
#create and train SVM model each time coz bug in opencv 3.1.0 svm.load() https://github.com/Itseez/opencv/issues/4969
cam=int(raw_input("Enter Camera number: "))
cap=cv2.VideoCapture(cam)
font = cv2.FONT_HERSHEY_SIMPLEX
def nothing(x) :
pass
text= " "
temp=0
previouslabel=None
previousText=" "
label = [None]
while(cap.isOpened()):
_,img=cap.read()
cv2.rectangle(img,(900,100),(1300,500),(255,0,0),3)
img1=img[100:500, 900:1300]
img_ycrcb = cv2.cvtColor(img1, cv2.COLOR_BGR2YCR_CB)
blur = cv2.GaussianBlur(img_ycrcb,(11,11),0)
skin_ycrcb_min = np.array((0, 138, 67))
skin_ycrcb_max = np.array((255, 183, 133))
mask = cv2.inRange(blur, skin_ycrcb_min, skin_ycrcb_max) # detecting the hand in the bounding box using skin detection
contours,hierarchy = cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL, 2)
cnt=ut.getMaxContour(contours,4000) # using contours to capture the skin filtered image of the hand
if cnt!=None:
gesture,label=ut.getGestureImg(cnt,img1,mask,model) # passing the trained model for prediction and fetching the result
if(label!=None):
if(temp==0):
previouslabel=label
if previouslabel==label :
previouslabel=label
temp+=1
else :
temp=0
if(temp==40):
if(label=='P'):
label=" "
text= text + label
if(label=='Q'):
words = re.split(" +",text)
words.pop()
text = " ".join(words)
#text=previousText
print text
cv2.imshow('PredictedGesture',gesture) # showing the best match or prediction
cv2.putText(img,label,(50,150), font,8,(0,125,155),2) # displaying the predicted letter on the main screen
cv2.putText(img,text,(50,450), font,3,(0,0,255),2)
cv2.imshow('Frame',img)
cv2.imshow('Mask',mask)
k = 0xFF & cv2.waitKey(10)
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
This code is the translation of the sign language.
I tried to solve it, but I could not.
What parts should I change?
The more detailed the better.
I would appreciate your advice.
cap.read() was unsuccessful, so img is None. From the docs:
If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer.