【问题标题】:Why does this Facial detection code on python using open cv on raspberry pi outputs a blank screen?为什么在树莓派上使用 open cv 在 python 上的面部检测代码会输出一个空白屏幕?
【发布时间】:2022-09-28 04:11:56
【问题描述】:

我目前正在学习 OpenCV,我已经尝试了 picamera2 库 Github 中的这个示例代码,它只输出一个空白窗口,它应该输出相机镜头和一些人脸检测方块。它在调试或运行程序时不会给出任何错误消息。

我找不到对此代码的任何引用或为什么会发生这种情况,相机工作正常。

 import cv2

from picamera2 import Picamera2

 #Grab images as numpy arrays and leave everything else to OpenCV.

face_detector = cv2.CascadeClassifier(\"/home/pi/opencv-2.4.10/data/haarcascades/haarcascade_frontalface_default.xml\") cv2.startWindowThread()

picam2 = Picamera2() picam2.configure(picam2.create_preview_configuration(main={\"format\": \'XRGB8888\', \"size\": (640, 480)})) picam2.start()

while True:
    im = picam2.capture_array()

    grey = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    faces = face_detector.detectMultiScale(grey, 1.1, 5)

    for (x, y, w, h) in faces:
        cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0))

    cv2.imshow(\"Camera\", im)

我正在运行它带有靶心操作系统的树莓派 4, 我已经安装了蟒蛇 3.9.2在视觉工作室上,连接的相机是树莓派总部相机 V1.0 2018.在终端上使用命令 libcamera-hello 时,它可以很好地输出摄像机的实时画面。

    标签: python opencv raspberry-pi


    【解决方案1】:

    将 cv2.startWindowThread() 添加到第 10 行。我将 xml 放在当前文件夹.

    #!/usr/bin/python3
    
    import cv2
    
    from picamera2 import Picamera2
    
    # Grab images as numpy arrays and leave everything else to OpenCV.
    
    face_detector = cv2.CascadeClassifier("/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml")
    cv2.startWindowThread()
    
    picam2 = Picamera2()
    picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (640, 480)}))
    picam2.start()
    
    while True:
        im = picam2.capture_array()
    
        grey = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        faces = face_detector.detectMultiScale(grey, 1.1, 5)
    
        for (x, y, w, h) in faces:
            cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0))
    
        cv2.imshow("Camera", im)
    

    【讨论】:

      猜你喜欢
      • 2018-03-17
      • 1970-01-01
      • 2022-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多