1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #!/usr/bin/env python
-
- import sys
- import cv2
- import numpy as np
- import face_recognition
-
- def record(path, dev):
- def draw_face_rec(name, frame):
- rgb_frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
- framelocs = face_recognition.face_locations(rgb_frame)
- frameencs = face_recognition.face_encodings(rgb_frame, framelocs)
- for (top, right, bottom, left), frameenc in zip(framelocs, frameencs):
- cv2.rectangle(rgb_frame, (left, top), (right, bottom), (0, 0, 255), 2)
-
- cv2.imshow(name, rgb_frame)
-
- cap = cv2.VideoCapture(dev)
-
- avg = 128
- while True:
- ret, frame = cap.read()
- mean = cv2.mean(frame)[0]
- avg = (avg + mean) / 2
- if mean < avg:
- continue
-
- cv2.imshow("frame", frame)
-
- key = cv2.waitKey(1) & 0xFF
- if key == ord('q'):
- break
- elif key == ord('\r'):
- cv2.imshow("frame", frame)
- cv2.waitKey(1)
- draw_face_rec("frame", frame)
- while True:
- key = cv2.waitKey(0) & 0xFF
- if key == ord('\r'):
- cv2.imwrite(path, frame)
- return
- elif key == 27: # esc
- break
-
- if len(sys.argv) != 2:
- print(f"Usage: {sys.argv[0]} <path>")
- record(sys.argv[1], 2)
|