【问题标题】:Open CV has problem with opening webcam streamOpen CV 在打开网络摄像头流时出现问题
【发布时间】:2021-10-14 05:26:48
【问题描述】:

因此,我正在尝试制作简单的计算机视觉应用,在实时网络摄像头提要中在您的脸部周围显示不同颜色的方块。

问题是当我使用 vscode 终端启动应用程序时,我的笔记本电脑网络摄像头只是打开了一段时间然后关闭但没有出现应用程序窗口?

终端报错:

[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ttbyx0jz\opencv\modules\videoio\src\cap_msmf.cpp (1021) CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -2147483638
Traceback (most recent call last):
  File "C:\Users\asher\Downloads\Work\Work Stuff\Python Stuff\Learning Python AI blah blah\Face_Realtime.py", line 23, in <module>     
    grayscaled_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ttbyx0jz\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ttbyx0jz\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

我的应用代码:

import cv2
from random import randrange

# loading pre-trained data from opencv (haarcascade)
# classifier is just detector

trained_face_data = cv2.CascadeClassifier(
    'haarcascade_frontalface_default.xml')

webcam = cv2.VideoCapture(0)  # capturing live video

# loop to capture video
while True:

    successful_frame_read, frame = webcam.read()

    # we need to convert to grayscale before detecting faces

    grayscaled_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # we will detect faces using the line below
    face_coordinates = trained_face_data.detectMultiScale(grayscaled_img)

    for (x, y, w, h) in face_coordinates:  # loop to show all faces
        # create rectangles around face and randrage here creates random colors for rectangles
        cv2.rectangle(frame, (x, y), (x+w, y+h), (randrange(128, 256),randrange(128, 256), randrange(128, 256)), 10)
                  
    # this is app name for window and taking the img
    cv2.imshow('Face Detector', frame)
    key = cv2.waitKey(1)

    if key==81 or key==113:
        break

webcam.release()

print('Trippin through times lol... but code finished')

【问题讨论】:

  • 对我来说很好

标签: python c++ opencv camera computer-vision


【解决方案1】:

尝试添加if successful_frame_read:,检查帧是否读取成功。 if 语句确保只处理可读帧。这是因为successful_frame_read 的返回值是一个布尔值,正如您可能猜到的,它告诉您帧是否被成功读取。您可能需要它,因为某些帧可能已损坏并导致此错误。

您的代码应如下所示:

import cv2
from random import randrange

# loading pre-trained data from opencv (haarcascade)
# classifier is just detector

trained_face_data = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

webcam = cv2.VideoCapture(0)  # capturing live video

# loop to capture video
while True:

    successful_frame_read, frame = webcam.read()


    if successful_frame_read: # The newly added if statement
        # we need to convert to grayscale before detecting faces
        grayscaled_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # we will detect faces using the line below
        face_coordinates = trained_face_data.detectMultiScale(grayscaled_img)

        for (x, y, w, h) in face_coordinates:  # loop to show all faces
            # create rectangles around face and randrage here creates random colors for rectangles
            cv2.rectangle(frame, (x, y), (x+w, y+h), (randrange(128, 256),randrange(128, 256), randrange(128, 256)), 10)
                    
        # this is app name for window and taking the img
        cv2.imshow('Face Detector', frame)
        key = cv2.waitKey(1)

        if key==81 or key==113:
            break

webcam.release()

print('Trippin through times lol... but code finished')

修复属性错误

AttributeError: module 'cv2' has no attribute 'CascadeClassifier'

问题可能是安装错误。为确保不存在依赖问题,请安装Virtualenv(如果尚未安装)。

窗口:

  1. pip install virtualenv 安装 Virtualenv。

  2. cd C:\Users\asher\Downloads\Work\Work Stuff\Python Stuff\Learning Python AI blah blah\进入项目所在目录

  3. virtualenv venv创建一个名为venv的virtualenv

  4. venv\Scripts\activate 激活虚拟环境

  5. 安装 python 包。

要安装 OpenCV,请使用 pip install opencv-python

【讨论】:

  • 使用这个我得到了这个错误:Traceback(最近一次调用最后一次):文件“C:\Users\asher\Downloads\Work\Work Stuff\Python Stuff\Learning Python AI blah blah\Face_test. py",第 7 行,在 trained_face_data = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') AttributeError: module 'cv2' has no attribute 'CascadeClassifier'
  • 嘿,创建虚拟环境后,该属性错误已修复.. 但线程的原始问题存在程序永远不会打开,我在终端中收到此错误:[WARN:0] global C :\Users\runneradmin\AppData\Local\Temp\pip-req-build-1i5nllza\opencv\modules\videoio\src\cap_msmf.cpp (1022) CvCapture_MSMF::grabFrame videoio(MSMF):无法抓取帧。错误:-2147483638
  • 太好了,请投票帮助其他人。
猜你喜欢
  • 1970-01-01
  • 2018-04-26
  • 2015-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
相关资源
最近更新 更多