python - OpenCV VideoCapture only updates after 5 read()s -
i have unusual error has been plaguing research few years now. i'm using opencv2 python read image info webcam. however, image lagged 5 frames. in other words, each phone call read() 5 frame behind real-time.
one bandage prepare have been using grab() 4 frames , read 5th every time need updated image, absolutely murders performance.
here's code using display images webcam
frame = self.getgoodframe() if self.debug: window = cv2.namedwindow("angles") imghsv = cv2.cvtcolor(frame, cv2.cv.cv_bgr2hsv) ... reseach specific code shouldn't giving out here ... ... finds center of few bright colors in image if self.debug: debugimage = numpy.zeros((self.camera_height, self.camera_width), numpy.uint8) #blank image ... draw stuff image displayed ... cv2.imshow("angles", debugimage) cv2.waitkey(1) raw_input()
and getgoodframe()
def getgoodframe(self): min_reads_for_good_frame = 4 in xrange(min_reads_for_good_frame): successful_read = self.capture.grab() successful_read, frame = self.capture.read() if not successful_read: print "unable read webcam, exiting." homecoming frame
you'll notice have raw_input() call. makes can see how many reads need happen pressing come in few times in console. shows there 5 frames of lag.
i don't think it's hardware issue, i've had happen multiple webcams , multiple usb cords. haven't tried reproducing error on machine though.
so problem way hardware buffering frames. never quite got bottom of solution found simple paralellization. modified code open thread , update variable holding current frame. then, when need current frame inquire whatever variable @ moment.
note is still 5 read() calls behind because of ever-present buffering, because doing read() calls continuously , it's on own thread, date. because can phone call many read() calls per second. image not noticeably behind real-time @ all.
the python class made below. not nice code works. properly, (and will) add together graceful ways exit infinite loop. work me though , has improved speed of image detection code on 100 fold, extremely awesome , exciting me :)
class webcamimagegetter: def __init__(self): self.currentframe = none self.camera_width = #webcam width self.camera_height = #webcam height self.camera_num = 0 self.capture = cv2.videocapture(0) #put in right capture number here #opencv default gets half resolution image manually set right resolution self.capture.set(cv2.cv.cv_cap_prop_frame_width,self.camera_width) self.capture.set(cv2.cv.cv_cap_prop_frame_height,self.camera_height) #starts updating images in thread def start(self): thread(target=self.updateframe, args=()).start() #continually updates frame def updateframe(self): while(true): ret, self.currentframe = self.capture.read() while (self.currentframe == none): #continually grab frames until 1 ret, frame = self.capture.read() def getframe(self): homecoming self.currentframe
to utilize this, initialize called start on instance. create when later called getframe(), have date frame webcam. woohoo!
python opencv
No comments:
Post a Comment