2017-02-01 170 views
0

最近我在OpenCV的加速圖像處理上我的樹莓派3平臺上運行的掙扎。我做了臉部識別應用程序,但它運行速度非常緩慢。我讀線程,多處理等諸多課題,但我仍然感到困惑了。我只是用面部檢測來測試它,以使其更簡單。這裏是我的代碼:實時圖像處理與OpenCV的和樹莓派

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() 

當我顯示來自相機它的工作原始幀偉大的,但是當我僅僅是爲了處理圖像添加主程序的東西,視頻速度大約是1 FPS :(。 有人能幫助我嗎?

+1

臉部檢測是一種昂貴的任務和覆盆子pi是一個緩慢的decice 。也許試試TK1 SoC,但不能保證足夠快。 – Micka

+1

如果你知道面的近似大小在您的圖像,你可以調整圖像或限制檢測尺寸。 – Micka

回答

0

Micka的意見都非常好。

我有問題,可以用Python線程OpenCV的(不知道這是可能的),但在穿線PiCamera抓幀做得很好。

我除了上述嘗試以下操作:

  1. 使用「YUV」色彩空間,而不是「RGB」,並簡單地訪問第一(Y - lumminance)通道來獲得灰度數據,浪費更少的時間取幀和從RGB轉換爲灰度
  2. 嘗試使用LBP級聯代替HAAR(例如/usr/local/share/OpenCV/lbpcascades/lbpcascade_frontalface.xml) - 應快一點,但較不準確