def main(sysargs): args = EasyArgs(sysargs) cfg = EasyConfig(args.config, group="chessboard_extract") if "help" in args: usage() return 0 # test args if len(args) <= 2: usage() return 1 # video settings, etc window_name = "Chessboard Extractor" side = Theta.resolve(args.split) rotate = BuffSplitCap.r0 crop = (0, 0, 0, 0) font = cv2.FONT_HERSHEY_SIMPLEX # image stuff image_path = os.path.join(args[2], args.prefix or 'frame_') if not os.path.exists(args[2]): os.makedirs(args[2]) cv2.namedWindow(window_name) cap = BuffSplitCap(args[1], side=side, rotate=rotate, crop=crop, buff_max=cfg.buffer_size) # loop video file while cap.isOpened(): frame = cap.frame() # display status textframe = frame.copy() displayText(textframe, cap.status(), top=True) cv2.imshow(window_name, textframe) # controls key = cv2.waitKey(0) if key == Key.right: cap.next() elif key == Key.left: cap.back() elif key == Key.enter: print "Snapped at: ", cap.at() cv2.imwrite("{}{:02d}.jpg".format(image_path, cap.at()), frame) elif key == Key.esc: print "Quitting." break # clean up cap.release() cv2.destroyAllWindows() print "\nDone." return 0
def main(sysargs): args = EasyArgs(sysargs) cfg = EasyConfig(args.config, group="chessboard_extract") if "help" in args: usage() return 0 # test args if len(args) <= 2: usage() return 1 # video settings, etc window_name = "Chessboard Extractor" side = Theta.resolve(args.split) rotate = BuffSplitCap.r0 crop = (0,0,0,0) font = cv2.FONT_HERSHEY_SIMPLEX # image stuff image_path = os.path.join(args[2], args.prefix or 'frame_') if not os.path.exists(args[2]): os.makedirs(args[2]) cv2.namedWindow(window_name) cap = BuffSplitCap(args[1], side=side, rotate=rotate, crop=crop, buff_max=cfg.buffer_size) # loop video file while cap.isOpened(): frame = cap.frame() # display status textframe = frame.copy() displayText(textframe, cap.status(), top=True) cv2.imshow(window_name, textframe) # controls key = cv2.waitKey(0) if key == Key.right: cap.next() elif key == Key.left: cap.back() elif key == Key.enter: print "Snapped at: ", cap.at() cv2.imwrite("{}{:02d}.jpg".format(image_path, cap.at()), frame) elif key == Key.esc: print "Quitting." break # clean up cap.release() cv2.destroyAllWindows() print "\nDone." return 0
def main(sysargs): args = EasyArgs(sysargs) cfg = EasyConfig(args.config, group="mapper") if "help" in args: usage() return 0 if ["calib", "trainer", "output"] not in args: print "Must specify: -calib, -trainer, -output files" usage() return 1 if len(args) == 1: print "Not enough input CSV files" usage() return 1 if len(args) > 2 and args.map_trainer_mode: print "Too many CSV for trainer-mapping mode" usage() return 1 if "force_side" in args: side = Theta.resolve(args.force_side) if side == Theta.NonDual: print "Invalid force_side argument:", args.force_side usage() return 1 # set side overrides force_button = (side == Theta.Buttonside) force_back = not force_button else: force_button = force_back = False # working vars csvs = {} frame_num = 0 # open source CSV datasets for i in range(1, len(args)): print args[i] csvs[i] = Memset(args[i]) # reel all the files up to their first flash for i in csvs: csvs[i].restrict() if len(csvs[i].row()) < 10: print "CSV file:", args[i], "contains no marker data!\nAborting." return 1 # override csv name if args.map_trainer_mode: csvs[1]._name = cfg.trainer_target # open calib files try: buttonside = Mapper(args.calib, args.trainer, cfg, Theta.Buttonside) backside = Mapper(args.calib, args.trainer, cfg, Theta.Backside) except Exception as e: print e.message return 1 count = {'bts': 0, 'bks': 0, 'rej': 0} # open destination XML with open(args.output, "w") as xmlfile: w = XMLWriter(xmlfile) w.declaration() xmlfile.write( "<!DOCTYPE dataset SYSTEM \"http://storage.gwillz.com.au/eagleeye_v2.dtd\">" ) doc = w.start("dataset") # iterates until all vicon csvs reach eof while True: w.start("frameInformation") w.element("frame", number=str(frame_num)) #iterates through each vicon csv at the current row for i in csvs: c = csvs[i] # determine marker quality try: max_reflectors = int(c.row()[8]) visible_reflectors = int(c.row()[9]) except: print "Error in reading quality at row {}".format(i) return 1 try: # read VICON data x = float(c.row()[2]) y = float(c.row()[3]) z = float(c.row()[4]) # TODO: is this necessary? We never use the object's rotation rx = float(c.row()[5]) ry = float(c.row()[6]) rz = float(c.row()[7]) except: print "Error occurred when converting VICON data at row {}".format( i) return 1 # run projection/mapping on VICON data if backside.isVisible((x, y, z)): points = backside.reprojpts((x, y, z)) side = 'backside' count['bks'] += 1 elif buttonside.isVisible((x, y, z)): points = buttonside.reprojpts((x, y, z)) points[ 0] += 960 # add 960 to x for rightside points (Ricoh video is both frames side by side) side = 'buttonside' count['bts'] += 1 # TODO don't write non visible dots? else: points = [0., 0.] count['rej'] += 1 # TODO: Change DTD and double check with Manjung w.start("object", id=str(i), name=c.name(), lens=Theta.name(side)) w.element("boxinfo", height="99", width="99", x=str(points[0] - 50), y=str(points[1] - 50)) w.element("centroid", x=str(points[0]), y=str(points[1]), rx=str(rx), ry=str(ry), rz=str(rz)) w.element("visibility", visible=str(visible_reflectors), visibleMax=str(max_reflectors)) w.end() w.end() # test end of files eofs = 0 for i in csvs: if csvs[i].eof(): eofs += 1 if len(csvs) == eofs: print "end of all datasets" break # load next vicon frame frame_num += 1 for i in csvs: csvs[i].next() w.close(doc) print "\nbuttonside", count['bts'] print "backside", count['bks'] print "rejected", count['rej'] return 0
def main(sysargs): args = EasyArgs(sysargs) cfg = EasyConfig(args.cfg, group="compare_trainer") window_name = "EagleEye Comparator" if "help" in args: usage() return 0 # grab marks from args if len(args) > 5: mark_in = args[4] mark_out = args[5] # test integer-ness try: int(mark_in) and int(mark_out) except: usage() return 1 # or grab them from a marker tool elif len(args) > 3: ret, mark_in, mark_out = marker_tool(args[1], 50, window_name) if not ret: print "Not processing - exiting." return 1 else: usage() return 1 # determine sides side = Theta.resolve(args.side or 'buttonside') # open inouts files vid = BuffSplitCap(args[1], buff_max=cfg.buffer_size, side=Theta.Both) mapper_xml = Xmlset(args[2], offset=cfg.offset, offmode=Xmlset.offset_mode(cfg.offset_mode)) trainer_xml = Xmltrainer(args[3], side=side) reprojerror_list = {} # list of reprojection error of all frames lastframe = False # reject mapper_xml if it doesn't contain the trainer_target if cfg.trainer_target not in mapper_xml.data(0): print "Mapping file must contain training target:", cfg.trainer_target print "Target names in mapping file:", mapper_xml.data(0) print "Please specifiy correct target name in config" return 1 # also reject if it's lens data doesn't match the trainer_xml _test_lens = mapper_xml.data(0)[cfg.trainer_target]['lens'] if (_test_lens == Theta.NonDual and side != Theta.NonDual) \ or (side != Theta.NonDual and _test_lens == Theta.NonDual): print "Mapping file doesn't match training file" print "map:", Theta.name(_test_lens), "trainer:", Theta.name(side) return 1 # sync the video and xml vid.restrict(mark_in, mark_out) cropped_total = mark_out - mark_in mapper_xml.setRatio(cropped_total) # status print "ratio at:", mapper_xml.ratio() print "offset by:", cfg.offset, "in", cfg.offset_mode, "mode" print "" # open export (if specified) if args.video_export: in_fps = vid.get(cv2.CAP_PROP_FPS) in_size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))) out_vid = cv2.VideoWriter(args.video_export, cv2.VideoWriter_fourcc(*cfg.fourcc), in_fps, vid.shape[:2]) print "exporting to:", args.video_export else: cv2.namedWindow(window_name) print "interactive mode" # main loop while vid.isOpened(): frame = vid.frame() reprojerror_list = compareReproj(frame, vid.at(), mapper_xml, trainer_xml, reprojerror_list, cfg) # export or navigate if args.video_export: sys.stdout.write("Reading Video Frame: {} / {}\r".format(vid.at(), mark_out)) sys.stdout.flush() out_vid.write(frame) if vid.next(): reprojerror_list = compareReproj(frame, vid.at(), mapper_xml, trainer_xml, reprojerror_list, cfg) mapper_xml.next() else: calReprojList(reprojerror_list) print "\nend of video" break else: cv2.imshow(window_name, frame) key = cv2.waitKey(0) showAll = False # toggle to show all training points in one frame # controls if key == Key.esc: print "\nexiting." break elif key == Key.right: if vid.next(): mapper_xml.next() else: if lastframe is False: print "\nEnd of video" calReprojList(reprojerror_list) lastframe = True elif key == Key.left: if vid.back(): mapper_xml.back() elif key == Key.enter: while True: reprojerror_list = compareReproj(frame, vid.at(), mapper_xml, trainer_xml, reprojerror_list, cfg) cv2.imshow(window_name, frame) if vid.next(): mapper_xml.next() frame = vid.frame() # break when over else: break calReprojList(reprojerror_list) print "\nFast forwarded to end of video" if args.compare_export: print "Output to file" writeFile(args.compare_export, reprojerror_list) break elif key == Key.space: ''' TODO: display all training points, may be abandoned problems: frame is not refreshed after imshow if showAll is False: displayTrainPts(frame, mark_in, mark_out, trainer_xml, cfg) # #except: # print "Error in displaying training points. Exiting..." # return 1 # showAll = True else: frame, reprojerror_list = compareReproj(frame, vid.at(), mapper_xml, trainer_xml, reprojerror_list, cfg) showAll = False ''' pass # clean up vid.release() if args.video_export: out_vid.release() else: cv2.destroyAllWindows() return 0
def main(sysargs): args = EasyArgs(sysargs) cfg = EasyConfig(args.config, group="mapper") if "help" in args: usage() return 0 if ["calib", "trainer", "output"] not in args: print "Must specify: -calib, -trainer, -output files" usage() return 1 if len(args) == 1: print "Not enough input CSV files" usage() return 1 if len(args) > 2 and args.map_trainer_mode: print "Too many CSV for trainer-mapping mode" usage() return 1 if "force_side" in args: side = Theta.resolve(args.force_side) if side == Theta.NonDual: print "Invalid force_side argument:", args.force_side usage() return 1 # set side overrides force_button = (side == Theta.Buttonside) force_back = not force_button else: force_button = force_back = False # working vars csvs = {} frame_num = 0 # open source CSV datasets for i in range(1, len(args)): print args[i] csvs[i] = Memset(args[i]) # reel all the files up to their first flash for i in csvs: csvs[i].restrict() if len(csvs[i].row()) < 10: print "CSV file:", args[i], "contains no marker data!\nAborting." return 1 # override csv name if args.map_trainer_mode: csvs[1]._name = cfg.trainer_target # open calib files try: buttonside = Mapper(args.calib, args.trainer, cfg, Theta.Buttonside) backside = Mapper(args.calib, args.trainer, cfg, Theta.Backside) except Exception as e: print e.message return 1 count = {'bts':0, 'bks':0, 'rej':0} # open destination XML with open(args.output, "w") as xmlfile: w = XMLWriter(xmlfile) w.declaration() xmlfile.write("<!DOCTYPE dataset SYSTEM \"http://storage.gwillz.com.au/eagleeye_v2.dtd\">") doc = w.start("dataset") # main loop while True: w.start("frameInformation") w.element("frame", number=str(frame_num)) for i in csvs: c = csvs[i] # determine marker quality try: max_reflectors = int(c.row()[8]) visible_reflectors = int(c.row()[9]) except: print "Error in reading quality at row {}".format(i) return 1 try: # read VICON data x = float(c.row()[2]) y = float(c.row()[3]) z = float(c.row()[4]) # TODO: is this necessary? We never use the object's rotation rx = float(c.row()[5]) ry = float(c.row()[6]) rz = float(c.row()[7]) except: print "Error occurred when converting VICON data at row {}".format(i) return 1 # run projection/mapping on VICON data if backside.isVisible((x,y,z)): points = backside.reprojpts((x, y, z)) side = 'backside' count['bks'] += 1 elif buttonside.isVisible((x,y,z)): points = buttonside.reprojpts((x, y, z)) points[0] += 960 # add 960 to x for rightside points side = 'buttonside' count['bts'] += 1 # TODO don't write non visible dots? else: points = [0.,0.] count['rej'] += 1 # TODO: Change DTD and double check with Manjung w.start("object", id=str(i), name=c.name(), lens=Theta.name(side)) w.element("boxinfo", height="99", width="99", x=str(points[0]-50), y=str(points[1]-50)) w.element("centroid", x=str(points[0]), y=str(points[1]), rx=str(rx), ry=str(ry), rz=str(rz)) w.element("visibility", visible=str(visible_reflectors), visibleMax=str(max_reflectors)) w.end() w.end() # test end of files eofs = 0 for i in csvs: if csvs[i].eof(): eofs += 1 if len(csvs) == eofs: print "end of all datasets" break # load next frame frame_num += 1 for i in csvs: csvs[i].next() w.close(doc) print "\nbuttonside", count['bts'] print "backside", count['bks'] print "rejected", count['rej'] return 0
def main(sysargs): args = EasyArgs(sysargs) cfg = EasyConfig(args.cfg, group="compare_trainer") window_name = "EagleEye Comparator" if "help" in args: usage() return 0 # grab marks from args if len(args) > 5: mark_in = args[4] mark_out = args[5] # test integer-ness try: int(mark_in) and int(mark_out) except: usage() return 1 # or grab them from a marker tool elif len(args) > 3: ret, mark_in, mark_out = marker_tool(args[1], 50, window_name) if not ret: print "Not processing - exiting." return 1 else: usage() return 1 # determine sides side = Theta.resolve(args.side or 'buttonside') # open inouts files vid = BuffSplitCap(args[1], buff_max=cfg.buffer_size, side=Theta.Both) mapper_xml = Xmlset(args[2], offset=cfg.offset, offmode=Xmlset.offset_mode(cfg.offset_mode)) trainer_xml = Xmltrainer(args[3], side=side) reprojerror_list = {} # list of reprojection error of all frames lastframe = False # reject mapper_xml if it doesn't contain the trainer_target if cfg.trainer_target not in mapper_xml.data(0): print "Mapping file must contain training target:", cfg.trainer_target print "Target names in mapping file:", mapper_xml.data(0) print "Please specifiy correct target name in config" return 1 # also reject if it's lens data doesn't match the trainer_xml _test_lens = mapper_xml.data(0)[cfg.trainer_target]['lens'] if (_test_lens == Theta.NonDual and side != Theta.NonDual) \ or (side != Theta.NonDual and _test_lens == Theta.NonDual): print "Mapping file doesn't match training file" print "map:", Theta.name(_test_lens), "trainer:", Theta.name(side) return 1 # sync the video and xml vid.restrict(mark_in, mark_out) cropped_total = mark_out - mark_in mapper_xml.setRatio(cropped_total) # status print "ratio at:", mapper_xml.ratio() print "offset by:", cfg.offset, "in", cfg.offset_mode, "mode" print "" # open export (if specified) if args.video_export: in_fps = vid.get(cv2.CAP_PROP_FPS) in_size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))) out_vid = cv2.VideoWriter(args.video_export, cv2.VideoWriter_fourcc(*cfg.fourcc), in_fps, vid.shape[:2]) print "exporting to:", args.video_export else: cv2.namedWindow(window_name) print "interactive mode" # main loop while vid.isOpened(): frame = vid.frame() reprojerror_list = compareReproj(frame, vid.at(), mapper_xml, trainer_xml, reprojerror_list, cfg) # export or navigate if args.video_export: sys.stdout.write("Reading Video Frame: {} / {}\r".format( vid.at(), mark_out)) sys.stdout.flush() out_vid.write(frame) if vid.next(): reprojerror_list = compareReproj(frame, vid.at(), mapper_xml, trainer_xml, reprojerror_list, cfg) mapper_xml.next() else: calReprojList(reprojerror_list) print "\nend of video" break else: cv2.imshow(window_name, frame) key = cv2.waitKey(0) showAll = False # toggle to show all training points in one frame # controls if key == Key.esc: print "\nexiting." break elif key == Key.right: if vid.next(): mapper_xml.next() else: if lastframe is False: print "\nEnd of video" calReprojList(reprojerror_list) lastframe = True elif key == Key.left: if vid.back(): mapper_xml.back() elif key == Key.enter: while True: reprojerror_list = compareReproj(frame, vid.at(), mapper_xml, trainer_xml, reprojerror_list, cfg) cv2.imshow(window_name, frame) if vid.next(): mapper_xml.next() frame = vid.frame() # break when over else: break calReprojList(reprojerror_list) print "\nFast forwarded to end of video" if args.compare_export: print "Output to file" writeFile(args.compare_export, reprojerror_list) break elif key == Key.space: ''' TODO: display all training points, may be abandoned problems: frame is not refreshed after imshow if showAll is False: displayTrainPts(frame, mark_in, mark_out, trainer_xml, cfg) # #except: # print "Error in displaying training points. Exiting..." # return 1 # showAll = True else: frame, reprojerror_list = compareReproj(frame, vid.at(), mapper_xml, trainer_xml, reprojerror_list, cfg) showAll = False ''' pass # clean up vid.release() if args.video_export: out_vid.release() else: cv2.destroyAllWindows() return 0