Demo entry 6636619

test

   

Submitted by anonymous on Aug 27, 2017 at 18:17
Language: Python 3. Code size: 1.5 kB.

import time
from picamera import PiCamera
from picamera.array import PiRGBArray
import tensorflow as tf

def get_labels():
    
    with open('../inception/retrained_labels.txt', 'r') as fin:
        labels = [line.rstrip('\n') for line in fin]
    return labels

def run_classification(labels):
    
    camera = PiCamera()
    camera.resolution = (320, 240)
    camera.framerate = 2
    raw_capture = PiRGBArray(camera, size=(320, 240))
    
    time.sleep(2)

    with tf.gfile.FastGFile("../inception/retrained_graph.pb", 'rb') as fin:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(fin.read())
        _ = tf.import_graph_def(graph_def, name='')

    with tf.Session() as sess:
        softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
        for _, image in enumerate(
                camera.capture_continuous(
                    raw_capture, format='bgr', use_video_port=True
                )
            ):
            decoded_image = image.array
            predictions = sess.run(softmax_tensor, {'DecodeJpeg:0': decoded_image})
            prediction = predictions[0]
            prediction = prediction.tolist()
            max_value = max(prediction)
            max_index = prediction.index(max_value)
            predicted_label = labels[max_index]
            print("%s (%.2f%%)" % (predicted_label, max_value * 100))
            raw_capture.truncate(0)
if __name__ == '__main__':
    run_classification(get_labels())

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).