树莓派opencv实时人脸识别2

from picamera.array import PiRGBArray
from picamera import PiCamera
from functools import partial
 
import multiprocessing as mp
import cv2
import os
import time
 
 
### Setup #####################################################################
 
os.putenv( 'SDL_FBDEV', '/dev/fb0' )
 
resX = 320
resY = 240
 
# Setup the camera
camera = PiCamera()
camera.resolution = ( resX, resY )
camera.framerate = 90
 
t_start = time.time()
fps = 0
 
# Use this as our output
rawCapture = PiRGBArray( camera, size=( resX, resY ) )
 
# The face cascade file to be used
face_cascade = cv2.CascadeClassifier( '/home/pi/Downloads/opencv/data/haarcascades/haarcascade_frontalface_default.xml' )
 
 
### Helper Functions ##########################################################
 
def get_faces( img ):
 
    gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY )
    return face_cascade.detectMultiScale( gray ), img
 
def draw_frame( img, faces ):
 
    global fps
    global time_t
 
    # Draw a rectangle around every face
    for ( x, y, w, h ) in faces:
        cv2.rectangle( img, ( x, y ),( x + w, y + h ), ( 200, 255, 0 ), 2 )
 
    # Calculate and show the FPS
    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 ) 
    #img = cv2.flip(img, 1)
    cv2.imshow( "Frame", img )
    cv2.waitKey( 1 )
 
 
### Main ######################################################################
 
if __name__ == '__main__':
 
    pool = mp.Pool( processes=4 )
 
    i = 0
    rList = [None] * 17
    fList = [None] * 17
    iList = [None] * 17
 
    camera.capture( rawCapture, format="bgr" )  
 
    for x in range ( 17 ):
        rList[x] = pool.apply_async( get_faces, [ rawCapture.array ] )
        fList[x], iList[x] = rList[x].get()
        fList[x] = []
 
    rawCapture.truncate( 0 )    
 
    for frame in camera.capture_continuous( rawCapture, format="bgr", use_video_port=True ):
        image = frame.array
 
        if   i == 1:
            rList[1] = pool.apply_async( get_faces, [ image ] )
            draw_frame( iList[2], fList[1] )
 
        elif i == 2:
            iList[2] = image
            draw_frame( iList[3], fList[1] )
 
        elif i == 3:
            iList[3] = image
            draw_frame( iList[4], fList[1] )
 
        elif i == 4:
            iList[4] = image
            fList[5], iList[5] = rList[5].get()
            draw_frame( iList[5], fList[5] )
 
        elif i == 5:
            rList[5] = pool.apply_async( get_faces, [ image ] )
            draw_frame( iList[6], fList[5] )
 
        elif i == 6:
            iList[6] = image
            draw_frame( iList[7], fList[5] )
 
        elif i == 7:
            iList[7] = image
            draw_frame( iList[8], fList[5] )
 
        elif i == 8:
            iList[8] = image
            fList[9], iList[9] = rList[9].get()
            draw_frame( iList[9], fList[9] )
 
        elif i == 9:
            rList[9] = pool.apply_async( get_faces, [ image ] )
            draw_frame( iList[10], fList[9] )
 
        elif i == 10:
            iList[10] = image
            draw_frame( iList[11], fList[9] )
 
        elif i == 11:
            iList[11] = image
            draw_frame( iList[12], fList[9] )
 
        elif i == 12:
            iList[12] = image
            fList[13], iList[13] = rList[13].get()
            draw_frame( iList[13], fList[13] )
 
        elif i == 13:
            rList[13] = pool.apply_async( get_faces, [ image ] )
            draw_frame( iList[14], fList[13] )
 
        elif i == 14:
            iList[14] = image
            draw_frame( iList[15], fList[13] )
 
        elif i == 15:
            iList[15] = image
            draw_frame( iList[16], fList[13] )
 
        elif i == 16:
            iList[16] = image
            fList[1], iList[1] = rList[1].get()
            draw_frame( iList[1], fList[1] )
 
            i = 0
 
        i += 1
 
        rawCapture.truncate( 0 )

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值