def main(argv): if len(sys.argv) != 2: print "Usage: main.py <file name>" sys.exit() cap = cv2.VideoCapture(sys.argv[1]) if not cap.isOpened(): print "Error when reading image file" log = logWrite.logWrite() log.new(str(sys.argv[1])) while(cap.isOpened()): ROIs.clear() ret, frame = cap.read() if int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT): break frame = cv2.resize(frame, (0,0), fx=constant.RESCALE_FACTOR, fy=constant.RESCALE_FACTOR, interpolation=cv2.INTER_NEAREST) cv2.putText(frame, str(int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES))), (25, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0)) cv2.namedWindow('frame') cv2.cv.SetMouseCallback('frame', on_mouse, frame) cv2.imshow('frame',frame) k=-1 while k not in (chr(27), 'u', 'n', 's', 'w', 's', 'a', 'd'): k = chr(cv2.waitKey(0) & 255) print k + " was pressed" if k==chr(27): # Esc key to stop break elif k=='u': ROIs.clear() frame_metadata[int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES))] = "UNCERTAIN" print "UNCERTAIN" elif k=='n': ROIs.clear() frame_metadata[int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES))] = "EMPTY" print "EMPTY" elif k=='w': cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) - constant.FRAME_SKIP - 1) elif k=='s': cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) + constant.FRAME_SKIP - 1) elif k=='a': cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) - 2) elif k=='d': pass if len(ROIs) > 0: frame_metadata[int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES))] = list(ROIs) log.write(collections.OrderedDict(sorted(frame_metadata.items(), key=lambda t: t[0]))) log.close() cap.release() cv2.destroyAllWindows()
def main(argv): global brush_size global selected_area global highlighted_area global max_frame_num global current_frame_num if len(sys.argv) != 2: print "Usage: main.py <frame location>" sys.exit() frame_location = sys.argv[1] frames = get_frames(frame_location) max_frame_num = len(frames) - 1 if len(frames) == 0: print "Error: failed to read frames from ", frame_location sys.exit() log = logWrite.logWrite() log.new(str(sys.argv[1])) current_frame_num = 1 while True: ROIs.clear() frame = frames[current_frame_num] try: height, width, channels = frame.shape except: print current_frame_num highlighted_area = np.zeros((height, width, 3), np.uint8) selected_area = np.zeros((height, width, 3), np.uint8) try: load_ROIs(frame_metadata[current_frame_num]) except Exception as e: # print "Warning: Failure to load ROIs that were previously chosen for this frame." # print e pass cv2.putText(frame, str(current_frame_num), (6, 18), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0)) cv2.namedWindow('frame') cv2.cv.SetMouseCallback('frame', on_mouse, frame) frame = cv2.addWeighted(frame, 0.7, selected_area, 0.3, 0) frame = cv2.resize(frame, (0,0), fx=constant.RESCALE_FACTOR, fy=constant.RESCALE_FACTOR, interpolation=cv2.INTER_NEAREST) cv2.imshow('frame',frame) k = 0 while k not in (chr(27), 'u', 'n', 's', 'w', 's', 'a', 'd', 'q'): k = chr(cv2.waitKey(0) & 255) try: brush_size = int(k) except: print k + " was pressed" if len(ROIs) > 0: frame_metadata[current_frame_num] = tuple(ROIs) if k==chr(27) or k=='q': # Esc key or 'q' to stop break elif k=='u': ROIs.clear() frame_metadata[current_frame_num] = "UNCERTAIN" print "UNCERTAIN" current_frame_num = get_new_frame_num(1) elif k=='n': ROIs.clear() frame_metadata[current_frame_num] = "EMPTY" print "EMPTY" current_frame_num = get_new_frame_num(1) elif k=='w': current_frame_num = get_new_frame_num(-constant.FRAME_SKIP) elif k=='s': current_frame_num = get_new_frame_num(constant.FRAME_SKIP) elif k=='a': current_frame_num = get_new_frame_num(-1) elif k=='d': current_frame_num = get_new_frame_num(1) log.write(collections.OrderedDict(sorted(frame_metadata.items(), key=lambda t: t[0]))) log.close() cv2.destroyAllWindows()
def main(argv): if len(sys.argv) != 2: print "Usage: main.py <file name>" sys.exit() cap = cv2.VideoCapture(sys.argv[1]) if not cap.isOpened(): print "Error when reading image file" log = logWrite.logWrite() log.new(str(sys.argv[1])) while (cap.isOpened()): ROIs.clear() ret, frame = cap.read() if int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) == cap.get( cv2.cv.CV_CAP_PROP_FRAME_COUNT): break frame = cv2.resize(frame, (0, 0), fx=constant.RESCALE_FACTOR, fy=constant.RESCALE_FACTOR, interpolation=cv2.INTER_NEAREST) cv2.putText(frame, str(int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES))), (25, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0)) cv2.namedWindow('frame') cv2.cv.SetMouseCallback('frame', on_mouse, frame) cv2.imshow('frame', frame) k = -1 while k not in (chr(27), 'u', 'n', 's', 'w', 's', 'a', 'd'): k = chr(cv2.waitKey(0) & 255) print k + " was pressed" if k == chr(27): # Esc key to stop break elif k == 'u': ROIs.clear() frame_metadata[int(cap.get( cv2.cv.CV_CAP_PROP_POS_FRAMES))] = "UNCERTAIN" print "UNCERTAIN" elif k == 'n': ROIs.clear() frame_metadata[int(cap.get( cv2.cv.CV_CAP_PROP_POS_FRAMES))] = "EMPTY" print "EMPTY" elif k == 'w': cap.set( cv2.cv.CV_CAP_PROP_POS_FRAMES, int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) - constant.FRAME_SKIP - 1) elif k == 's': cap.set( cv2.cv.CV_CAP_PROP_POS_FRAMES, int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) + constant.FRAME_SKIP - 1) elif k == 'a': cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) - 2) elif k == 'd': pass if len(ROIs) > 0: frame_metadata[int(cap.get( cv2.cv.CV_CAP_PROP_POS_FRAMES))] = list(ROIs) log.write( collections.OrderedDict( sorted(frame_metadata.items(), key=lambda t: t[0]))) log.close() cap.release() cv2.destroyAllWindows()