forked from jtheiner/LegoBrickClassification
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCamera_Feed.py
307 lines (266 loc) · 10.7 KB
/
Camera_Feed.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import numpy as np
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import cv2
from PIL import Image
import time
import numpy as np
import imutils
import argparse
import sys
import os
import tensorflow as tf
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Dense, Flatten, Dropout
from tensorflow.keras.applications import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam, RMSprop
#from getkeys import key_check
from matplotlib import pyplot as plt
def camera_info(video_in):
width = int(video_in.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video_in.get(cv2.CAP_PROP_FRAME_HEIGHT))
length = int(video_in.get(cv2.CAP_PROP_FRAME_COUNT))
fps = int(video_in.get(5))
print(' Height {} x Width {} FPS {} FPS {}'.format(height,width,length,fps))
def onclick(event):
print("test")
def press(event):
print('press', event.key)
sys.stdout.flush()
if event.key == 'x':
sys.stdout.flush()
cam.release()
print("key")
def screen_record():
cam = cv2.VideoCapture(0)
image_1= None
last_time = time.time()
FIGSIZE = 6.0
SPACING = 0.1
rows = 2
cols = 2
subplot=(rows,cols,1)
plt.figure(figsize=(FIGSIZE,FIGSIZE/cols*rows))
cam.set(cv2.CAP_PROP_FPS, 100)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
camera_info(cam)
while(True):
ret, frame = cam.read()
if frame is None:
print("No Camera")
break
if ret is False:
print("No Picture")
break
cam_frame = imutils.resize(frame, width=500)
gray = cv2.cvtColor(cam_frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
if image_1 is None:
image_1 = gray
continue
frameDelta = cv2.absdiff(image_1, gray)
#cv2.imshow("Frame Delta", frameDelta)
#print(np.argmax(frameDelta, axis=-1))
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
diff =np.concatenate(thresh).sum()
print('loop took {} seconds'.format(time.time()-last_time))
last_time = time.time()
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
if diff >100000:
thresh = cv2.dilate(thresh, None, iterations=2)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(cnts)
image_1= gray
biggest_contour =0
for c in contours:
#print(cv2.contourArea(c))
if cv2.contourArea(c) > biggest_contour:
biggest_contour = cv2.contourArea(c)
(x, y, w, h) = cv2.boundingRect(c)
continue
cutout_frame = np.zeros(cam_frame.shape, dtype=np.uint8)
blank =True
if biggest_contour>0:
cv2.rectangle(cam_frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
print('Box x={} y={} w={} h={}'.format(x,y,w,h))
if w*h > 2000:
cutout_frame = cam_frame[y:y+h, x:x+w]
blank =False
#fig, ax = plt.subplots()
#plt.subplot(*subplot)
#cid = fig.canvas.mpl_connect('button_press_event', onclick)
#plt.axis('off')
#plt.title('previous_image')
#plt.imshow(image_1)
#plt.subplot(subplot[0],subplot[1],subplot[2]+1)
#plt.axis('off')
#plt.title('Processed')
#plt.imshow(gray)
#plt.subplot(subplot[0],subplot[1],subplot[2]+2)
#plt.title('Contour')
#plt.axis('off')
#plt.imshow(cam_frame)
#plt.subplot(subplot[0],subplot[1],subplot[2]+3)
if blank == False:
title = 'Cut out w='+str(w)+' h='+str(h)
#plt.title(title)
#plt.axis('off')
#plt.imshow(cutout_frame)
cv2.imshow(title, cutout_frame)
#plt.ion()
#plt.clf()
#plt.show()
#plt.pause(1)
#plt.tight_layout()
#plt.subplots_adjust(wspace=SPACING, hspace=SPACING)
#keys = key_check()
# p pauses game and can get annoying.
if cv2.waitKey(25) & 0xFF == ord('q'):
cam.release()
break
def get_strategy():
try: # detect TPUs
tpu = tf.distribute.cluster_resolver.TPUClusterResolver() # TPU detection
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
strategy = tf.distribute.experimental.TPUStrategy(tpu)
except ValueError: # no TPU found, detect GPUs
#strategy = tf.distribute.MirroredStrategy() # for GPU or multi-GPU machines
strategy = tf.distribute.get_strategy() # default strategy that works on CPU and single GPU
#strategy = tf.distribute.experimental.MultiWorkerMirroredStrategy() # for clusters of multi-GPU machines
print("Number of accelerators: ", strategy.num_replicas_in_sync)
return strategy
def load_model():
strategy = get_strategy()
dir_path = os.path.dirname(os.path.realpath(__file__))
model_file = os.path.join (dir_path , "Brick_Rec")
model = tf.keras.models.load_model(model_file)
model.summary()
return model
## if os.path.isfile(model_file):
##
## new_model.summary()
## else:
## print(model_file)
## print("Model does not exist")
## exit()
def file_list():
for dirname, _, filenames in os.walk(dir_path):
for filename in filenames:
print(os.path.join(dirname, filename))
if __name__ == '__main__':
FIGSIZE = 6.0
SPACING = 0.1
rows = 2
cols = 2
cam = cv2.VideoCapture(0)
image_1= None
last_time = time.time()
#cam.set(cv2.CAP_PROP_FPS, 100)
#cam.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
#cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
#camera_info(cam)
ret, frame = cam.read()
if frame is None:
print("No Camera")
exit(1)
#subplot=(rows,cols,1)
#plt.figure(figsize=(FIGSIZE,FIGSIZE/cols*rows))
print(tf.__version__)
path = os.path.dirname(os.path.realpath(__file__))
dataset_path = os.path.join(path,'DATA','LEGO-brick-images','Brick_List.csv')
df = pd.read_csv(dataset_path, skipinitialspace=True, skip_blank_lines=True,encoding='utf-8', index_col='Brick')
Bricks = [( str(f)) for f in df.index]
print(len(Bricks))
model = load_model()
#sou_path = os.path.join(path,'DATA','archive')
#filenames = tf.io.gfile.glob(sou_path+'/*/*.png')
##for file_path in filenames:
while(True):
ret, frame = cam.read()
if ret is False:
print("No Picture")
break
cam_frame = frame
gray = cv2.cvtColor(cam_frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
if image_1 is None:
image_1 = gray
continue
frameDelta = cv2.absdiff(image_1, gray)
#cv2.imshow("Frame Delta", frameDelta)
#print(np.argmax(frameDelta, axis=-1))
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
diff =np.concatenate(thresh).sum()
print('loop took {} seconds'.format(time.time()-last_time))
last_time = time.time()
cutout_frame = cam_frame
cam_frame = cutout_frame
image = cutout_frame
key = cv2.waitKey(1) & 0xFF
blank =True
if key == ord("q"):
break
if diff >100:
thresh = cv2.dilate(thresh, None, iterations=2)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(cnts)
image_1= gray
biggest_contour =0
for c in contours:
#print(cv2.contourArea(c))
if cv2.contourArea(c) > biggest_contour:
biggest_contour = cv2.contourArea(c)
(x, y, w, h) = cv2.boundingRect(c)
continue
blank =True
if biggest_contour>0:
cv2.rectangle(cam_frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
print('Box x={} y={} w={} h={}'.format(x,y,w,h))
if w*h > 2000:
image = cam_frame[y:y+h, x:x+w]
blank =False
if ( blank == False ):
image_from_array = Image.fromarray(image, 'RGB')
size_image = image_from_array.resize((224,224))
p = np.expand_dims(size_image, 0)
dir_path = os.path.dirname(os.path.realpath(__file__))
img = tf.cast(p, tf.float32)
probabilitie = model.predict(img)
prediction = np.argmax(probabilitie, axis=-1)
print(type(prediction))
print(Bricks[int(prediction)])
title = "Predict="+Bricks[prediction[0]]
print(title)
path_pic = os.path.join(path,'DATA','LEGO-brick-images')
filenames_match = tf.io.gfile.glob(path_pic+'/'+Bricks[prediction[0]]+'/*.jpg')
print(prediction)
image2 = cv2.imread(filenames_match[0])
#plt.subplot(*subplot)
#cid = fig.canvas.mpl_connect('button_press_event', onclick)
fig, ax = plt.subplots(2,2)
plt.title('Source')
ax[0,0].imshow(frame)
ax[0,0].axis('off')
#ax[0,1].title('Cut out')
ax[0,1].axis('off')
ax[0,1].imshow(cam_frame)
ax[1,0].axis('off')
#ax[1,0].title('Box')
ax[1,0].axis('off')
ax[1,0].imshow(image)
ax[1,0].axis('off')
#ax[1,1].title(title)
ax[1,1].axis('off')
ax[1,1].imshow(image2)
#plt.ioff()
#plt.close()
plt.show()
plt.pause(1)
#plt.tight_layout()
#plt.subplots_adjust(wspace=SPACING, hspace=SPACING)
#screen_record()