系统:ubantu16.04
编辑器:pycharm
所需python库:python_opencv,face_recognition
代码如下:
import face_recognition import cv2 camera=cv2.VideoCapture(0) n=0 while(1): if n<10: success,img=camera.read() face_locations = face_recognition.face_locations(img) face_encodings = face_recognition.face_encodings(img, face_locations) print(face_locations) if len(face_locations)>=1: top = face_locations[0][0] right = face_locations[0][1] bottom = face_locations[0][2] left = face_locations[0][3] print(top) start = (left, top) end = (right, bottom) color = (55, 255, 155) thickness = 3 img=cv2.rectangle(img, start, end, color, thickness) cv2.imshow('img', img) n = n + 1 key = cv2.waitKey(30) & 0xFF print(key) if key == 113: break else: break print(n) # camera.release() cv2.destroyAllWindows()
结果如下: