I'm trying real-time image processing right now and i ran into troubles concerning performance issues. My script divides an 480x640 image array into a bunch of rectangles an then merges the pixels in these areas into the average pixel. i measured time consumption for the main blocks of my code:
Code: Select all
#numbers are marks for time measurement. I erased the code for readability.
#bounds is an array of rectangle coordinates. it has the following structure: [ [x1,x2,y1,y2],[x1,x2,y1,y2], ... ]
while True:
#1
# get frame from camera
ret, frame = cap.read()
#2
layer = []
#iterate over areas
for a,areaX in enumerate(areas[0]):
col = []
for b,areaY in enumerate(areas[1]):
#3
b = bounds[a][b]
area = frame[b[0]:b[1],b[2]:b[3]]
#convert to 1 dimension to simplify code
area1d = np.concatenate(area[:])
count = len(area1d)
#4
blue = np.sum(area1d[:,0])
green = np.sum(area1d[:,1])
red = np.sum(area1d[:,2])
#5
averagePixel = np.array([red,green,blue])/count
col.append(averagePixel)
#6
layer.append(col)
time consumption:
Code: Select all
#1-#2: 0.12s
#3-#4: 0.65s
#4-#5: 1.3s
#5-#6: 0.75s
all: 2.8 s
I know my code is not top level, I'm a beginner at python and stuff. So my question is if somebody can help me improving this code. I was told numpy is surprisingly fast, but 1.3s to add some numbers is not what I expected. My goal are at least 3-5 fps, I'd like 10fps.
I'm using:
raspi model b rev2 @ 800Mhz
4vl2 driver (measured about 8fps, supposed to be 30fps or something. why? i dont really changed anything in the settings)
and yeah I know python is not the fastest language. But its easy and I dont have enough time and the will to jump into C. I think its possible to do this in python.
Help is appreciated
regards,
simpa
ps: I've tried pypy, but it seems it doesn't support opencv. Any other ways to get a video frame in python?
