Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# coding=UTF8
# Copyright 2015 Google Inc. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
# pi@lookbox:~ $ ./ 2>/dev/null
"""Simple image classification with Inception.
Run image classification with Inception trained on ImageNet 2012 Challenge data
This program creates a graph from a saved GraphDef protocol buffer,
and runs inference on an input JPEG image. It outputs human readable
strings of the top 5 predictions along with their probabilities.
Change the --image_file argument to any jpg image to compute a
classification of that image.
Please see the tutorial and website for a detailed description of how
to use this script to perform image recognition.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# cam
from import PiVideoStream
from import FPS
from picamera.array import PiRGBArray
from picamera import PiCamera
import argparse
import imutils
import time
import cv2
import random
import os
import os.path
import re
import sys
import tarfile
import numpy as np
from six.moves import urllib
import tensorflow as tf
# button
import RPi.GPIO as GPIO
import time
import datetime
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# classify_image_graph_def.pb:
# Binary representation of the GraphDef protocol buffer.
# imagenet_synset_to_human_label_map.txt:
# Map from synset ID to a human readable string.
# imagenet_2012_challenge_label_map_proto.pbtxt:
# Text representation of a protocol buffer mapping a label to synset ID.
'model_dir', '/home/pi/tensorflow/tensorflow/models/image/imagenet/model_dir',
"""Path to classify_image_graph_def.pb, """
"""imagenet_synset_to_human_label_map.txt, and """
"""imagenet_2012_challenge_label_map_proto.pbtxt.""")'image_file', '',
"""Absolute path to image file.""")'num_top_predictions', 5,
"""Display this many predictions.""")
# pylint: disable=line-too-long
# pylint: enable=line-too-long
# cam
print("[INFO] cam sampling THREADED frames from `picamera` module...")
vs = PiVideoStream().start()
fps = FPS().start()
### more
def run_image(sess, img_id, img_url, node_lookup):
from six.moves import urllib
from urllib2 import HTTPError
image_data = urllib.request.urlopen(img_url, timeout=1.0).read()
except HTTPError:
return (img_id, img_url, None)
return (img_id, img_url, None)
scores = []
softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
predictions =,
{'DecodeJpeg/contents:0': image_data})
predictions = np.squeeze(predictions)
top_k = predictions.argsort()[-num_top_predictions:][::-1]
scores = []
for node_id in top_k:
if node_id not in node_lookup:
human_string = ''
human_string = node_lookup[node_id]
score = predictions[node_id]
scores.append((human_string, score))
return (img_id, img_url, scores)
### end more
class NodeLookup(object):
"""Converts integer node ID's to human readable labels."""
def __init__(self,
if not label_lookup_path:
label_lookup_path = os.path.join(
FLAGS.model_dir, 'imagenet_2012_challenge_label_map_proto.pbtxt')
if not uid_lookup_path:
uid_lookup_path = os.path.join(
FLAGS.model_dir, 'imagenet_synset_to_human_label_map.txt')
self.node_lookup = self.load(label_lookup_path, uid_lookup_path)
def load(self, label_lookup_path, uid_lookup_path):
"""Loads a human readable English name for each softmax node.
label_lookup_path: string UID to integer node ID.
uid_lookup_path: string UID to human-readable string.
dict from integer node ID to human-readable string.
if not tf.gfile.Exists(uid_lookup_path):
tf.logging.fatal('File does not exist %s', uid_lookup_path)
if not tf.gfile.Exists(label_lookup_path):
tf.logging.fatal('File does not exist %s', label_lookup_path)
# Loads mapping from string UID to human-readable string
proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines()
uid_to_human = {}
p = re.compile(r'[n\d]*[ \S,]*')
for line in proto_as_ascii_lines:
parsed_items = p.findall(line)
uid = parsed_items[0]
human_string = parsed_items[2]
uid_to_human[uid] = human_string
# Loads mapping from string UID to integer node ID.
node_id_to_uid = {}
proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines()
for line in proto_as_ascii:
if line.startswith(' target_class:'):
target_class = int(line.split(': ')[1])
if line.startswith(' target_class_string:'):
target_class_string = line.split(': ')[1]
node_id_to_uid[target_class] = target_class_string[1:-2]
# Loads the final mapping of integer node ID to human-readable string
node_id_to_name = {}
for key, val in node_id_to_uid.items():
if val not in uid_to_human:
tf.logging.fatal('Failed to locate: %s', val)
name = uid_to_human[val]
node_id_to_name[key] = name
return node_id_to_name
def id_to_string(self, node_id):
if node_id not in self.node_lookup:
return ''
return self.node_lookup[node_id]
def create_graph():
"""Creates a graph from saved GraphDef file and returns a saver."""
# Creates graph from saved graph_def.pb.
with tf.gfile.FastGFile(os.path.join(
FLAGS.model_dir, 'classify_image_graph_def.pb'), 'rb') as f:
graph_def = tf.GraphDef()
_ = tf.import_graph_def(graph_def, name='')
def run_inference_on_image(fn='/home/pi/Desktop/camlive.jpg'):
"""Runs inference on an image.
image: Image file name.
print("Starting tf.Session()")
with tf.Session() as sess:
# Some useful tensors:
# 'softmax:0': A tensor containing the normalized prediction across
# 1000 labels.
# 'pool_3:0': A tensor containing the next-to-last layer containing 2048
# float description of the image.
# 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
# encoding of the image.
# Runs the softmax tensor by feeding the image_data as input to the graph.
print("Session initialized.")
node_lookup = NodeLookup() # Creates node ID --> English string lookup.
print("done node lookup")
softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
print("got tensor")
while True:
input_state = GPIO.input(18)
if input_state == False:
print('Button Pressed')
frame =
frame = imutils.resize(frame, width=400)
print('Captured %dx%d image' % ( frame.shape[1], frame.shape[0]) )
cv2.imwrite(fn, frame)
print("written file")
if not tf.gfile.Exists(fn):
tf.logging.fatal('File does not exist %s', fn)
image_data = tf.gfile.FastGFile(fn, 'rb').read()
print("read file")
predictions =, {'DecodeJpeg/contents:0': image_data})
predictions = np.squeeze(predictions)
top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]
print("got predictions")
node_id = top_k[0]
score = predictions[node_id]
human_string = None
# if(score > 0.20):
human_string = node_lookup.id_to_string(node_id)
arr = human_string.split(",")
print('%s (score = %.5f)' % (arr[0], score))
os.system("/usr/bin/pico2wave -w test.wav '"+arr[0]+"' | mplayer test.wav")
# else:
# os.system("/usr/bin/pico2wave -w test.wav 'nothing' | mplayer test.wav")
# save results
epoch = datetime.datetime.utcfromtimestamp(0)
dt ="%Y%m%dT%H%M%S")
os.system("cp "+fn+" results/"+dt+".jpg")
f1=open("results/"+dt+".txt", 'w+')
def maybe_download_and_extract():
"""Download and extract model tar file."""
dest_directory = FLAGS.model_dir
if not os.path.exists(dest_directory):
filename = DATA_URL.split('/')[-1]
filepath = os.path.join(dest_directory, filename)
if not os.path.exists(filepath):
def _progress(count, block_size, total_size):
sys.stdout.write('\r>> Downloading %s %.1f%%' % (
filename, float(count * block_size) / float(total_size) * 100.0))
filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath,
statinfo = os.stat(filepath)
print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.'), 'r:gz').extractall(dest_directory)
def main(_):
create_graph() # Creates graph from saved GraphDef.
if __name__ == '__main__':

This comment has been minimized.

Copy link

commented Apr 7, 2019

I tried implementing this on my Raspberry pi 3 following the instructions you have given in your blog. I was able to get the image identification working, but the text to speech conversion is not working neither are any os.system() commands after the execution of
predictions =, {'DecodeJpeg/contents:0': image_data})
predictions = np.squeeze(predictions)
top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]
print("got predictions")
Thanks in advance for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.