2

Currently, my system is only producing 10 FPS out of 20, which is my target. I'm barely hit 15 FPS in single grab-and-show simple code like this:

from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import numpy as np

def clock():
    return cv2.getTickCount() / cv2.getTickFrequency()
def draw_str(dst,x,y, s):
    cv2.putText(dst, s, (x+1, y+1), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), thickness=2,lineType=cv2.LINE_AA)
    cv2.putText(dst, s, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (255, 255, 255), lineType=cv2.LINE_AA)
#init PiCamera
resch=[(640,480),(320,240)]
res=resch[0]
camera = PiCamera()
camera.resolution = res
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=res)
time.sleep(0.1)
millis=0

for image in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

    frame = image.array
    deltatime=clock() - millis
    fps=1/deltatime
    draw_str(frame, 20, 20, 'time: %.1f ms' % (deltatime * 1000))
    draw_str(frame, 20, 460, 'fps: %.1f' % (fps))
    rawCapture.truncate(0)
    cv2.imshow('frame',frame)
    print (int(fps))
    millis=clock()
    k = cv2.waitKey(1) & 0xFF
    if k == 27:

i thought it was the 'I/O' bottleneck stuff, so i went to Multiprocessing code which is basically polling the frame whenever it is available without waiting for each time camera capture frame to complete (that is what i'm currently understand).

but there were no significant different on FPS counter or i must say, only 2 FPS increase.

This is my Multiprocessing code:

import os
from multiprocessing import Process, Pipe
from time import sleep
import cv2
import numpy as np
from picamera.array import PiRGBArray
from picamera import PiCamera


def clock():
    return cv2.getTickCount() / cv2.getTickFrequency()
def draw_str(dst,x,y, s):
    cv2.putText(dst, s, (x+1, y+1), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), thickness=2,lineType=cv2.LINE_AA)
    cv2.putText(dst, s, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (255, 255, 255), lineType=cv2.LINE_AA)


def tangki(keran1):
    a=None
    while (1):
        millis=clock()
        frame=keran1.recv()
        cv2.flip(frame,1, frame)
        deltatime=clock() - millis
        fps=1/deltatime
        if fps>=20:
            print (fps)
        draw_str(frame, 20, 20, 'time: %.1f ms' % (deltatime * 1000))
        draw_str(frame, 20, 460, 'fps: %.1f' % (fps))
        cv2.imshow('frame',frame)
        k = cv2.waitKey(1) & 0xFF
        if k == 27:
            break
    cv2.destroyAllWindows()

def tangki2(keran3):
    a2=None
    while (1):
        millis2=clock()
        frame2=keran3.recv()
        cv2.flip(frame2,1, frame2)
        deltatime=clock() - millis2
        fps=1/deltatime 
        draw_str(frame2, 20, 20, 'time: %.1f ms' % (deltatime * 1000))
        draw_str(frame2, 20, 460, 'fps: %.1f' % (fps))
        cv2.imshow('frame',frame2)
        k = cv2.waitKey(1) & 0xFF
        if k == 27:
            break
    cv2.destroyAllWindows()

def pompa(gate1,gate2):                                     
    resch=[(640,480),(320,240)]
    res=resch[0]
    camera = PiCamera()
    camera.resolution = res
    camera.framerate = 32
    rawCapture = PiRGBArray(camera, size=res)
    sleep(0.1)
    for image in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
        frame = image.array
        gate1.send(frame)
        gate2.send(frame)                               
        rawCapture.truncate(0)
        k = cv2.waitKey(1) & 0xFF
        if k == 27:
            break
    camera.close()
    cv2.destroyAllWindows()

if __name__ == '__main__':

    pipa1, pipa2 = Pipe()
    pipa3, pipa4 = Pipe()                           

    ptangki = Process(target=tangki, args=(pipa2, ))
    ptangki2 = Process (target=tangki2, args=(pipa4,))  

    ppompa = Process(target=pompa, args=(pipa1,pipa3))      

    ptangki.start()
    ptangki2.start()                                    
    ppompa.start()

    ptangki.join()
    ptangki2.join()                                     
    ppompa.join()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.