import cv2 as cv
def face_detect_demo(img):
gary = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
face_detect = cv.CascadeClassifier('D:/BaiduNetdiskDownload/OPENCV(WIN)/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')
face = face_detect.detectMultiScale(gary)
for x,y,w,h in face:
cv.rectangle(img,(x,y),(x+w,y+h),color=(0,0,255),thickness=2)
cv.imshow('result',img)
# gray_img = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
# cv.imshow('gray',gray_img)
# cv.imwrite('gray_face11.jpg',gray_img)
# cv.imshow('read_img',img)
# #修改尺寸
# resize_img = cv.resize(img,dsize=(200,200))
#
# #显示原图
# cv.imshow('img',img)
# #显示修改后的
# cv.imshow('resize_img',resize_img)
#
# #打印原图大小
# print('未修改',img.shape)
#
# print('修改后',resize_img.shape)
# #坐标
# x,y,w,h=100,100,100,100
# #绘制矩形
# cv.rectangle(img,(x,y,x+w,y+h),color=(0,0,255),thickness=1)
#
# #绘制圆形
# cv.circle(img,center=(x+w,y+h),radius=100,color=(255,0,0),thickness=5)
# #显示
# cv.imshow('re_ig',img)
#
#
# img = cv.imread('face2.jpg')
# face_detect_demo()
# # 等待
cap = cv.VideoCapture('1.mp4')
while True:
flag,frame = cap.read()
if not flag:
break
face_detect_demo(frame)
if ord('q') == cv.waitKey(0):
break
cv.destroyAllWindows()
cap.release()
运行结果如图:
?
|