时间:2022-1-13 作者:Deri 分类: Python
需要先安装库:dlib/opencv-py/numpy
安装过程有点麻烦需要耐心;
注意修改代码中的图片路径(要完整路径)
在电脑上运营后会弹出识别结果弹窗,点击这个窗口按ESC退出
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import cv2
import dlib
import numpy as np
def plot_rectangle(image, faces):
for face in faces:
cv2.rectangle(image, (face.left(), face.top()), (face.right(), face.bottom()), (200,200,0), 1)
return image
def main():
img = cv2.imread("/Users/xxx/Documents/python/face/test.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
detector = dlib.get_frontal_face_detector()
dets_result = detector(gray, 1)
img_result = plot_rectangle(img.copy(), dets_result)
cv2.imshow("Faceshow", img_result)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()