This repository has been archived by the owner on May 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
check_image.py
229 lines (194 loc) · 8.4 KB
/
check_image.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# Copyright 2018 The Fuego Authors.
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""
Show the ML scores for each square in an image
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import os
fuegoRoot = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(fuegoRoot, 'lib'))
sys.path.insert(0, fuegoRoot)
import settings
settings.fuegoRoot = fuegoRoot
import collect_args
import rect_to_squares
import tf_helper
import pathlib
import subprocess
import re
import numpy as np
import tensorflow as tf
import math
import tkinter as tk
from PIL import Image, ImageTk, ImageDraw, ImageFont
# alternate version that uses in-memory image segments without persisting to disk
from skimage import io
import numpy as np
def read_tensor_from_array(data,
input_height=299,
input_width=299,
input_mean=0,
input_std=255):
float_caster = data
dims_expander = tf.expand_dims(float_caster, 0)
resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
sess = tf.Session()
result = sess.run(normalized)
return result
def calcScoresInMemory(model_file, label_file, imgPath):
# XXXX
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.1
graph_def = tf.GraphDef()
with tf.gfile.FastGFile(model_file, 'rb') as f:
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
# Unpersists graph from file
# with tf.Session(config=config) as sess: #config=tf.ConfigProto(log_device_placement=True)
with tf.Session(config=config) as sess:
image = io.imread(imgPath, plugin='matplotlib')
print(image.shape)
# hardcoded for testing the code path.
coords=[
( 0,0,341,339 ),
( 341,0,682,339 ),
( 682,0,1024,339 ),
( 0,290,341,631 ),
( 341,290,682,631 ),
( 682,290,1024,631 ),
( 0,597,341,938 ),
( 341,597,682,938 ),
( 682,597,1024,938 ),
]
for (minX,minY,maxX,maxY) in coords:
print('coord', (minX,minY,maxX,maxY))
cropped_image=image[minY:maxY, minX:maxX]
print('shape', cropped_image.shape)
image_data = np.array(cropped_image)[:,:,0:3]
res = read_tensor_from_array(image_data)
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
try:
predictions = sess.run(softmax_tensor, {'Placeholder:0': res})
print('preds', predictions)
except:
print("image fault", imgPath, (minX,minY,maxX,maxY))
#Caused by image not decoding properly, even though it was a .jpg. Crashed the entire session.
print(image_data.shape)
continue
def imageDisplay(imgOrig, title=''):
rootTk = tk.Tk()
rootTk.title('Fuego: ' + title)
screen_width = rootTk.winfo_screenwidth() - 100
screen_height = rootTk.winfo_screenheight() - 100
print("Image:", (imgOrig.size[0], imgOrig.size[1]), ", Screen:", (screen_width, screen_height))
scaleX = min(screen_width/imgOrig.size[0], 1)
scaleY = min(screen_height/imgOrig.size[1], 1)
scaleFactor = min(scaleX, scaleY)
print('scale', scaleFactor, scaleX, scaleY)
scaledImg = imgOrig
if (scaleFactor != 1):
scaledImg = imgOrig.resize((int(imgOrig.size[0]*scaleFactor), int(imgOrig.size[1]*scaleFactor)), Image.ANTIALIAS)
imgPhoto = ImageTk.PhotoImage(scaledImg)
canvasTk = tk.Canvas(rootTk, width=imgPhoto.width(), height=imgPhoto.height(), bg="light yellow")
canvasTk.config(highlightthickness=0)
aff=canvasTk.create_image(0, 0, anchor='nw', image=imgPhoto)
canvasTk.focus_set()
canvasTk.pack(side='left', expand='yes', fill='both')
return (rootTk, canvasTk, imgPhoto, scaleFactor)
def buttonClick(event):
exit()
# use multiple colors to make it slightly easier to see the overlapping boxes
colors = ['red', 'blue']
def displayImageWithScores(imgOrig, segments):
(rootTk, canvasTk, imgPhoto, scaleFactor) = imageDisplay(imgOrig)
canvasTk.bind("<Button-1>", buttonClick)
canvasTk.bind("<Button-2>", buttonClick)
canvasTk.bind("<Button-3> ", buttonClick)
for counter, segmentInfo in enumerate(segments):
offset = ((counter%2) - 0.5)*2
x0 = segmentInfo['MinX']*scaleFactor + offset
y0 = segmentInfo['MinY']*scaleFactor + offset
x1 = segmentInfo['MaxX']*scaleFactor + offset
y1 = segmentInfo['MaxY']*scaleFactor + offset
centerX = (x0 + x1)/2
centerY = (y0 + y1)/2
color = colors[counter % len(colors)]
scoreStr = '%.2f' % segmentInfo['score']
canvasTk.create_text(centerX, centerY, fill=color, font="Arial 50", text=scoreStr)
canvasTk.create_rectangle(x0, y0, x1, y1, outline=color, width=2)
rootTk.mainloop()
def drawRect(imgDraw, x0, y0, x1, y1, width, color):
for i in range(width):
imgDraw.rectangle((x0+i,y0+i,x1-i,y1-i),outline=color)
def drawBoxesAndScores(imgOrig, segments):
imgDraw = ImageDraw.Draw(imgOrig)
for counter, segmentInfo in enumerate(segments):
offset = ((counter%2) - 0.5)*2
x0 = segmentInfo['MinX'] + offset
y0 = segmentInfo['MinY'] + offset
x1 = segmentInfo['MaxX'] + offset
y1 = segmentInfo['MaxY'] + offset
color = colors[counter % len(colors)]
lineWidth=3
drawRect(imgDraw, x0, y0, x1, y1, lineWidth, color)
centerX = (x0 + x1)/2
centerY = (y0 + y1)/2
fontSize=60
font = ImageFont.truetype(os.path.join(settings.fuegoRoot, 'lib/Roboto-Regular.ttf'), size=fontSize)
scoreStr = '%.2f' % segmentInfo['score']
textSize = imgDraw.textsize(scoreStr, font=font)
centerX -= textSize[0]/2
centerY -= textSize[1]/2
imgDraw.text((centerX,centerY), scoreStr, font=font, fill=color)
def main():
reqArgs = [
["i", "image", "filename of the image"],
["o", "output", "output directory name"],
]
optArgs = [
["l", "labels", "labels file generated during retraining"],
["m", "model", "model file generated during retraining"],
["d", "display", "(optional) specify any value to display image and boxes"]
]
args = collect_args.collectArgs(reqArgs, optionalArgs=optArgs)
model_file = args.model if args.model else settings.model_file
labels_file = args.labels if args.labels else settings.labels_file
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
graph = tf_helper.load_graph(model_file)
labels = tf_helper.load_labels(labels_file)
segments = []
with tf.Session(graph=graph) as tfSession:
if True: # chops image in segment files and classifies each segment
imgOrig = Image.open(args.image)
segments = rect_to_squares.cutBoxes(imgOrig, args.output, args.image)
tf_helper.classifySegments(tfSession, graph, labels, segments)
if False: # version that classifies entire image without cropping
imgOrig = Image.open(args.image)
segments = [{'imgPath': args.image}]
tf_helper.classifySegments(tfSession, graph, labels, segments)
if False: # chops image into in-memory segments and classifies each segment
calcScoresInMemory(args.model, args.labels, args.image)
for segmentInfo in segments:
print(segmentInfo['imgPath'], segmentInfo['score'])
if args.display:
drawBoxesAndScores(imgOrig, segments)
displayImageWithScores(imgOrig, [])
if __name__=="__main__":
main()