【问题标题】:Real time face recognition ran slowly on Raspberry Pi3实时人脸识别在树莓派3上跑得很慢
【发布时间】:2018-05-06 18:05:45
【问题描述】:

我正在使用 Raspberry Pi3 进行人脸识别,这是我检测人脸的代码,但实时识别运行缓慢

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)`

请问如何改正?感谢您的帮助。

【问题讨论】:

  • 你确定你的代码可以在树莓派上快速运行吗?
  • 是的,代码在树莓派上运行很快,但是当凸轮打开时,帧被延迟

标签: python raspberry-pi3 face-recognition


【解决方案1】:

您应该使用线程来最大限度地提高性能。 imutils 是一个库,可让您在 picamera 和网络摄像头捕获上使用线程。这里的问题是在帧之间执行了太多的输入输出操作。

这是帮助我提高 fps 的文章: https://www.pyimagesearch.com/2015/12/28/increasing-raspberry-pi-fps-with-python-and-opencv/

这是你可以添加的代码:

import imutils
from imutils.video.pivideostream import PiVideoStream

然后代替cam = cv2.VideoCapture(0)

使用cam = PiVideoStream().start()

而不是ret, img = cam.read() 使用im = cam.read()

并释放相机使用:

cam.stop()

【讨论】:

    猜你喜欢
    • 2016-09-20
    • 2020-10-18
    • 2021-03-18
    • 2019-01-21
    • 2017-10-05
    • 2019-11-11
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    相关资源
    最近更新 更多