def main(args): # The image source may differ from the default depending on the command-line arguments source = None try: if args.picamera: source = capture.picamera() LOG.info('Will grab frames from the picamera.') elif args.image is not None: file = args.image source = capture.image_file(file) LOG.info('Will grab frames from a static image: {}.'.format(file)) elif args.ipcamera is not None: url = 'http://{}/mjpg/video.mjpg'.format(args.ipcamera) source = capture.ip_camera(url) LOG.info('Will grab frames from IP camera at {}'.format(url)) else: index = args.webcam source = capture.webcam(index) LOG.info('Will grab frames from webcam #{}.'.format(index)) except (capture.CameraInitializationError, FileNotFoundError) as e: # TODO Do something meaningful and retry LOG.error(e) exit('Unable to read from image source.') # Initialize NetworkTables one time at the start; other code assumes it is initialized. network.init_network_tables(args.ip) # NetworkTables communication. The script aims to be interchangeable with GRIP. network_publisher = NetworkTablePublisher('GRIP/myContoursReport', silent=args.nt_silent) # Debug communication detect_receiver = None # TODO detect_publisher = NetworkTablePublisher('Grapple', silent=args.nt_silent) detect = Detect(detect_receiver, detect_publisher) # For GUI debugging display = DisplayObserver() for image in source: targets = detect.get_data(image) # Adapt the payload into the same format as GRIP to be compatible with # the current code on the RIO # TODO this is probably why the zip function is a thing areas, centers_x, centers_y = [], [], [] for area, (center_x, center_y) in targets: areas.append(area) centers_x.append(center_x) centers_y.append(center_y) network_publisher.on_next(('area', areas)) network_publisher.on_next(('centerX', centers_x)) network_publisher.on_next(('centerY', centers_y)) if not args.no_gui: # Overlay the contours onto the original image and display it centers = list(zip(centers_x, centers_y)) display.on_next(detect.to_image(centers)) # All done, manually evoke cleanup callbacks network_publisher.on_completed() display.on_completed()