cv2.imshow 不显示网络摄像头视频并且不打开任何 window
The cv2.imshow not showing webcam video and not opening any window
我在使用 OpenCV
显示 window 和 cam 捕获时遇到问题
当我 运行 脚本时,我看到摄像头正在工作,但是带有此摄像头的 window 没有显示在任何地方,我只有 Python 图标出现了,但它甚至无法点击。
设置:
- macOS 大苏尔 11.4
- python 3.8
- cv2 4.2.0
我试过:
- 在
ret, frame = video_capture.read()
之前添加睡眠
- 将 cv2.waitKey() 更改为 0、1 和 500
- 已在我的笔记本摄像头上测试,并在 phone 使用 IriunWebcam
- 尝试了不同的 IDE(可视代码和 PyCharm),并尝试 运行
终端中的脚本
import cv2
import sys
import logging as log
import datetime as dt
from time import sleep
faceCascade = cv2.CascadeClassifier('/Users/***/Documents/GitHub/FaceAuth/haarcascade_frontalface_default.xml')
log.basicConfig(filename='webcam.log', level=log.INFO)
video_capture = cv2.VideoCapture(0) # 0 for phone cam, 1 for pc cam
anterior = 0
while True:
if not video_capture.isOpened():
print('Unable to load camera.')
sleep(5)
pass
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 1.1, 4)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
if anterior != len(faces):
anterior = len(faces)
log.info("faces: " + str(len(faces)) + " at " + str(dt.datetime.now()))
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Display the resulting frame
cv2.imshow('Video', frame)
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
这是我在调试断点中的变量,在 cv2.imshow('Video', frame)
之前
来自评论区@idonthaveaname:
Have you tried upgrading opencv to 4.4.0? Maybe it should work, 4.4.0 is the latest version.
我在使用 OpenCV
显示 window 和 cam 捕获时遇到问题当我 运行 脚本时,我看到摄像头正在工作,但是带有此摄像头的 window 没有显示在任何地方,我只有 Python 图标出现了,但它甚至无法点击。
设置:
- macOS 大苏尔 11.4
- python 3.8
- cv2 4.2.0
我试过:
- 在
ret, frame = video_capture.read()
之前添加睡眠
- 将 cv2.waitKey() 更改为 0、1 和 500
- 已在我的笔记本摄像头上测试,并在 phone 使用 IriunWebcam
- 尝试了不同的 IDE(可视代码和 PyCharm),并尝试 运行 终端中的脚本
import cv2
import sys
import logging as log
import datetime as dt
from time import sleep
faceCascade = cv2.CascadeClassifier('/Users/***/Documents/GitHub/FaceAuth/haarcascade_frontalface_default.xml')
log.basicConfig(filename='webcam.log', level=log.INFO)
video_capture = cv2.VideoCapture(0) # 0 for phone cam, 1 for pc cam
anterior = 0
while True:
if not video_capture.isOpened():
print('Unable to load camera.')
sleep(5)
pass
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 1.1, 4)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
if anterior != len(faces):
anterior = len(faces)
log.info("faces: " + str(len(faces)) + " at " + str(dt.datetime.now()))
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Display the resulting frame
cv2.imshow('Video', frame)
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
cv2.imshow('Video', frame)
来自评论区@idonthaveaname:
Have you tried upgrading opencv to 4.4.0? Maybe it should work, 4.4.0 is the latest version.