Hello!
I'm trying to use OpenCV for face recognition. For some reason the dataset creation part crashes after a while. Sometimes a few images can be captured, but it never works fully. 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(100) print("Print2") cv2.waitKey(10) print("Print3") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.waitKey(10) print("Print4") faces = face_cascade.detectMultiScale(gray,1.3, 5) print("Print5") 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) #cv2.waitKey(100) print("Found image number " +str(sampleN)); print("Print6") cv2.imshow('img',img) print("Print7") cv2.waitKey(100) print("Print8") if sampleN > sampleBreakPoint: print(str(sampleBreakPoint)+" images found. Now breaking.") cv2.waitKey(300) break print("Print9") cap.release() cv2.destroyAllWindows()