首页 > 解决方案 > VideoCapture 显示“无摄像头访问”占位符图像

问题描述

我正在尝试显示来自相机的实时视频源。当我运行程序时, ret 返回 true 但 cv2.imshow() 显示占位符图像。任何帮助将不胜感激。

import numpy as np
from cv2 import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#initialize video from the webcam
video = cv2.VideoCapture(0)
print(cv2.VideoCapture(0).isOpened()) # ->returns True

while True:
# ret tells if the camera works properly. Frame is an actual frame from the video feed
    ret, frame= video.read()
    if ret ==True:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # Detect the faces
        faces = face_cascade.detectMultiScale(gray, 1.1, 4)
        # Draw the rectangle around each face
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        # Display
        cv2.imshow('img', frame)
        if cv2.waitKey(30) & 0xff==27:
            break
video.release()
cv2.destroyAllWindows()
    

cv2.imshow('img', frame) 打开以下窗口。

在此处输入图像描述

所以,我检查了相机权限是否被允许,似乎已经被允许了。我正在使用 MacOS Big Sur(11.6 版)。

在此处输入图像描述

标签: pythonmacosopencv

解决方案


推荐阅读