【问题标题】:The cv2.imshow not showing webcam video and not opening any windowcv2.imshow 不显示网络摄像头视频并且不打开任何窗口
【发布时间】:2021-12-19 22:28:32
【问题描述】:

我在使用 OpenCV 显示窗口和摄像头时遇到问题

当我运行脚本时,我看到 cam 正在工作,但是带有这个 cam 的窗口没有显示在任何地方,我只显示了 Python 图标,但它甚至无法点击。

设置:

  • macOS Big Sur 11.4
  • python 3.8
  • cv2 4.2.0

我已经尝试过:

  • ret, frame = video_capture.read()之前添加睡眠
  • 将 cv2.waitKey() 更改为 0、1 和 500
  • 已在我的笔记本摄像头和手机上使用 IriunWebcam 进行了测试
  • 尝试了不同的 IDE(Visual 代码和 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)之前

【问题讨论】:

  • 你试过将opencv升级到4.4.0吗?也许它应该可以工作,4.4.0 是最新版本。
  • 尝试将faceCascade = cv2.CascadeClassifier('/Users/***/Documents/GitHub/FaceAuth/haarcascade_frontalface_default.xml') 更改为faceCascade = cv2.CascadeClassifier(cv2.haarcascades + "haarcascade_frontalface_default.xml")
  • 第二个 cv2.imshow('Video', frame) 应该做什么?它可能会不必要地减慢速度。

标签: python opencv video-processing face-recognition webcam-capture


【解决方案1】:

来自 cmets 部分的@idonthaveaname:

您是否尝试过将 opencv 升级到 4.4.0?也许它应该可以工作,4.4.0 是最新版本。

【讨论】:

    猜你喜欢
    • 2017-02-09
    • 2019-08-30
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 2016-12-02
    相关资源
    最近更新 更多