【发布时间】: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