系統:win10
編譯環境:pycharm
python庫:cv2、dlib(自己安裝)
代碼:
import dlib
import cv2
# 加載並初始化檢測器
detector = dlib.get_frontal_face_detector()
camera = cv2.VideoCapture('./data/1.mp4')
if not camera.isOpened():
print("cannot open camear")
exit(0)
j=0
while True:
ret, frame = camera.read()
if not ret:
break
frame_new = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 檢測臉部
dets = detector(frame_new, 1)
print("Number of faces detected: {}".format(len(dets)))
# 查找臉部位置
for i, face in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {} ".format(
i, face.left(), face.top(), face.right(), face.bottom()))
# 繪製臉部位置
cv2.rectangle(frame, (face.left(), face.top()), (face.right(), face.bottom()), (0, 255, 0), 1)
#保存臉部圖片
img1=frame[face.top():face.bottom(),face.left():face.right()]
cv2.imwrite("./data/zy"+str(j)+'.jpg',img1)
j=j+1
cv2.imshow("Camera", frame)
key = cv2.waitKey(1)
if key == 27:
break
cv2.destroyAllWindows()