55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
from picamera2 import Picamera2
|
|
from gpiozero import LED
|
|
import cv2
|
|
import os
|
|
|
|
|
|
camera = Picamera2()
|
|
|
|
led = LED(17)
|
|
off = True
|
|
on_time = 100
|
|
time_left = 0
|
|
|
|
preview_config = camera.create_preview_configuration(main={'size': (640, 480)})
|
|
|
|
camera.configure(preview_config)
|
|
camera.start()
|
|
|
|
if not os.path.exists("haarcascade_frontalface_default.xml"):
|
|
import urllib.request
|
|
url = "https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml"
|
|
urllib.request.urlretrieve(url, "haarcascade_frontalface_default.xml")
|
|
|
|
|
|
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
|
|
|
while(1):
|
|
frame = camera.capture_array()
|
|
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
|
|
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
|
for (x, y, w, h) in faces:
|
|
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
|
|
|
|
try:
|
|
if faces[0][0] > 0 :
|
|
off = False
|
|
time_left = on_time
|
|
except:
|
|
off = True
|
|
|
|
if not off or time_left > 0:
|
|
led.on()
|
|
time_left = time_left - 1
|
|
else:
|
|
led.off()
|
|
time_left = 0
|
|
|
|
print(time_left)
|
|
|
|
cv2.imshow("Real-Time Face Detection", frame)
|
|
if cv2.waitKey(1) & 0xFF == ord('q'):
|
|
break
|
|
|
|
cv2.destroyAllWindows() |