Abid Omar
Abid Omar

Reputation: 31

Real time face recognition ran slowly on Raspberry Pi3

I am using Raspberry Pi3 for face recognition and this is my code to detect the faces but the real time recognition ran slowly

cam = cv2.VideoCapture(0)

rec = cv2.face.LBPHFaceRecognizer_create();

rec.read(...'/data/recognizer/trainingData.yml')
    getId = 0
    font = cv2.FONT_HERSHEY_SIMPLEX
    userId = 0
    i = 0
    while (cam.isOpened() and i<91):
        i=i+1
        ret, img = cam.read()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = faceDetect.detectMultiScale(gray, 1.3, 5)
        for (x, y, w, h) in faces:
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

            getId, conf = rec.predict(gray[y:y + h, x:x + w])  # This will predict the id of the face

            # print conf;
            if conf < 50:
                userId = getId
                cv2.putText(img, "Detected", (x, y + h), font, 2, (0, 255, 0), 2)
                record = Records.objects.get(id=userId)
                record.is_present = True
                record.save()
            else:
                cv2.putText(img, "Unknown", (x, y + h), font, 2, (0, 0, 255), 2)

            # Printing that number below the face
            # @Prams cam image, id, location,font style, color, stroke

        cv2.imshow("Face", img)
        cv2.waitKey(50)`

How to correct it please ? Thanks for your helping hand.

Upvotes: 0

Views: 757

Answers (1)

Ninad Gaikwad
Ninad Gaikwad

Reputation: 4480

You should use threads to mazimize performance. imutils is a library that lets you use threading both on picamera and webcam capture. The issue here is that there are too many Input output operations being performed in between frames.

Here is the article that helped increase my fps: https://www.pyimagesearch.com/2015/12/28/increasing-raspberry-pi-fps-with-python-and-opencv/

And this is the code you can add:

import imutils
from imutils.video.pivideostream import PiVideoStream

Then instead of cam = cv2.VideoCapture(0)

use cam = PiVideoStream().start()

and instead of ret, img = cam.read() use im = cam.read()

and to release the camera use:

cam.stop()

Upvotes: 1

Related Questions