Keval Doshi
Published © GPL3+

Face Detection using Raspberry Pi and Smartphone

Face Detection is really awesome! This is a tutorial about making a very cheap face detection system.

IntermediateFull instructions provided2 hours13,394
Face Detection using Raspberry Pi and Smartphone

Things used in this project

Story

Read more

Code

Code

Python
import cv2 
import sys 
import logging as log 
import datetime as dt 
from time import sleep 
cascPath = sys.argv[1] 
faceCascade = cv2.CascadeClassifier(cascPath) 
log.basicConfig(filename='webcam.log',level=log.INFO) 
video_capture = cv2.VideoCapture(0) 
anterior = 0 
print('Keval') 
while True: 
   if not video_capture.isOpened(): 
       print('Unable to load camera.') 
       sleep(5) 
       pass 
   # Capture frame-by-frame 
   ret, frame = video_capture.read() 
   cv2.rectangle(frame, (20, 20), (20, 20), (255, 0, 0), 2) 
   gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
   faces = faceCascade.detectMultiScale( 
       gray, 
       scaleFactor=1.1, 
       minNeighbors=5, 
       minSize=(30, 30) 
   ) 
   # Draw a rectangle around the faces 
   for (x, y, w, h) in faces: 
       cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) 
   if anterior != len(faces): 
       anterior = len(faces) 
       log.info("faces: "+str(len(faces))+" at "+str(dt.datetime.now())) 
   if cv2.waitKey(1) & 0xFF == ord('q'): 
       break 
   # Display the resulting frame 
   cv2.imshow('Video', frame) 
# When everything is done, release the capture 
video_capture.release() 
cv2.destroyAllWindows() 

Credits

Keval Doshi

Keval Doshi

1 project • 28 followers
Hardware Hacker. Love going to hackathons. Enough said!

Comments