def __init__(self, video_src = 0, interactive = True, video = 'vss.avi', fallback = 'synth:bg=./data/hi.jpg:noise=0.05', save_frames = False, frame_dir = './data', frame_prefix='frame-'):
cam = create_capture(video_src, fallback=fallback)
vwriter = None
if interactive:
print("q to quit")
window = 'video square search'
cv2.namedWindow(window, cv2.WINDOW_NORMAL)
else:
vwriter = VideoWriter(video)
run = True
t = clock()
frameCounter = 0
while run:
ret, img = cam.read()
if interactive:
print("read next image")
squares = find_squares(img, 0.2)
nOfSquares = len(squares)
cv2.drawContours( img, squares, -1, (0, 255, 0), 3 )
dt = clock() - t
draw_str(img, (80, 80), 'time: %.1f ms found = %d' % (dt*1000, nOfSquares), 2.0)
if interactive:
cv2.imshow(window, img)
print('q to quit, n for next')
ch = 0xff & cv2.waitKey(100)
if ch == ord('q'):
run = False
elif ch == ord('n'):
continue
else:
vwriter.addFrame(img)
if save_frames:
fn = os.path.join(frame_dir, '%s-%04d.%s' % (frame_prefix, frameCounter, 'jpg'))
print("Saving %d frame to %s" % (frameCounter, fn))
cv2.imwrite(fn, img)
frameCounter+=1
if vwriter:
vwriter.finalise()
评论列表
文章目录