【发布时间】:2020-08-13 05:10:37
【问题描述】:
import cv2
import sys
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture(0)
img_counter = 0
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
k = cv2.waitKey(1)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.5,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
# 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)
# Display the resulting frame
cv2.imshow('FaceDetection', frame)
if k%256 == 27: #ESC Pressed
break
elif k%256 == 32:
# SPACE pressed
img_name = "facedetect_webcam_{}.png".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
after running the code in python the image of my webcam turn into this
【问题讨论】:
-
你能减少你的代码,看看在捕获或颜色转换后是否已经直接出现失真?
-
我尝试使用其他简单的代码,但网络摄像头流是相同的。
-
你能发布已经导致问题的最小代码吗?
-
在此之前找到的代码工作,可能是我安装的一些库和其他软件导致了这个问题,我安装了Cmake和其他库dlib,opencv人脸识别,opencv contrib python和其他
-
我遇到了同样的问题,这个解决方案虽然是针对不同的问题,但对我有用
video_capture = cv2.VideoCapture(cv2.CAP_DSHOW)取自这个stackoverflow答案link