代码拉取完成,页面将自动刷新
import cv2
import numpy as np
# Load the face cascade file
face_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_frontalface_alt.xml')
nose_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_mcs_nose.xml')
# Check if the face cascade file has been loaded
if face_cascade.empty():
raise IOError('Unable to load the face cascade classifier xml file')
# Initialize the video capture object
cap = cv2.VideoCapture(0)
# Define the scaling factor
scaling_factor = 0.5
# Loop until you hit the Esc key
while True:
# Capture the current frame and resize it
ret, frame = cap.read()
frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor,
interpolation=cv2.INTER_AREA)
# Convert to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Run the face detector on the grayscale image
face_rects = face_cascade.detectMultiScale(gray, 1.3, 5)
# Draw rectangles on the image
for (x, y, w, h) in face_rects:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = frame[y:y + h, x:x + w]
noses = nose_cascade.detectMultiScale(roi_gray, 1.3, 5)
for (ex, ey, ew, eh) in noses:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 0, 255), 1)
break
# Display the image
cv2.imshow('Face and Nose Detector - Camera', frame)
# Check if Esc key has been pressed
c = cv2.waitKey(1)
if c == 27:
break
# Release the video capture object and close all windows
cap.release()
cv2.destroyAllWindows()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。