face_detection.py
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import os
cx = 160
cy = 120
os.system( "echo 0=150 > /dev/servoblaster" )
os.system( "echo 1=150 > /dev/servoblaster" )
xdeg = 150
ydeg = 150
camera = PiCamera()
camera.resolution = ( 320, 240 )
camera.framerate = 60
rawCapture = PiRGBArray( camera, size=( 320, 240 ) )
face_cascade = cv2.CascadeClassifier( '/home/pi/opencv-3.4.1/data/haarcascades_cuda/haarcascade_frontalface_alt.xml' )
t_start = time.time()
fps = 0
for frame in camera.capture_continuous( rawCapture, format="bgr", use_video_port=True ):
image = frame.array
gray = cv2.cvtColor( image, cv2.COLOR_BGR2GRAY )
faces = face_cascade.detectMultiScale( gray )
print "Found " + str( len( faces ) ) + " face(s)"
for ( x, y, w, h ) in faces:
cv2.rectangle( image, ( x, y ), ( x + w, y + h ), ( 100, 255, 100 ), 2 )
cv2.putText( image, "Face No." + str( len( faces ) ), ( x, y ), cv2.FONT_HERSHEY_SIMPLEX, 0.5, ( 0, 0, 255 ), 2 )
tx = x + w/2
ty = y + h/2
if ( cx - tx > 10 and xdeg <= 190 ):
xdeg += 3
os.system( "echo 0=" + str( xdeg ) + " > /dev/servoblaster" )
elif ( cx - tx < -10 and xdeg >= 110 ):
xdeg -= 3
os.system( "echo 0=" + str( xdeg ) + " > /dev/servoblaster" )
if ( cy - ty > 10 and ydeg >= 110 ):
ydeg -= 3
os.system( "echo 1=" + str( ydeg ) + " > /dev/servoblaster" )
elif ( cy - ty < -10 and ydeg <= 190 ):
ydeg += 3
os.system( "echo 1=" + str( ydeg ) + " > /dev/servoblaster" )
fps = fps + 1
sfps = fps / ( time.time() - t_start )
cv2.putText( image, "FPS : " + str( int( sfps ) ), ( 10, 10 ), cv2.FONT_HERSHEY_SIMPLEX, 0.5, ( 0, 0, 255 ), 2 )
cv2.imshow( "Frame", image )
cv2.waitKey( 1 )
rawCapture.truncate( 0 )
face_detection2.py
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import os
import pygame
os.putenv( 'SDL_FBDEV', '/dev/fb1' )
camera = PiCamera()
camera.resolution = ( 320, 240 )
camera.framerate = 30
rawCapture = PiRGBArray( camera, size=( 320, 240 ) )
fcounter = 0
facefind = 0
face_cascade = cv2.CascadeClassifier( '/home/pi/opencv-3.4.1/data/haarcascades/haarcascade_frontalface_alt2.xml' )
t_start = time.time()
fps = 0
for frame in camera.capture_continuous( rawCapture, format="bgr", use_video_port=True ):
image = frame.array
if fcounter == 3:
fcounter = 0
gray = cv2.cvtColor( image, cv2.COLOR_BGR2GRAY )
faces = face_cascade.detectMultiScale( gray )
print "Found " + str( len( faces ) ) + " face(s)"
if str( len( faces ) ) != 0:
facefind = 1
facess = faces
else:
facefind = 0
for ( x, y, w, h ) in faces:
cv2.rectangle( image, ( x, y ), ( x + w, y + h ), ( 200, 255, 0 ), 2 )
cv2.putText( image, "Face No." + str( len( facess ) ), ( x, y ), cv2.FONT_HERSHEY_SIMPLEX, 0.5, ( 0, 0, 255 ), 2 )
facess = faces
else:
if facefind == 1 and str( len( facess ) ) != 0:
for ( x, y, w, h ) in facess:
cv2.rectangle( image, ( x, y ), ( x + w, y + h ), ( 200, 255, 0 ), 2 )
cv2.putText( image, "Face No." + str( len( facess ) ), ( x, y ), cv2.FONT_HERSHEY_SIMPLEX, 0.5, ( 0, 0, 255 ), 2 )
fcounter += 1
fps = fps + 1
sfps = fps / ( time.time() - t_start )
cv2.putText( image, "FPS : " + str( int( sfps ) ), ( 10, 10 ), cv2.FONT_HERSHEY_SIMPLEX, 0.5, ( 0, 0, 255 ), 2 )
cv2.imshow( "Frame", image )
cv2.waitKey( 1 )
rawCapture.truncate( 0 )
face_detection3.py
from picamera.array import PiRGBArray
from picamera import PiCamera
from functools import partial
import multiprocessing as mp
import cv2
import os
import time
os.putenv( 'SDL_FBDEV', '/dev/fb0' )
resX = 320
resY = 240
cx = resX / 2
cy = resY / 2
os.system( "echo 0=150 > /dev/servoblaster" )
os.system( "echo 1=150 > /dev/servoblaster" )
xdeg = 150
ydeg = 150
camera = PiCamera()
camera.resolution = ( resX, resY )
camera.framerate = 60
rawCapture = PiRGBArray( camera, size=( resX, resY ) )
face_cascade = cv2.CascadeClassifier('/home/pi/opencv-3.4.1/data/haarcascades/haarcascade_frontalface_alt2.xml')
t_start = time.time()
fps = 0
def get_faces( img ):
gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY )
faces = face_cascade.detectMultiScale( gray )
return faces, img
def draw_frame( img, faces ):
global xdeg
global ydeg
global fps
global time_t
for ( x, y, w, h ) in faces:
cv2.rectangle( img, ( x, y ),( x + w, y + h ), ( 200, 255, 0 ), 2 )
cv2.putText(img, "Face No." + str( len( faces ) ), ( x, y ), cv2.FONT_HERSHEY_SIMPLEX, 0.5, ( 0, 0, 255 ), 2 )
tx = x + w/2
ty = y + h/2
if ( cx - tx > 15 and xdeg <= 190 ): xdeg += 1 os.system( "echo 0=" + str( xdeg ) + " > /dev/servoblaster" )
elif ( cx - tx < -15 and xdeg >= 110 ):
xdeg -= 1
os.system( "echo 0=" + str( xdeg ) + " > /dev/servoblaster" )
if ( cy - ty > 15 and ydeg >= 110 ):
ydeg -= 1
os.system( "echo 1=" + str( ydeg ) + " > /dev/servoblaster" )
elif ( cy - ty < -15 and ydeg <= 190 ): ydeg += 1 os.system( "echo 1=" + str( ydeg ) + " > /dev/servoblaster" )
fps = fps + 1
sfps = fps / (time.time() - t_start)
cv2.putText(img, "FPS : " + str( int( sfps ) ), ( 10, 10 ), cv2.FONT_HERSHEY_SIMPLEX, 0.5, ( 0, 0, 255 ), 2 )
cv2.imshow( "Frame", img )
cv2.waitKey( 1 )
if __name__ == '__main__':
pool = mp.Pool( processes=4 )
fcount = 0
camera.capture( rawCapture, format="bgr" )
r1 = pool.apply_async( get_faces, [ rawCapture.array ] )
r2 = pool.apply_async( get_faces, [ rawCapture.array ] )
r3 = pool.apply_async( get_faces, [ rawCapture.array ] )
r4 = pool.apply_async( get_faces, [ rawCapture.array ] )
f1, i1 = r1.get()
f2, i2 = r2.get()
f3, i3 = r3.get()
f4, i4 = r4.get()
rawCapture.truncate( 0 )
for frame in camera.capture_continuous( rawCapture, format="bgr", use_video_port=True ):
image = frame.array
if fcount == 1:
r1 = pool.apply_async( get_faces, [ image ] )
f2, i2 = r2.get()
draw_frame( i2, f2 )
elif fcount == 2:
r2 = pool.apply_async( get_faces, [ image ] )
f3, i3 = r3.get()
draw_frame( i3, f3 )
elif fcount == 3:
r3 = pool.apply_async( get_faces, [ image ] )
f4, i4 = r4.get()
draw_frame( i4, f4 )
elif fcount == 4:
r4 = pool.apply_async( get_faces, [ image ] )
f1, i1 = r1.get()
draw_frame( i1, f1 )
fcount = 0
fcount += 1
rawCapture.truncate( 0 )