def cli(left_eye, right_eye, output_folder, chessboard_rows, chessboard_columns): """ Find chessboard calibration images in both frames of the stereo pair """ out_path = Path(output_folder).expanduser() out_path.mkdir(parents=True, exist_ok=True) chessboard_size = (chessboard_columns, chessboard_rows) finder = ChessboardFinder(chessboard_size) stream = StereoCamera( left_src=left_eye, right_src=right_eye, ) player = VideoPlayer() with player, stream: original_writer = get_stereo_writer(stream, file_name="original_stereo.avi") annotated_writer = get_stereo_writer(stream, file_name="annotated_stereo.avi") frame_count = 0 good_count = 0 while go(): ok, frames = stream.read() frame_count += 1 if ok: good = [] annotated = [] # so we have a good stereo frame, now inspect each frame and if # a chessboard is found in each, save the pair to disk. for frame in frames.frames(): labels = [f"Stereo Calibrate {frame_count}"] display_frame = frame.copy() height, width, channels = display_frame.shape has_corners, corners = finder.corners(frame) if has_corners: good.append(True) finder.draw(display_frame, corners) labels.append("CHESSBOARD") else: good.append(False) labels.append("NO CHESSBOARD FOR YOU") add_frame_labels(display_frame, labels=labels) annotated.append(display_frame) if all(good): # both frames have verified chessboards save frames for analysis for side, frame in frames.calibration_named_frames(): cv.imwrite(f"{out_path}/{side}_{good_count:05}.jpg", frame) good_count += 1 player.show(np.hstack(annotated)) annotated_writer.stereo_write(annotated) original_writer.stereo_write(frames.frames())
def decorate_frame(frame, source): img = add_frame_labels( frame=frame, labels=[f"{source}"], color=colors.get("BHP"), ) return img
def add_calib_info(camera, img, side): """label the corrected frames to aid in diagnostics""" return add_frame_labels( frame=img, labels=calibration_labels(camera.calibration, side), color=colors.get("BLACK"), )
def canny(mat): img = imutils.auto_canny(image=mat, sigma=0.3) img = add_frame_labels( frame=img, labels=[f"canny cost: {canny.cost:6.3f}s"], color=colors.get("WHITE"), ) return img
def find_chessboards_in_stream(source, chessboard_size, out_folder): # accommodate the types of sources if source.find("rtsp") >= 0: source_path = source else: try: source = int(source) source_path = source except ValueError: source_path = str(Path(source).expanduser()) print(source_path) out_path = Path(out_folder).expanduser() out_path.mkdir(parents=True, exist_ok=True) stream = VideoStream(src=source_path) finder = ChessboardFinder(chessboard_size) player = VideoPlayer(stream=stream) with player, stream: count = 0 while go(): ok, frame = stream.read() labels = [f"FPS {stream.read.cost:.6f}s"] if ok: display_frame = frame.copy() has_corners, corners = finder.corners(frame) if has_corners: cv.imwrite(f"{out_path}/{count:05}.jpg", frame) count += 1 labels.append("CHESSBOARD") finder.draw(display_frame, corners) else: labels.append("NO CHESSBOARD FOR YOU") labels.append( f"find chessboard cost: {finder.has_chessboard.cost:.3f}s") add_frame_labels(display_frame, labels=labels) player.show(display_frame) else: print("No more frames") break
def decorate_frame(frame, side, count, source): img = add_frame_labels( frame=frame, labels=[ f"{side}", f"{source}", f"frame # {count}", ], color=colors.get("BHP"), ) return img
def size(img): h, w = img.shape[:2] img = add_frame_labels( frame=img, labels=[ f"size: {w}x{h}", "Geyser watch", "q to quit", ], color=colors.get("GREEN"), ) return img
def label_frame(camera, frame): labels = [ f"Reprojected fisheye frame", f"undistort cost: {camera.correct.cost:6.3f}s", f"balance: {camera.balance}", f"cid: {camera.cid} calibrated on {camera.calibration_time_formatted}", # f'dim2 {dim2}', # f'dim3 {dim3}', ] labeled_frame = add_frame_labels( frame=frame, labels=labels, color=colors.get("BHP"), ) return labeled_frame
def update(): rectified = camera.correct(img) labels = [ f"Reprojected fisheye frame", f"undistort cost: {camera.correct.cost:6.3f}s", f"balance: {camera.balance}", f"cid: {camera.cid} calibrated on {camera.calibration_time_formatted}", # f'dim2 {dim2}', # f'dim3 {dim3}', ] labeled_frame = add_frame_labels( frame=rectified, labels=labels, color=colors.get("BHP"), ) cv.imshow(window_name, labeled_frame) cv.imshow("Raw image", img)
def cli(left_source, right_source, calibration_file, balance): """ Displays corrected live stereo video feed from fisheye stereo rig Visual inspection tool to verify correction works """ camera = CalibratedFisheyeCamera( calibration_file=calibration_file, balance=balance, dim2=None, dim3=None, # remember we have these ) stream = VideoStream(src=source) player = VideoPlayer() with stream, player: ok, frame = stream.read() if ok: camera.set_map(first_frame=frame) else: print("Cannot read video feed") sys.exit(0) frame_count = 0 while go(): ok, frame = stream.read() if ok: frame_count += 1 undistorted_frame = camera.correct(frame) labels = [ f"Reprojected fisheye frame: {frame_count}", f"undistort cost: {camera.correct.cost:6.3f}s", f"balance: {balance}", f"cid: {camera.cid} calibrated on {camera.calibration_time_formatted}", # f'dim2 {dim2}', # f'dim3 {dim3}', ] labeled_frame = add_frame_labels( frame=undistorted_frame, labels=labels, color=colors.get("BHP"), ) stack = np.hstack((frame, labeled_frame)) player.show(stack)
def cli(source, calibration_file): """Undistort live feed from pinhole model type camera""" calibration_path = Path(calibration_file).expanduser() camera = CalibratedPinholeCamera(calibration_file=calibration_path) stream = VideoFile(src=str(source)) player = VideoPlayer() with stream, player: while go(): ok, frame = stream.read() if ok: frame = camera.correct(frame) frame = add_frame_labels( frame=frame, labels=[f"undistort cost: {camera.correct.cost:6.3f}s"], color=colors.get("BHP"), ) player.show(frame)
def get_frames(chessboards_folder, image_number): """get stereo frame pair from chessboard capture folder""" frames = [] source = Path(chessboards_folder).expanduser() if not source.exists(): print(f"Source folder {chessboards_folder} does not exist") sys.exit() for side in ("left", "right"): fname = f"{side}_{image_number}.jpg" file_path = source / Path(fname) print(f"loading {file_path}") if not file_path.exists(): print(f"{side} image file {file_path} does not exist") sys.exit() img = cv.imread(str(file_path)) img = add_frame_labels(img, labels=[f"{file_path}"]) img = add_reticle(img) frames.append(img) return frames