-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectRecognitionFramework.py
236 lines (188 loc) · 8.4 KB
/
ObjectRecognitionFramework.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
import os
import sys
import random
import math
import numpy as np
import cv2
import json
# Root directory of the project
ROOT_DIR = os.path.abspath("./")
# Import Mask RCNN
sys.path.append(ROOT_DIR) # To find local version of the library
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "Mask_RCNN-master/samples/coco/")) # To find local version
from samples.coco import coco
def random_colors(N):
np.random.seed(1)
colors = [tuple(255 * np.random.rand(3)) for _ in range(N)]
return colors
def apply_mask(image, mask, color, alpha=0.5):
"""apply mask to image"""
for n, c in enumerate(color):
image[:, :, n] = np.where(
mask == 1,
image[:, :, n] * (1 - alpha) + alpha * c,
image[:, :, n]
)
return image
def display_instances(image, boxes, masks, ids, names, scores, colors):
"""
take the image and results and apply the mask, box, and Label
"""
n_instances = boxes.shape[0]
if not n_instances:
print('NO INSTANCES TO DISPLAY')
else:
assert boxes.shape[0] == masks.shape[-1] == ids.shape[0]
for i in enumerate(n_instances):
if not np.any(boxes[i]):
continue
color = colors[ids[i]]
y1, x1, y2, x2 = boxes[i]
label = names[ids[i]]
score = scores[i] if scores is not None else None
caption = '{} {:.2f}'.format(label, score) if score else label
mask = masks[:, :, i]
image = apply_mask(image, mask, color)
image = cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
image = cv2.putText(
image, caption, (x1, y1), cv2.FONT_HERSHEY_COMPLEX, 0.7, color, 2
)
return image
class ObjectRecognitionFramework:
def __init__(self, config_path="config.json"):
'''# Root directory of the project
ROOT_DIR = os.path.abspath("../")
# Import Mask RCNN
sys.path.append(ROOT_DIR) # To find local version of the library
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "Mask_RCNN-master/samples/coco/")) # To find local version
import coco
'''
config_file = json.loads(open(config_path).read())
# Directory to save logs and trained model
MODEL_DIR = config_file.get("logs_path", "logs")
# Local path to trained weights file
COCO_MODEL_PATH = config_file.get("weights", "mask_rcnn_coco.h5")
# Directory of images to run detection on
#IMAGE_DIR = os.path.join(ROOT_DIR, "Mask_RCNN-master/images")
class InferenceConfig(coco.CocoConfig):
# Set batch size to 1 since we'll be running inference on
# one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
GPU_COUNT = 1
IMAGES_PER_GPU = 1
self.config = InferenceConfig()
self.model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=self.config)
# Load weights trained on MS-COCO
self.model.load_weights(COCO_MODEL_PATH, by_name=True)
self.class_colors = random_colors(81)
# COCO Class names
# Index of the class in the list is its ID. For example, to get ID of
# the teddy bear class, use: class_names.index('teddy bear')
self.class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
'kite', 'baseball bat', 'baseball glove', 'skateboard',
'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
'teddy bear', 'hair drier', 'toothbrush']
def input_image(self,image_dir):
# Load a random image from the images folder
file_names = next(os.walk(image_dir))[2]
image = cv2.imread(os.path.join(image_dir, random.choice(file_names)))
# Run detection
results = self.model.detect([image], verbose=1)
# Visualize results
r = results[0]
visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
self.class_names, r['scores'])
def input_single_image(self,image_dir, image_name):
image_dir+="/"+image_name
# Load a random image from the images folder
#file_names = next(os.walk(IMAGE_DIR))[2]
image = cv2.imread(os.path.join(image_dir))
# Run detection
results = self.model.detect([image], verbose=1)
# Visualize results
r = results[0]
visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
self.class_names, r['scores'])
def input_video(self, video_path):
cam = cv2.VideoCapture(video_path)
if (cam.isOpened()== False):
print("Error opening video stream or file")
try:
# creating a folder named data
if not os.path.exists('video'):
os.makedirs('video')
# if not created then raise error
except OSError:
print ('Error: Creating directory of data')
# frame
currentframe = 0
while(True):
# reading from frame
ret,frame = cam.read()
if ret:
# if video is still left continue creating images
root_vid=ROOT_DIR+"/video"
name = root_vid+"/frame" + str(currentframe) + ".jpg"
print ('Creating...' + name)
frame = display_instances(frame, r['rois'], r['masks'], r['class_ids'],
self.class_names, r['scores'], self.class_colors)
# writing the extracted images self.class_colors
cv2.imwrite(name, frame)
# increasing counter so that it will
# show how many frames are created
currentframe += 1
else:
break
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
def input_move(self,VIDEO_DIR):
capture = cv2.VideoCapture(VIDEO_DIR)
# Check if camera opened successfully
if (capture.isOpened()== False):
print("Error opening video stream or file")
VIDEO_SAVE_DIR=ROOT_DIR+"/"
width = int(capture.get(3))
height = int(capture.get(4))
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'MPEG')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (width,height))
while(capture.isOpened()):
ret, frame = capture.read()
if ret==True:
#frame = cv2.flip(frame,0)
results = self.model.detect([frame], verbose=1)
# Visualize results
r = results[0]
#frame = (frame, r['rois'], r['masks'], r['class_ids'], self.class_names, r['scores'])
display_instances(frame, r['rois'], r['masks'], r['class_ids'],
self.class_names, r['scores'], self.class_colors)
# write the frame
#out.write(frame)
#cv2.imshow('frame',frame)
#if cv2.waitKey(1) & 0xFF == ord('q'):
# break
else:
break
# Release everything if job is finished
capture.release()
out.release()
cv2.destroyAllWindows()