树莓派3B实现人脸图像采集与识别

描述:

树莓派3B+平台搭载了Linux嵌入式系统,在Linux(Ubuntu 16.04)上安装python、OpenCV和tensorflow便可以组成一台mini主机用于数据处理。其硬件系统介绍如下:

  • 博通BCM2837B0 SoC,集成四核ARM Cortex-A53(ARMv8)64位@ 1.4GHz CPU,集成博通 Videocore-IV GPU
  • 内存:1GB LPDDR2 SDRAM
  • 有线网络:千兆以太网(通过USB2.0通道,最大吞吐量 300Mbps)
  • 无线网络:2.4GHz和5GHz 双频Wi-Fi,支持802.11b/g/n/ac
  • 蓝牙:蓝牙4.2&低功耗蓝牙(BLE)
  • 存储:Micro-SD
  • 其他接口:HDMI,3.5mm模拟音频视频插孔,4x USB 2.0,以太网,摄像机串行接口(CSI),显示器串行接口(DSI),MicroSD卡座,40pin扩展双排插针
  • 尺寸:82mmx 56mmx 19.5mm,50克

 


pivideostream.py - 采集数据

from picamera.array import PiRGBArray 
from picamera import PiCamera 
from threading import Thread 
import cv2 

class PiVideoStream: 

    def __init__(self, resolution=(640, 480), framerate=30): 
     self.camera = PiCamera() 
     self.camera.resolution = resolution 
     self.camera.framerate = framerate 
     self.rawCapture = PiRGBArray(self.camera, size=resolution) 
     self.stream = self.camera.capture_continuous(self.rawCapture,format='bgr', use_video_port=True) 
     self.image = None 
     self.stopped = False 

    def start(self): 
     t = Thread(target=self.update) 
     t.daemon = True 
     t.start() 
     return self 

    def update(self): 
     for frame in self.stream: 
      self.image = frame.array 
      self.rawCapture.truncate(0) 

      if self.stopped: 
       self.stream.close() 
       self.rawCapture.close() 
       self.camera.close() 
       return 

    def read(self): 
     return self.image 

    def stop(self): 
     self.stopped = True 

process_img_thread.py - 主程序

from pivideostream import PiVideoStream 
import cv2 
from picamera.array import PiRGBArray 
from picamera import PiCamera 
import time 


def detect_in_thread(): 
    # Start updating frames in threaded manner 
    face_cascade = cv2.CascadeClassifier('./haarcascades/haarcascade_frontalface_default.xml') 
    eye_cascade = cv2.CascadeClassifier('./haarcascades/haarcascade_eye.xml') 
    thread_stream = PiVideoStream() 
    thread_stream.start() 
    time.sleep(2) 

    # Read frames 
    while True: 

     # Original image 
     image = thread_stream.read() 

     # Full image - face detection 
     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 
     faces = face_cascade.detectMultiScale(gray,1.3,5) 
     for (x,y,w,h) in faces: 
      detected_face = cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2) 

      # Region of interest - eyes detection 
      roi_color = image[y:y+h,x:x+w] 
      roi_gray = gray[y:y+h,x:x+w] 
      eyes = eye_cascade.detectMultiScale(roi_gray,1.03,5,0,(40,40)) 
      for (ex,ey,ew,eh) in eyes: 
       cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,255),2) 

     # Show computed image 
     cv2.imshow('Threaded Camera OpenCV Preview',image) 

     if cv2.waitKey(1) & 0xFF == ord("q"): 
      break 

    # Close image window and thread 
    cv2.destroyAllWindows() 
    thread_stream.stop() 


if __name__ == "__main__": 
    detect_in_thread() 

问题解决:

I am having problems with Python thread OpenCV (not sure if this is possible), but the threading PiCamera catches the frame well.

I tried the following except the above:

  1. Use the "YUV" color space instead of "RGB" and simply access the first (Y - lumminance) channel to get grayscale data, wasting less time fetching frames and converting from RGB to grayscale
  2. Try to use LBP cascade instead of HAAR (for example ) - should be faster, but less accurate/usr/local/share/OpenCV/lbpcascades/lbpcascade_frontalface.xml

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Techblog of HaoWANG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值