我确信您现在已经解决了您的错误,但这适用于访问此问题的其他人:
写下这是你的相机.py file
不要对draw_box方法感到困惑,它只是我正在使用的自定义正方形。您可以使用普通的 cv2.rectangle 在面上制作矩形。
import cv2
WHITE = [255, 255, 255]
face_cascade = cv2.CascadeClassifier('Haar/haarcascade_frontalcatface.xml')
eye_cascade = cv2.CascadeClassifier('Haar/haarcascade_eye.xml')
def draw_box(Image, x, y, w, h):
cv2.line(Image, (x, y), (x + int(w / 5), y), WHITE, 2)
cv2.line(Image, (x + int((w / 5) * 4), y), (x + w, y), WHITE, 2)
cv2.line(Image, (x, y), (x, y + int(h / 5)), WHITE, 2)
cv2.line(Image, (x + w, y), (x + w, y + int(h / 5)), WHITE, 2)
cv2.line(Image, (x, (y + int(h / 5 * 4))), (x, y + h), WHITE, 2)
cv2.line(Image, (x, (y + h)), (x + int(w / 5), y + h), WHITE, 2)
cv2.line(Image, (x + int((w / 5) * 4), y + h), (x + w, y + h), WHITE, 2)
cv2.line(Image, (x + w, (y + int(h / 5 * 4))), (x + w, y + h), WHITE, 2)
class VideoCamera(object):
def __init__(self):
self.video = cv2.VideoCapture(0)
def __del__(self):
self.video.release()
def get_frame(self):
success, image = self.video.read()
# We are using Motion JPEG, but OpenCV defaults to capture raw images,
# so we must encode it into JPEG in order to correctly display the
# video stream.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
gray_face = cv2.resize((gray[y:y + h, x:x + w]), (110, 110))
eyes = eye_cascade.detectMultiScale(gray_face)
for (ex, ey, ew, eh) in eyes:
draw_box(gray, x, y, w, h)
ret, jpeg = cv2.imencode('.jpg', gray)
return jpeg.tobytes()