コード例 #1
0
ファイル: run_edge.py プロジェクト: prokosna/coral_scripts
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model', help='.tflite model file')
    parser.add_argument('--labels', help='.txt label file')
    parser.add_argument('--threshold',
                        help='Class Score Threshold',
                        type=float,
                        default=0.7)
    parser.add_argument('--top_k', help='Class Top K', type=int, default=2)
    parser.add_argument('--manual',
                        help='Take a picture when key pressed',
                        action='store_true')
    args = parser.parse_args()

    engine = ClassificationEngine(args.model)
    labels = load_labels(args.labels)

    input_monitor = InputMonitor(gpio_pin=8)
    led = LED(gpio_r=6, gpio_g=7, gpio_b=None, invert=True)
    led.switch_off_all()
    light_duration = 3 if args.manual else 0.1

    last_time = time.monotonic()

    if args.manual:
        input_monitor.daemon = True
        input_monitor.start()

    def user_callback(image, svg_canvas):
        nonlocal last_time

        if args.manual:
            if not input_monitor.is_key_pressed():
                return

        start_time = time.monotonic()
        results = engine.ClassifyWithImage(image,
                                           threshold=0.1,
                                           top_k=args.top_k)
        end_time = time.monotonic()

        text_lines = [
            'Inference: %.2f ms' % ((end_time - start_time) * 1000),
            'FPS: %.2f fps' % (1.0 / (end_time - last_time)),
        ]

        if len(results) == 0:
            led.switch_off_all()
        else:
            results.sort(key=lambda result: result[1], reverse=True)
            for index, score in results:
                text_lines.append('score=%.2f: %s' % (score, labels[index]))

            top_label = labels[results[0][0]]
            top_score = results[0][1]
            if top_score >= args.threshold:
                if top_label == 'roadway_green':
                    led.switch_green(duration=light_duration)
                elif top_label == 'roadway_red':
                    led.switch_red(duration=light_duration)
                elif top_label == 'roadway_yellow':
                    led.switch_yellow(duration=light_duration)
                else:
                    led.switch_off_all()

        last_time = end_time
        print(' '.join(text_lines))
        generate_svg(svg_canvas, text_lines)

    gstreamer.run_pipeline(user_callback)
コード例 #2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--project', help='GCP Project')
    parser.add_argument('--bucket', help='GCS bucket name')
    parser.add_argument('--path', help='GCS path prefix for uploading images')
    parser.add_argument('--region', help='GCP Region')
    parser.add_argument('--registry_id', help='IoT Core Registry ID')
    parser.add_argument('--device_id', help='IoT Core Device ID')
    parser.add_argument('--private_key', help='IoT Core Private Key File')
    parser.add_argument('--algorithm', help='IoT Core JWT Algorithm',
                        default='RS256')
    parser.add_argument('--ca_certs', help='IoT Core roots.pem',
                        default='roots.pem')
    parser.add_argument('--mqtt_host', help='IoT Core hostname',
                        default='mqtt.googleapis.com')
    parser.add_argument('--mqtt_port', help='IoT Core port',
                        type=int,
                        default=443)
    args = parser.parse_args()

    input_monitor = InputMonitor(gpio_pin=8)
    led = LED(gpio_r=6, gpio_g=7, gpio_b=None, invert=True)
    led.switch_off_all()

    def user_callback(image, svg_canvas):
        nonlocal input_monitor

        if input_monitor.is_key_pressed():
            upload.upload(args.bucket, args.path, image)

    input_monitor.daemon = True
    input_monitor.start()
    print('monitoring keyboard input...')

    mqtt.setup_mqtt_client(
        args.project,
        args.registry_id,
        args.private_key,
        args.device_id,
        args.region,
        args.algorithm,
        args.ca_certs,
        args.mqtt_host,
        args.mqtt_port)

    def message_callback(payload):
        try:
            preds = json.loads(payload)
            preds.sort(key=lambda pred: pred['class_score'], reverse=True)
            top = preds[0]['class_name']
            if top == 'roadway_green':
                led.switch_green(duration=3)
            elif top == 'roadway_red':
                led.switch_red(duration=3)
            elif top == 'roadway_yellow':
                led.switch_yellow(duration=3)
            else:
                led.switch_off_all()
        except Exception as ex:
            print(ex)

    mqtt.add_message_callback(message_callback)

    gstreamer.run_pipeline(user_callback)