写一段python人脸识别视频的代码
时间: 2023-03-07 09:30:10 浏览: 118
CNN_FacesRecognition-master.zip
答:import cv2 # Start capturing the video
cap = cv2.VideoCapture(0) # Load the cascade
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") while True:
# Read the frame
_, frame = cap.read() # Convert to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect the faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4) # Draw the rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # Display
cv2.imshow('frame', frame) # Stop if escape key is pressed
k = cv2.waitKey(30) & 0xff
if k==27:
break# Release the VideoCapture object
cap.release()
阅读全文