图像人脸检测
import cv2
import numpy as np
def StaticDetect(filename):
face_cascade = cv2.CascadeClassifier('data_cascades/haarcascades/haarcascade_frontalface_default.xml')
img = cv2.imread(filename)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray_img, 1.3, 5)
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.namedWindow('Face Detected!')
cv2.imshow('Face Detected!', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
视频人脸检测
import numpy as np
import cv2
cap=cv2.VideoCapture("C:/Users/ie/Desktop/lemon/law.mp4")
fourcc=cv2.VideoWriter_fourcc('M','P','4','2')
w=int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h=int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
video_writer=cv2.VideoWriter("C:/Users/ie/Desktop/lemon/law.avi",fourcc,fps=10,frameSize=(w,h))
face_detector=cv2.CascadeClassifier("C:/Users/ie/Desktop/lemon/haarcascade_frontalface_default.xml")
success,frame=cap.read()
while(success):
gray=cv2.cvtColor(frame,code=6)
face_zone= face_detector.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize=(50,50))
for x,y,w,h in face_zone:
cv2.circle(frame,center=(x+w//2,y+h//2),radius=w//2,color=[0,0,255],thickness=2)
cv2.imshow('law',frame)
video_writer.write(frame)
if cv2.waitKey(1000//24)==ord('q'):
break
success,frame=cap.read()
cap.release()
video_writer.release()
cv2.destroyAllWindows()
摄像头人脸检测
import numpy as np
import cv2
eye_detector = cv2.CascadeClassifier("C:/Users/ie/Desktop/lemon/haarcascade_eye.xml")
mouth_detector = cv2.CascadeClassifier("C:/Users/ie/Desktop/lemon/haarcascade_mcs_mouth.xml")
face_detector = cv2.CascadeClassifier("C:/Users/ie/Desktop/lemon/haarcascade_frontalface_default.xml")
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', '2')
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
video_writer = cv2.VideoWriter("C:/Users/ie/Desktop/lemon/luzi.avi", fourcc, fps=10, frameSize=(w, h))
while (cap.isOpened()):
success, frame = cap.read()
gray = cv2.cvtColor(frame, code=cv2.COLOR_BGR2GRAY)
face_zone = face_detector.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(50, 50))
for x, y, w, h in face_zone:
cv2.rectangle(frame, pt1=(x, y), pt2=(x + w, y + h), color=[0, 0, 255], thickness=2)
x, y, w, h in face_zone
head = gray[y:y + h, x:x + w]
split_h = int(h * 0.6)
up_head = head[0:split_h]
down_head = head[split_h:]
eye_zone = eye_detector.detectMultiScale(up_head, scaleFactor=1.2, minNeighbors=5, minSize=(50, 50))
for ex, ey, ew, eh in eye_zone:
cv2.circle(frame, center=(x + ex + ew // 2, y + ey + eh // 2), radius=ew // 2, color=[0, 0, 255], thickness=2)
mouth_zone = mouth_detector.detectMultiScale(down_head, scaleFactor=1.2, minNeighbors=5, minSize=(50, 50))
for mx, my, mw, mh in eye_zone:
cv2.rectangle(frame, pt1=(x + mx, y + my + split_h), pt2=(x + mx + mw, +y + my + mh + split_h),
color=[0, 0, 255], thickness=2)
cv2.imshow('cap', frame)
if cv2.waitKey(1000 // 24) == ord('q'):
break
cap.release()
video_writer.release()
cv2.destroyAllWindows()