Ask Your Question
0

How can I gain the speed on facial recognition

asked 2020-05-09 23:33:08 -0600

updated 2020-05-10 07:22:06 -0600

supra56 gravatar image

I am using my Raspberry to perform LBPH facial recognition. But it takes a long time to load the "trainer.yml" and It got stuck when I tried to draw a rectangle around the recognized face.

#coding=utf8
import cv2
import os
import time
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('face_trainer/trainer.yml')
cascadePath = "haarcascade_frontalface_alt.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)

font = cv2.FONT_HERSHEY_SIMPLEX

alarm = 0
idnum = 0

names = ['1', '2']

cam = cv2.VideoCapture(0)

minW = 0.1*cam.get(3)

minH = 0.1*cam.get(4)

while True:

    ret, img = cam.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.2,
        minNeighbors=5,
        minSize=(int(minW), int(minH))
    )

    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
        idnum, confidence = recognizer.predict(gray[y:y+h, x:x+w])

        if confidence < 85:
            idnum = names[idnum]
            confidence = "{0}%".format(round(100 - confidence))
        else:
            idnum = "unknown"
            time.sleep(5)
            alarm = 1
            break
            confidence = "{0}%".format(round(100 - confidence))

        cv2.putText(img, str(idnum), (x+5, y-5), font, 1, (0, 0, 255), 1)
        cv2.putText(img, str(confidence), (x+5, y+h-5), font, 1, (0, 0, 0), 1)

    cv2.imshow('camera', img)

    k = cv2.waitKey(10)
    if k == 27 or alarm == 1:
        cv2.imwrite('face.jpg',img)
        break

#os.system("python /home/pi/mail.py")
#os.system("rm face.jpg")

cam.release()
cv2.destroyAllWindows()
os.system("python /home/pi/python2.7_guest/main.py")
edit retag flag offensive close merge delete

Comments

and It got stuck when I tried to draw a rectangle

you probably need to explain that, can you show your code ?

berak gravatar imageberak ( 2020-05-10 02:22:48 -0600 )edit
Derekxxzzyy gravatar imageDerekxxzzyy ( 2020-05-10 06:14:45 -0600 )edit
1

Comment out this time.sleep(5) and break

supra56 gravatar imagesupra56 ( 2020-05-12 07:21:08 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-05-12 14:05:49 -0600

supra56 gravatar image

updated 2020-05-12 14:14:14 -0600

I attempted your code.

  • If you don't comment out. It will search for dataset's folder and if it not there, it will waits for 5 sec to to write filename. And it will stop executed.
  • If you comment out. If it is there, that fine. Otherwise, if not, it will write to filename. And you will see filename unknown. And then stop executed and ended immediately.

Just smiled your face in front of webcam. Any emotion will do trick.

if confidence < 85:
    idnum = names[idnum]
    confidence = "{0}%".format(round(100 - confidence))
else:
    idnum = "unknown"
    alarm = 1
    confidence = "{0}%".format(round(100 - confidence))
edit flag offensive delete link more

Comments

Btw, I just comment out os.system("python /home/pi/python2.7_guest/main.py")

supra56 gravatar imagesupra56 ( 2020-05-12 14:17:56 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-05-09 23:33:08 -0600

Seen: 229 times

Last updated: May 12 '20