-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel_image.py
199 lines (159 loc) · 6.22 KB
/
label_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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from twilio.rest import Client
import argparse
import sys
import time
import numpy as np
import tensorflow as tf
import cv2 as c
import threading
# Author A.C.M.Sulhi (CSE,UOM)
# This Code based on concurrent programming . when the code is running two threads executes .
# one thread is capture the photos and save it in a specific file , after insert the photo name to "queue_teken queue"
# other thread is initially load the trained tensorflow graph and if there any photo in "queue_taken" queue pop photo one by one
# and check whether there exisis any water stagnating places . if there exists , move there photo from current directory to another directory .
# also insert the name of the photo to "queue_varified" queue. otherwise delete the photo from existing directory.
queue_taken = [] #
queue_varified = []
class detection(threading.Thread):
def __init__(self , lock):
threading.Thread.__init__(self)
self.model_file = "tf_files/retrained_graph.pb"
self.label_file = "tf_files/retrained_labels.txt"
self.input_height = 224
self.input_width = 224
self.input_mean = 128
self.input_std = 128
self.input_layer = "input"
self.output_layer = "final_result"
self.locked = lock
def load_graph(self,model_file):
graph = tf.Graph()
graph_def = tf.GraphDef()
with open(model_file, "rb") as f:
graph_def.ParseFromString(f.read())
with graph.as_default():
tf.import_graph_def(graph_def)
return graph
def read_tensor_from_image_file(self,file_name, input_height=299, input_width=299,
input_mean=0, input_std=255):
input_name = "file_reader"
output_name = "normalized"
file_reader = tf.read_file(file_name, input_name)
if file_name.endswith(".png"):
image_reader = tf.image.decode_png(file_reader, channels = 3,
name='png_reader')
elif file_name.endswith(".gif"):
image_reader = tf.squeeze(tf.image.decode_gif(file_reader,
name='gif_reader'))
elif file_name.endswith(".bmp"):
image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
else:
image_reader = tf.image.decode_jpeg(file_reader, channels = 3,
name='jpeg_reader')
float_caster = tf.cast(image_reader, tf.float32)
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 load_labels(self,label_file):
label = []
proto_as_ascii_lines = tf.gfile.GFile(label_file).readlines()
for l in proto_as_ascii_lines:
label.append(l.rstrip())
return label
def run(self):
graph = self.load_graph(self.model_file)
file_path = "captured/"
while True:
if self.locked.getLock():
if len(queue_taken)!=0:
self.locked.setLock()
count =queue_taken.pop(0)
print ("poped" , count)
self.locked.releceLock()
file_name = file_path + count
t = self.read_tensor_from_image_file(file_name,
input_height=self.input_height,
input_width=self.input_width,
input_mean=self.input_mean,
input_std=self.input_std)
input_name = "import/" + self.input_layer
output_name = "import/" + self.output_layer
input_operation = graph.get_operation_by_name(input_name);
output_operation = graph.get_operation_by_name(output_name);
with tf.Session(graph=graph) as sess:
start = time.time()
results = sess.run(output_operation.outputs[0],
{input_operation.outputs[0]: t})
end=time.time()
results = np.squeeze(results)
top_k = results.argsort()[-5:][::-1]
labels = self.load_labels(self.label_file)
if results[1]>=0.5:
new_file = "detected/" + count
queue_varified.append(new_file)
text= new_file
message = client.messages.create(to=phone, from_="+18302132214",body=text)
print(message.sid)
print("")
print ("water" , count)
tf.gfile.Rename(file_name,new_file,overwrite=True)
else:
tf.gfile.Remove(file_name)
print ("no wter" , count)
print('\nEvaluation time (1-image): {:.3f}s\n'.format(end-start))
template = "{} (score={:0.5f})"
for i in top_k:
print(template.format(labels[i], results[i]))
class capturing(threading.Thread):
def __init__(self , lock):
threading.Thread.__init__(self)
self.locked = lock
def run(self):
cap=c.VideoCapture(0)
path = "captured/"
count = 1
while 1:
ret, frame = cap.read()
print ("captured" , count)
name1 = path+ str(count) +'.JPG'
n = str(count)+'.JPG'
if self.locked.getLock():
self.locked.setLock()
c.imwrite(name1,frame)
print ("saved" , count)
queue_taken.append(n)
print ("appended ",count)
self.locked.releceLock()
print ("sleeping")
time.sleep(2)
print ("wake up")
count+=1
# we creating a lock object to provide synchronisation to shared resourses. in our code "queue_taken" queue is a shared resourse.
class Lock:
def __init__(self):
self.locked = True
def setLock(self):
self.locked=False
def releceLock(self):
self.locked=True
def getLock(self):
return self.locked
if __name__ == "__main__":
# Your Account SID from twilio.com/console
account_sid = "ACa9bbf7cc2d7a2fed695afc56bf6b5141"
# Your Auth Token from twilio.com/console
auth_token = "07daccf0d2f6d0c02f6c59c12d51e6f5"
client = Client(account_sid, auth_token)
phone="+94769058318"
lock = Lock()
cap1 = capturing(lock)
det = detection(lock)
cap1.start()
det.start()
print ("finished")