V
V
Vladimir2015-05-09 01:17:10
Python
Vladimir, 2015-05-09 01:17:10

How to speed up image analysis on raspberry pi2 + python + opencv?

There is a task, you need to analyze the color uniformity in the picture from the camera (the picture is bw, there is a reference brightness and permissible deviations up / down, in the test example the resolution is 640 x 480)
In the code example below, I process only the first frame in order to remove the delay receiving a frame from the camera, I can only test on a USB webcam, and there FPS is from 6 to 20 frames per second

import numpy as np
import cv2
import time
import serial

ttyDev = '/dev/ttyAMA0'
#ttyDev = 0
ttyBourRate = 115200
refBW = 100
limitLight = 10
limitDark = 90

ser = serial.Serial(ttyDev, ttyBourRate, timeout=0)  # open first serial port

font = cv2.FONT_ITALIC
cap = cv2.VideoCapture(0)
firstFrame = 1
while(True):
    badPixels = 0
    if firstFrame == 1:
      ret, frame = cap.read() 
      #height, width = frame.shape[:2]
      #frame = cv2.resize(frame,(width/2, height/2), interpolation = cv2.INTER_CUBIC)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    pixelsArr = np.array(gray)
    if firstFrame == 1:
        totalPixels = pixelsArr.size

    darkPixels = pixelsArr < refBW-limitDark
    ligthPixels = pixelsArr > refBW+limitLight 
    badPixels = np.sum(darkPixels+ligthPixels)
    percentageBad = int(round(100*badPixels/totalPixels))

    time2 = int(round(time.time() * 1000))
    if firstFrame != 1:
        delta = time2-time1
        #cv2.putText(frame, 'FPS: '+str(round(1000.0*1.0/delta,3)), (20, 40), font, 1, (255, 0, 0), 2)
  print str(round(1000.0*1.0/delta,3))
    #cv2.putText(frame, 'Bad: '+str(percentageBad), (20, 80), font, 1, (255, 0, 0), 2)
    ser.write([percentageBad])

    # Display the resulting frame
    #cv2.imshow('test',frame)
    time1 = int(round(time.time() * 1000))
    firstFrame = 0
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break

# When everything done, release the capture
ser.close() 
cap.release()
cv2.destroyAllWindows()

So, a banal array with pixels that do not fit into the brightness limit gives FPS: 12-18
Maybe my calculation algorithm is too clumsy?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question