Face recognition video crashing[SOLVED]
Hello!
I'm trying to use OpenCV for face recognition. For some reason the dataset creation part crashes after some seconds. Sometimes a few images can be captured, but it never works fully. No error messages are produced. Can anyone please see any issues in this code? I have stared at it for so long. I'm very thankful for any help!
import numpy as np import os import cv2 face_cascade = cv2.CascadeClassifier('/home/pi/opencv-3.3.0/data/haarcascades/haarcascade_frontalface_default.xml') print("Starting face recognition") print("Step 1: Dataset creation.") path = '/home/pi/Documents/FaceRecognition/dataset/'# path were u want store the data set id = input('Enter person\'s name: ') pathname=path+id print(pathname) try: # Create target Directory print(path+str(id)) os.mkdir(path+str(id)) print("Directory " , path+str(id), " Created ") except FileExistsError: print("Directory " , path+str(id) , " already exists") sampleN=0; whileLooper=0; sampleBreakPoint=30; print("Efter create directory") cap = cv2.VideoCapture(0) while(cap.isOpened()): whileLooper = whileLooper+1; print("Print1. Loop: "+str(whileLooper)) ret, img = cap.read() if not ret: break cv2.waitKey(10) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.waitKey(10) faces = face_cascade.detectMultiScale(gray,1.3, 5) cv2.waitKey(10) for (x,y,w,h) in faces: #print("In for loop "+str(sampleN)) sampleN=sampleN+1; cv2.imwrite(path+str(id)+ "/" +str(sampleN)+ ".jpg", gray[y:y+h,x:x+w]) cv2.waitKey(10) cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) print("Found image number " +str(sampleN)); cv2.imshow('img',img) cv2.waitKey(100) if sampleN > sampleBreakPoint: print(str(sampleBreakPoint)+" images found. Now breaking.") cv2.waitKey(300) break cap.release() cv2.destroyAllWindows()
sorry, but that is far too vague. please add a better description of the problem and actual error msgs to you question
I don't know, but waitKey is supposed to be used only with a highgui window and it _could be_, that otherwise it is unstable. Anyway, you use it in ways it is not designed for AFAIK
I don't see nothing wrong with your code. I am using OpenCV 4.2.0, 3B+, 4B It is working now. And the output:
This is what you see at beginning.
Upgrade OpenCV 4.2.0. Your code will worked too.
This isn't a discussion forum. Instead of adding
[SOLVED]
to the title, please mark an answer as solved instead. Otherwise, it just clutters up the list of unanswered questions forever.