def start_self_driving(): global on model = inference.ModelDescriptor( name='mobilenet_160', input_shape=(1, 160, 160, 3), input_normalizer=(128.0, 128.0), compute_graph=utils.load_compute_graph(MODEL_NAME)) with PiCamera(sensor_mode=4, resolution=(160, 160), framerate=30) as camera: camera_thread = threading.Thread(target=capture, args=(camera, )) camera_thread.daemon = True camera_thread.start() with inference.CameraInference(model) as inf: print('Model is ready. Type on/off to start/stop self-driving') sys.stdout.flush() on_off_thread = threading.Thread(target=on_off, args=()) on_off_thread.daemon = True on_off_thread.start() for result in inf.run(): if on: direction, probability = process(result) print('prediction: {:.2f} {} {:.2f}'.format( time.time(), direction, probability)) sys.stdout.flush()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--input_layer', default='map/TensorArrayStack/TensorArrayGatherV3', help='Name of input layer.') parser.add_argument('--output_layer', default="prediction", help='Name of output layer.') parser.add_argument( '--num_frames', type=int, default=-1, help='Sets the number of frames to run for, otherwise runs forever.') parser.add_argument('--input_mean', type=float, default=128.0, help='Input mean.') parser.add_argument('--input_std', type=float, default=128.0, help='Input std.') parser.add_argument( '--threshold', type=float, default=0.1, help='Threshold for classification score (from output tensor).') parser.add_argument('--top_k', type=int, default=3, help='Keep at most top_k labels.') parser.add_argument('--detecting_list', type=list, default=[ 'Biston betularia (Peppered Moth)', 'Spodoptera litura (Oriental Leafworm Moth)' ], help='Input a list of bugs that you want to keep.') parser.add_argument('--message_threshold', type=int, default=1, help='Input detection threshold for sending sms') args = parser.parse_args() model = inference.ModelDescriptor( name='mobilenet_based_classifier', input_shape=(1, 192, 192, 3), input_normalizer=(128.0, 128.0), compute_graph=utils.load_compute_graph( 'mobilenet_v2_192res_1.0_inat_insect.binaryproto')) labels = read_labels( "/home/pi/models/mobilenet_v2_192res_1.0_inat_insect_labels.txt") detector = FawDetector() detector.run(args.input_layer, args.output_layer, args.num_frames, args.input_mean, args.input_std, args.threshold, args.top_k, args.detecting_list, args.message_threshold, model, labels)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model_path', required=True, help='Path to converted model file that can run on VisionKit.') parser.add_argument('--label_path', required=True, help='Path to label file that corresponds to the model.') parser.add_argument('--input_height', type=int, required=True, help='Input height.') parser.add_argument('--input_width', type=int, required=True, help='Input width.') parser.add_argument('--input_layer', required=True, help='Name of input layer.') parser.add_argument('--output_layer', required=True, help='Name of output layer.') parser.add_argument('--num_frames', type=int, default=None, help='Sets the number of frames to run for, otherwise runs forever.') parser.add_argument('--input_mean', type=float, default=128.0, help='Input mean.') parser.add_argument('--input_std', type=float, default=128.0, help='Input std.') parser.add_argument('--input_depth', type=int, default=3, help='Input depth.') parser.add_argument('--threshold', type=float, default=0.1, help='Threshold for classification score (from output tensor).') parser.add_argument('--top_k', type=int, default=3, help='Keep at most top_k labels.') parser.add_argument('--preview', action='store_true', default=False, help='Enables camera preview in addition to printing result to terminal.') parser.add_argument('--show_fps', action='store_true', default=False, help='Shows end to end FPS.') args = parser.parse_args() model = inference.ModelDescriptor( name='mobilenet_based_classifier', input_shape=(1, args.input_height, args.input_width, args.input_depth), input_normalizer=(args.input_mean, args.input_std), compute_graph=utils.load_compute_graph(args.model_path)) labels = read_labels(args.label_path) with PiCamera(sensor_mode=4, resolution=(1640, 1232), framerate=30) as camera: if args.preview: camera.start_preview() with inference.CameraInference(model) as camera_inference: for result in camera_inference.run(args.num_frames): processed_result = process(result, labels, args.output_layer, args.threshold, args.top_k) send_signal_to_servos(processed_result[0]) message = get_message(processed_result, args.threshold, args.top_k) if args.show_fps: message += '\nWith %.1f FPS.' % camera_inference.rate print(message) if args.preview: camera.annotate_foreground = Color('black') camera.annotate_background = Color('white') # PiCamera text annotation only supports ascii. camera.annotate_text = '\n %s' % message.encode( 'ascii', 'backslashreplace').decode('ascii') if args.preview: camera.stop_preview()
def start_self_driving(): model = inference.ModelDescriptor( name='mobilenet_160', input_shape=(1, 160, 160, 3), input_normalizer=(128.0, 128.0), compute_graph=utils.load_compute_graph(MODEL_NAME)) print('Model loaded') with PiCamera(sensor_mode=4, resolution=(160, 160), framerate=30) as camera: print('Connected to the Pi Camera') with inference.CameraInference(model) as inf: for result in inf.run(): direction, probability = process(result) RCool_drive.drive(direction) print('{:.2f} {} {:.2f}'.format(time.time(), direction, probability))
def main(): model = inference.ModelDescriptor( name='mobilenet_160', input_shape=(1, 160, 160, 3), input_normalizer=(128.0, 128.0), compute_graph=utils.load_compute_graph('dumb.binaryproto')) with inference.ImageInference(model) as inf: print('Waiting for input...') sys.stdout.flush() for _ in sys.stdin: now = time.time() img = Image.open('{}/current.jpg'.format(os.getcwd())) result = inf.run(img) label, probability = process(result) print('prediction: {} {} {}'.format(now, label, probability)) sys.stdout.flush()
def initialize(self): if Model.car is None: car = Car() if Car.connected: Model.car = car self.log('INFO', 'Car for self-driving is connected') else: self.log('ERROR', 'Car is not connected for self-driving') return False if Model.model is None: try: from aiy.vision import inference from aiy.vision.models import utils Model.model = inference.ModelDescriptor( name='mobilenet_160', input_shape=(1, 160, 160, 3), input_normalizer=(128.0, 128.0), compute_graph=utils.load_compute_graph(MODEL_NAME)) self.log('INFO', 'Self-driving model is loaded') except Exception as e: self.log( 'ERROR', 'Self-driving model cannot be loaded: {}'.format(str(e))) return False if Model.inference_engine is None: try: from aiy.vision import inference Model.inference_engine = inference.InferenceEngine() try: Model.inference_engine.unload_model('mobilenet_160') except: pass Model.model_name = Model.inference_engine.load_model( Model.model) Model.good = True self.log('INFO', 'Image inference has started') except Exception as e: self.log( 'ERROR', 'Image inference cannot be started: {}'.format(str(e))) return False return True
def main(): # Loading the model and label model = inference.ModelDescriptor( name='mobilenet_based_classifier', input_shape=(1, 160, 160, 3), input_normalizer=(128.0, 128.0), compute_graph=utils.load_compute_graph('CrackClassification_graph.binaryproto')) print("Model loaded.") labels = read_labels(label_path + 'crack_label.txt') print("Labels loaded") # Classifier parameters top_k = 3 threshold = 0.4 num_frame = None show_fps = False # LED setup ledRED = LED(PIN_B) ledGREEN = LED(PIN_A) ledRED.off() ledGREEN.on() with PiCamera(sensor_mode=4, resolution=(1640, 1232), framerate=30) as camera: with inference.CameraInference(model) as camera_inference: for result in camera_inference.run(num_frame): processed_result = process(result, labels, 'final_result',threshold, top_k) if processed_result[0][0] == 'positive': print("CRACK") ledGREEN.off() ledRED.on() else: print("CLEAR") ledRED.off() ledGREEN.on() print("Camera inference rate: " + str(camera_inference.rate))
with open(filename) as fp: fp = open(filename) tmp_shutter_numb = fp.readlines() tmp_shutter_numb = tmp_shutter_numb[0].rstrip() shutter_numb = int(tmp_shutter_numb) def read_labels(label_path): with open(label_path) as label_file: return [label.strip() for label in label_file.readlines()] model = inference.ModelDescriptor( name='mobilenet_based_classifier', input_shape=(1, args.input_height, args.input_width, args.input_depth), input_normalizer=(args.input_mean, args.input_std), compute_graph=utils.load_compute_graph(args.model_path)) labels = read_labels(args.label_path) def get_message(result, threshold, top_k): if result: return '%s' % '\n'.join(result) else: return 'Nothing detected when threshold=%.2f, top_k=%d' % (threshold, top_k) def process(result, labels, tensor_name, threshold, top_k): """Processes inference result and returns labels sorted by confidence."""
def main(): parser = argparse.ArgumentParser() parser.add_argument('--dog_park_model_path', help='Path to the model file for the dog park.') parser.add_argument('--vb1_model_path', help='Path to the model file for volley ball court 1.') parser.add_argument('--vb2_model_path', help='Path to the model file for volley ball court 1.') parser.add_argument( '--label_path', required=True, help='Path to label file that corresponds to the model.') parser.add_argument('--input_mean', type=float, default=128.0, help='Input mean.') parser.add_argument('--input_std', type=float, default=128.0, help='Input std.') parser.add_argument('--input_depth', type=int, default=3, help='Input depth.') parser.add_argument('--enable_streaming', default=False, action='store_true', help='Enable streaming server') parser.add_argument('--streaming_bitrate', type=int, default=1000000, help='Streaming server video bitrate (kbps)') parser.add_argument('--mdns_name', default='', help='Streaming server mDNS name') parser.add_argument( '--preview', action='store_true', default=False, help= 'Enables camera preview in addition to printing result to terminal.') parser.add_argument( '--time_interval', type=int, default=10, help='Time interval at which to store data in seconds.') parser.add_argument( '--gather_data', action='store_true', default=False, help='Also save images according to the assigned category.') parser.add_argument( '--timelapse', action='store_true', default=False, help='Also save some timelapses of the entire scene, every 120 seconds.' ) parser.add_argument('--image_folder', default='/home/pi/Pictures/Data', help='Folder to save captured images') args = parser.parse_args() labels = read_labels(args.label_path) # At least one model needs to be passed in. assert args.dog_park_model_path or args.vb1_model_path or args.vb2_model_path # Check that the folder exists if args.gather_data: expected_subfolders = ['dog_park', 'court_one', 'court_two'] subfolders = os.listdir(args.image_folder) for folder in expected_subfolders: assert folder in subfolders with ExitStack() as stack: dog_park = { 'location_name': 'dog_park', 'path': args.dog_park_model_path, } if args.dog_park_model_path else None vb1 = { 'location_name': 'court_one', 'path': args.vb1_model_path, } if args.vb1_model_path else None vb2 = { 'location_name': 'court_two', 'path': args.vb2_model_path, } if args.vb2_model_path else None # Get the list of models, filter to only the ones that were passed in. models = [dog_park, vb1, vb2] models = list(filter(lambda model: model, models)) # Initialize models and add them to the context for model in models: print('Initializing {model_name}...'.format( model_name=model["location_name"])) descriptor = inference.ModelDescriptor( name='mobilenet_based_classifier', input_shape=(1, 160, 160, args.input_depth), input_normalizer=(args.input_mean, args.input_std), compute_graph=utils.load_compute_graph(model['path'])) model['descriptor'] = descriptor if dog_park: dog_park['image_inference'] = stack.enter_context( inference.ImageInference(dog_park['descriptor'])) if vb1: vb1['image_inference'] = stack.enter_context( inference.ImageInference(vb1['descriptor'])) if vb2: vb2['image_inference'] = stack.enter_context( inference.ImageInference(vb2['descriptor'])) camera = stack.enter_context( PiCamera(sensor_mode=4, resolution=(820, 616), framerate=30)) server = None if args.enable_streaming: server = stack.enter_context( StreamingServer(camera, bitrate=args.streaming_bitrate, mdns_name=args.mdns_name)) if args.preview: # Draw bounding boxes around locations # Load the arbitrarily sized image img = Image.new('RGB', (820, 616)) draw = ImageDraw.Draw(img) for location in LOCATIONS.values(): x1, y1, x2, y2 = location draw_rectangle(draw, x1, y1, x2, y2, 3, outline='white') # Create an image padded to the required size with # mode 'RGB' pad = Image.new('RGB', ( ((img.size[0] + 31) // 32) * 32, ((img.size[1] + 15) // 16) * 16, )) # Paste the original image into the padded one pad.paste(img, (0, 0)) # Add the overlay with the padded image as the source, # but the original image's dimensions camera.add_overlay(pad.tobytes(), alpha=64, layer=3, size=img.size) camera.start_preview() data_filename = _make_filename(args.image_folder, 'data', None, 'json') data_generator = commit_data_to_long_term(args.time_interval, data_filename) data_generator.send(None) # Capture one picture of entire scene each time it's started again. time.sleep(2) date = time.strftime('%Y-%m-%d') scene_filename = _make_filename(args.image_folder, date, None) camera.capture(scene_filename) # Draw bounding box on image showing the crop locations with Image.open(scene_filename) as scene: draw = ImageDraw.Draw(scene) for location in LOCATIONS.values(): x1, y1, x2, y2 = location draw_rectangle(draw, x1, y1, x2, y2, 3, outline='white') scene.save(scene_filename) # Constantly get cropped images for cropped_images in get_cropped_images(camera, args.timelapse): svg_doc = None if args.enable_streaming: width = 820 * SVG_SCALE_FACTOR height = 616 * SVG_SCALE_FACTOR svg_doc = svg.Svg(width=width, height=height) for location in LOCATIONS.values(): x, y, x2, y2 = location w = (x2 - x) * SVG_SCALE_FACTOR h = (y2 - y) * SVG_SCALE_FACTOR x = x * SVG_SCALE_FACTOR y = y * SVG_SCALE_FACTOR svg_doc.add( svg.Rect( x=int(x), y=int(y), width=int(w), height=int(h), rx=10, ry=10, fill_opacity=0.3, style='fill:none;stroke:white;stroke-width:4px')) # For each inference model, crop and process a different thing. for model in models: location_name = model['location_name'] image_inference = model['image_inference'] cropped_image = cropped_images[location_name] # TODO: (Image Comparison) If False,return no activity. if cropped_image: # then run image_inference on them. result = image_inference.run(cropped_image) processed_result = process(result, labels, 'final_result') data_generator.send( (location_name, processed_result, svg_doc)) message = get_message(processed_result) # Print the message # print('\n') # print('{location_name}:'.format(location_name=location_name)) # print(message) else: # Fake processed_result processed_result = [('inactive', 1.00), ('active', 0.00)] data_generator.send( (location_name, processed_result, svg_doc)) label = processed_result[0][0] timestamp = time.strftime('%Y-%m-%d_%H.%M.%S') # print(timestamp) # print('\n') if args.gather_data and cropped_image: # Gather 1% data on 'no activity' since it's biased against that. # Gather 0.1% of all images. if ( # (label == 'no activity' and random.random() > 0.99) or # (random.random() > 0.999) # (location_name != 'dog_park' and random.random() > 0.99) or (random.random() > 0.9)): subdir = '{location_name}/{label}'.format( location_name=location_name, label=label) filename = _make_filename(args.image_folder, timestamp, subdir) cropped_image.save(filename) # if svg_doc: # ## Plot points out # ## 160 x 80 grid # ## 16px width # ## 20, 40, 60 for 0, 1, 2 # lines = message.split('\n') # y_correction = len(lines) * 20 # for line in lines: # svg_doc.add(svg.Text(line, # x=(LOCATIONS[location_name][0]) * SVG_SCALE_FACTOR, # y=(LOCATIONS[location_name][1] - y_correction) * SVG_SCALE_FACTOR, # fill='white', font_size=20)) # y_correction = y_correction - 20 # TODO: Figure out how to annotate at specific locations. # if args.preview: # camera.annotate_foreground = Color('black') # camera.annotate_background = Color('white') # # PiCamera text annotation only supports ascii. # camera.annotate_text = '\n %s' % message.encode( # 'ascii', 'backslashreplace').decode('ascii') if server: server.send_overlay(str(svg_doc)) if args.preview: camera.stop_preview()
def main(): parser = argparse.ArgumentParser() parser.add_argument( '--model_path', required=True, help='Path to converted model file that can run on VisionKit.') parser.add_argument( '--label_path', required=True, help='Path to label file that corresponds to the model.') parser.add_argument('--input_height', type=int, required=True, help='Input height.') parser.add_argument('--input_width', type=int, required=True, help='Input width.') parser.add_argument('--input_layer', required=True, help='Name of input layer.') parser.add_argument('--output_layer', required=True, help='Name of output layer.') parser.add_argument('--input_mean', type=float, default=128.0, help='Input mean.') parser.add_argument('--input_std', type=float, default=128.0, help='Input std.') parser.add_argument('--input_depth', type=int, default=3, help='Input depth.') parser.add_argument( '--threshold', type=float, default=0.1, help='Threshold for classification score (from output tensor).') parser.add_argument('--top_k', type=int, default=1, help='Keep at most top_k labels.') args = parser.parse_args() model = inference.ModelDescriptor( name='mobilenet_based_classifier', input_shape=(1, args.input_height, args.input_width, args.input_depth), input_normalizer=(args.input_mean, args.input_std), compute_graph=utils.load_compute_graph(args.model_path)) labels = read_labels(args.label_path) print("Taking photo") with PiCamera() as camera: camera.resolution = (640, 480) camera.start_preview() sleep(3.000) camera.capture(photo_filename) with inference.ImageInference(model) as image_inference: image = Image.open(photo_filename) result = image_inference.run(image) processed_result = process(result, labels, args.output_layer, args.threshold, args.top_k) message = get_message(processed_result, args.threshold, args.top_k) return message
def main(): parser = argparse.ArgumentParser() parser.add_argument('--input_layer', default='map/TensorArrayStack/TensorArrayGatherV3', help='Name of input layer.') parser.add_argument('--output_layer', default="prediction", help='Name of output layer.') parser.add_argument( '--num_frames', type=int, default=-1, help='Sets the number of frames to run for, otherwise runs forever.') parser.add_argument('--input_mean', type=float, default=128.0, help='Input mean.') parser.add_argument('--input_std', type=float, default=128.0, help='Input std.') parser.add_argument( '--threshold', type=float, default=0.6, help='Threshold for classification score (from output tensor).') parser.add_argument('--top_k', type=int, default=3, help='Keep at most top_k labels.') parser.add_argument( '--detecting_list', type=list, default=[ 'Biston betularia (Peppered Moth)', 'Spodoptera litura (Oriental Leafworm Moth)', 'Utetheisa ornatrix (Ornate Bella Moth)', 'Polygrammate hebraeicum (Hebrew Moth)', 'Palpita magniferalis (Splendid Palpita Moth) (0.14)', 'Hyles lineata (White-lined Sphinx Moth)', 'Hemileuca eglanterina (Western Sheep Moth)', 'Ceratomia undulosa (Waved Sphinx Moth)', 'Nadata gibbosa (White-dotted Prominent Moth)', 'Lophocampa caryae (Hickory Tussock Moth)', 'Spodoptera ornithogalli (Yellow-striped Armyworm Moth)', 'Spodoptera litura (Oriental Leafworm Moth)', 'Charadra deridens (Laugher Moth)' ], help='Input a list of bugs that you want to keep.') parser.add_argument('--message_threshold', type=int, default=4, help='Input detection threshold for sending sms') args = parser.parse_args() model = inference.ModelDescriptor( name='mobilenet_based_classifier', input_shape=(1, 192, 192, 3), input_normalizer=(128.0, 128.0), compute_graph=utils.load_compute_graph( 'mobilenet_v2_192res_1.0_inat_insect.binaryproto')) labels = read_labels( "/home/pi/models/mobilenet_v2_192res_1.0_inat_insect_labels.txt") detector = FawDetector() detector.run(args.input_layer, args.output_layer, args.num_frames, args.input_mean, args.input_std, args.threshold, args.top_k, args.detecting_list, args.message_threshold, model, labels)