Ask Your Question
0

Face recognition video crashing[SOLVED]

asked 2020-02-20 10:05:16 -0600

erma gravatar image

updated 2020-02-27 20:57:24 -0600

supra56 gravatar image

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()
edit retag flag offensive close merge delete

Comments

but it never works fully.

sorry, but that is far too vague. please add a better description of the problem and actual error msgs to you question

berak gravatar imageberak ( 2020-02-20 10:35:34 -0600 )edit

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

mvuori gravatar imagemvuori ( 2020-02-20 13:56:17 -0600 )edit

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:

Found image number 28
Print1. Loop: 58
Found image number 29
Print1. Loop: 59
Found image number 30
Print1. Loop: 60
Found image number 31
30 images found. Now breaking.
supra56 gravatar imagesupra56 ( 2020-02-21 03:50:22 -0600 )edit

This is what you see at beginning.

Starting face recognition
Step 1: Dataset creation.
Enter person's name: Supra
/home/pi/Documents/FaceRecognition/dataset/Supra
/home/pi/Documents/FaceRecognition/dataset/Supra
Directory  /home/pi/Documents/FaceRecognition/dataset/Supra  Created 
Efter create directory
Print1. Loop: 1
Print1. Loop: 2
Print1. Loop: 3
Print1. Loop: 4
.....
.....
....
supra56 gravatar imagesupra56 ( 2020-02-21 03:58:33 -0600 )edit

Upgrade OpenCV 4.2.0. Your code will worked too.

supra56 gravatar imagesupra56 ( 2020-02-21 07:52:46 -0600 )edit

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.

ssokolow gravatar imagessokolow ( 2020-03-23 18:41:03 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-02-27 12:38:44 -0600

erma gravatar image

Thank you for your help. I think this was a power issue - that the program required a lot of stable energy supply for the Raspberry Pi to be run. I reinstalled everything on the Raspberry Pi and chose a charger with a higher output: 2.4 Ampere instead of 1 Ampere. Now it is not crashing :)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-02-20 10:05:16 -0600

Seen: 697 times

Last updated: Feb 27 '20