简体   繁体   中英

How can I improve the FPS and decrease the lag I'm getting while using a Raspberry Pi to look for circles?

Got a little question: At the moment I'm working on detecting circles with my raspberry pi with a webcam. There are plenty of topics on detecting circles in python with HoughCircles, and I got that working. It's a little bit too slow though: It gives me a huge lag of a couple of seconds. In these seconds, it keeps printing "no circles detected" even though I am sure I'm keeping my circle in front of the webcam. A couple of seconds later, it prints the location of the center.

My code is pasted below, any help is appreciated!

import cv2
import numpy as np
import time

camera_port = 0
ramp_frames = 20 #throw away to make it adjust to light levels

camera = cv2.VideoCapture(camera_port) #define camera class
retval = camera.set(3,320) #width of frame
retval = camera.set(4,240) #height of frame

def get_image(): #function to get 1 image
 retval, im = camera.read()
 return im

for i in xrange(ramp_frames): #adjust to light levels, throw away 20 frames
 temp = get_image()

while True:
    time.sleep(1)
    camera_capture = get_image() # Take the actual image we want to keep

    #processing
    img = camera_capture 
    cimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    circles = cv2.HoughCircles(cimg,cv2.cv.CV_HOUGH_GRADIENT,1,1,param1=200,param2=80,minRadius=0,maxRadius=0)
    if circles is not None:
        x=circles[0][0][0]
        y=circles[0][0][1]
        print "x= ",x,"y= ",y
    else:
        print "no circles detected"

Without knowing anything about the things you want to do, i do know that

if circles is not None:

is very much unwanted!

see: "Python Boolean Operations"

try:

if circles:

I don't think it will fix your problem, but at least your problem will look more pythonic! ;)

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM