-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
137 lines (114 loc) · 4.41 KB
/
main.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
Preparing model:
- Install bazel ( check tensorflow's github for more info )
Ubuntu 14.04:
- Requirements:
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
- Download bazel, ( https://github.com/bazelbuild/bazel/releases )
tested on: https://github.com/bazelbuild/bazel/releases/download/0.2.0/bazel-0.2.0-jdk7-installer-linux-x86_64.sh
- chmod +x PATH_TO_INSTALL.SH
- ./PATH_TO_INSTALL.SH --user
- Place bazel onto path ( exact path to store shown in the output)
- For retraining, prepare folder structure as
- root_folder_name
- class 1
- file1
- file2
- class 2
- file1
- file2
- Clone tensorflow
- Go to root of tensorflow
- bazel build tensorflow/examples/image_retraining:retrain
- bazel-bin/tensorflow/examples/image_retraining/retrain --image_dir /path/to/root_folder_name --output_graph /path/output_graph.pb --output_labels /path/output_labels.txt --bottleneck_dir /path/bottleneck
** Training done. **
For testing through bazel,
bazel build tensorflow/examples/label_image:label_image && \
bazel-bin/tensorflow/examples/label_image/label_image \
--graph=/path/output_graph.pb --labels=/path/output_labels.txt \
--output_layer=final_result \
--image=/path/to/test/image
For testing through python, change and run this code.
"""
import numpy as np
import tensorflow as tf
import sys
import requests
import time
import mimetypes
from PIL import Image
import urllib2 as urllib2
imagePath = ''
modelFullPath = 'graph.pb'
labelsFullPath = 'labels.txt'
class HeadRequest(urllib2.Request):
def get_method(self):
return 'HEAD'
def isImageUrl(url):
response= urllib2.urlopen(HeadRequest(url))
maintype= response.headers['Content-Type'].split(';')[0].lower()
if maintype not in ('image/png', 'image/jpeg', 'image/gif'):
return False
else:
return True
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(modelFullPath, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
def run_inference_on_image():
answer = None
if not tf.gfile.Exists(imagePath):
tf.logging.fatal('File does not exist %s', imagePath)
return answer
image_data = tf.gfile.FastGFile(imagePath, 'rb').read()
# Creates graph from saved GraphDef.
create_graph()
with tf.Session() as sess:
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
predictions = sess.run(softmax_tensor,
{'DecodeJpeg/contents:0': image_data})
predictions = np.squeeze(predictions)
top_k = predictions.argsort()[-5:][::-1] # Getting top 5 predictions
f = open(labelsFullPath, 'rb')
lines = f.readlines()
labels = [str(w).replace("\n", "") for w in lines]
for node_id in top_k:
human_string = labels[node_id]
score = predictions[node_id]
print('%s (score = %.5f)' % (human_string, score))
answer = labels[top_k[0]]
return answer
def download_from_url(url):
if(not isImageUrl(url)):
return False
else:
t0 = time.clock()
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
response = requests.get(url, headers=headers, stream=True)
content_type = response.headers['content-type']
extension = mimetypes.guess_extension(content_type)
global imagePath
imagePath = str(t0) + extension
with open(imagePath, 'wb') as handle:
if not response.ok:
print(response)
for block in response.iter_content(1024):
if not block:
break
handle.write(block)
img = Image.open(imagePath)
imagePath = imagePath.replace(extension, '.jpg')
img.save(imagePath)
return True
if __name__ == '__main__':
if len(sys.argv) == 2:
url = str(sys.argv[1])
if(download_from_url(url)):
print(run_inference_on_image())
else:
print("invalid (score = 1.0)")