-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathonline.py
More file actions
55 lines (45 loc) · 1.92 KB
/
online.py
File metadata and controls
55 lines (45 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
Run the online classification system.
Capture an image, classify, do it again.
"""
import time
#from picamera import PiCamera
#from picamera.array import PiRGBArray
import tensorflow as tf
import cv2
import os
def get_labels():
"""Get a list of labels so we can see if it's an ad or not."""
with open('./inception/retrained_labels.txt', 'r') as fin:
labels = [line.rstrip('\n') for line in fin]
return labels
def run_classification(labels):
"""Stream images off the camera and process them."""
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:
# And capture continuously forever.
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
#for video in os.listdir("./predictimage"):
cam = cv2.VideoCapture(0)
while True:
ret, frame = cam.read()
#image1 = cv2.resize(frame, (640, 480))
#print(frame)
decoded_image = frame
# Make the prediction. Big thanks to this SO answer:
# http://stackoverflow.com/questions/34484148/feeding-image-data-in-tensorflow-for-transfer-learning
predictions = sess.run(softmax_tensor, {'DecodeJpeg:0': decoded_image})
prediction = predictions[0]
# Get the highest confidence category.
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))
# Reset the buffer so we're ready for the next one.
#raw_capture.truncate(0)
if __name__ == '__main__':
run_classification(get_labels())