【发布时间】:2022-01-21 18:21:19
【问题描述】:
我正在尝试在 opencv 中检测人脸,但我遇到了一些问题: 1-当我输入以下语法时:
gray = cv2.cvtColor(frames,cv2.COLOR_BGR2GRAY),它显示为红色并且不起作用。 2-haarcascade 也显示为红色:faces = face_cascade.detectMultiScale( gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30),flags = cv2.CV_HAAR_SCALE_IMAGE)。我尝试在一些教程中这样做,但它不起作用。你有什么想法吗? 这是我的代码:
#importing packages
import cv2
import numpy as np
#variables
webcam = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
w_size = (700,500)
#turn the webcam on
while (True):
#reading camera and turing into frames
ret,frames = webcam.read()
frames = cv2.resize(frames,w_size)
#detection
gray = cv2.cvtColor(frames,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale( gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30),flags = cv2.CV_HAAR_SCALE_IMAGE)
for (x, y, w, h) in faces:
cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('face_recognition',frames)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#running script
webcam.release()
cv2.destroyAllWindows()
【问题讨论】:
-
“不起作用”是对该问题的糟糕描述。详细说明。 --“显示为红色”表示您的 IDE 很混乱。这并不重要,因为某些 IDE 在其“拼写检查”功能方面并不是那么可靠。重要的是代码是运行还是抛出错误。
-
感谢您的建议,下次会记住的。
标签: python opencv haar-classifier cascade-classifier