【问题标题】:The Picamera does not work in face recognition using Raspberry PiPicamera 无法使用 Raspberry Pi 进行人脸识别
【发布时间】:2020-12-24 03:21:30
【问题描述】:

我是 python 和 R-Pi 的新手。所以看完 Adrian 的教程后,我想让面部识别工作。 (https://www.pyimagesearch.com/2018/06/11/how-to-build-a-custom-face-recognition-dataset/)

我的 picamera 正常工作,但是按照上面的帖子,我的 picamera 不工作。

这是我的源代码:

from imutils.video import VideoStream
import argparse
import imutils
import time
import cv2
import os

ap = argparse.ArgumentParser()
ap.add_argument("-c", "--cascade", required=True,
    help = "path to where the face cascade resides")
ap.add_argument("-o", "--output", required=True,
    help="path to output directory")
args = vars(ap.parse_args())

detector = cv2.CascadeClassifier(args["cascade"])
print("[INFO] starting video stream...")
#vs = VideoStream(src=0).start()
 vs = VideoStream(usePiCamera=True).start()
time.sleep(2.0)
total = 0

while True:

    frame = vs.read()
    orig = frame.copy()
    frame = imutils.resize(frame, width=400)

    rects = detector.detectMultiScale(
        cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), scaleFactor=1.1, 
        minNeighbors=5, minSize=(30, 30))

    for (x, y, w, h) in rects:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF
 
    if key == ord("k"):
        p = os.path.sep.join([args["output"], "{}.png".format(
            str(total).zfill(5))])
        cv2.imwrite(p, orig)
        total += 1
    elif key == ord("q"):
        break

print("[INFO] {} face images stored".format(total))
print("[INFO] cleaning up...")
cv2.destroyAllWindows()
vs.stop()

当我运行这段代码时,

[INFO] starting video stream...

出现但相机没有出现在树莓派上;而且,当我面对镜头时,

[INFO] {} face images stored 
[INFO] cleaning up...

出现。

【问题讨论】:

  • 如果尚未完成,您可以让 piCamera 像普通网络摄像头一样工作。所以它更容易与 CV2 一起使用。你有 /dev/video0 吗?
  • 启用 /dev/video0:sudo modprobe bcm2835-v4l2 或将 bcm2835-v4l2 添加到 /etc/modules
  • raspistill 还在拍照吗?您是否在 raspi-config 中激活了网络摄像头?

标签: python raspberry-pi face-recognition


【解决方案1】:

你可以试试这个。

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

【讨论】:

  • 请解释为什么您的代码是问题的答案 - OP 的代码有什么问题?
  • 你试过我的代码了吗?行得通吗?如果是这种情况并且您还没有使用 bcm2835-v4l2 做这些事情,请尝试使用:vs = VideoStream(src=0).start()
  • 只是想找出正在发生的事情。
  • 你看到问题下的 cmets 了吗?
  • 非常感谢您的回答!多亏了你,我学会了以不同的方式处理它。!
猜你喜欢
  • 2018-05-26
  • 2021-07-27
  • 2014-04-15
  • 2021-03-12
  • 2014-01-20
  • 2014-05-04
  • 2012-11-01
  • 2012-12-10
  • 2014-05-21
相关资源
最近更新 更多