-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_phone.py
213 lines (183 loc) · 6.63 KB
/
find_phone.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
#!/usr/bin/env python3.7
import os
import mrcnn
import cv2
from mrcnn.utils import Dataset, extract_bboxes
from mrcnn.config import Config
from mrcnn.model import MaskRCNN
from mrcnn.utils import compute_ap
from mrcnn.model import load_image_gt
from mrcnn.model import mold_image
import numpy
from numpy.core.numeric import ones
from pascal_voc_writer import Writer
from xml.etree import ElementTree
from numpy import zeros
from numpy import asarray
from numpy import expand_dims
from numpy import mean
from skimage.util import dtype
from matplotlib import pyplot
from matplotlib.patches import Rectangle
from phone_dataset import PhoneDataset
import skimage.io
import argparse
import sys
'''
Config Class for Prediction of Phone Dataset
@param Config: Config Instance from R-CNN class
'''
class PredictionConfig(Config):
NAME = "phone_cfg"
NUM_CLASSES = 1+1
GPU_COUNT = 1
IMAGES_PER_GPU = 1
'''
Plot Actual and Prediction of Images Side by Side
@param dataset: Image Dataset Class Object
@param model: R-CNN model
@param cfg: Config Object of PredictionConfig Class
@param n_images: No. of images to compare
'''
def plot_actual_vs_predicted(dataset, model, cfg, n_images=5):
# load image and mask
for i in range(n_images):
# load the image and mask
image = dataset.load_image(i)
mask, _ = dataset.load_mask(i)
# convert pixel values (e.g. center)
scaled_image = mold_image(image, cfg)
# convert image into one sample
sample = expand_dims(scaled_image, 0)
# make prediction
yhat = model.detect(sample, verbose=0)[0]
# define subplot
pyplot.subplot(n_images, 2, i*2+1)
# plot raw pixel data
pyplot.imshow(image)
pyplot.title('Actual')
# plot masks
for j in range(mask.shape[2]):
pyplot.imshow(mask[:, :, j], cmap='gray', alpha=0.3)
# get the context for drawing boxes
pyplot.subplot(n_images, 2, i*2+2)
# plot raw pixel data
pyplot.imshow(image)
pyplot.title('Predicted')
ax = pyplot.gca()
# get coordinates
y1, x1, y2, x2 = yhat['rois'][0]
# calculate width and height of the box
width, height = x2 - x1, y2 - y1
# create the shape
rect = Rectangle((x1, y1), width, height, fill=False, color='red')
# draw the box
ax.add_patch(rect)
# show the figure
pyplot.show()
'''
Evaluate Trained Model
@param dataset: Image Dataset Class Object
@param model: R-CNN model
@param cfg: Config Object of PredictionConfig Class
'''
def evaluate_model(dataset, model, cfg):
APs = list()
for image_id in dataset.image_ids:
image, image_meta, gt_class_id, gt_bbox, gt_mask = load_image_gt(dataset, cfg, image_id, use_mini_mask = False)
scaled_image = mold_image(image, cfg)
sample = expand_dims(scaled_image, 0)
yhat = model.detect(sample, verbose = 1)
r = yhat[0]
AP, _, _, _ = compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r["masks"])
APs.append(AP)
mAP = mean(APs)
return mAP
'''
Get X-Coordinate from Pixel Width of Image
@param img_w: Image Width
@param pix_x: x-coordinate in pixel
'''
def getCoordsfromPixelinX(img_w, pix_x):
x = pix_x/img_w
return x
'''
Get Y-Coordinate from Pixel Height of Image
@param img_h: Image Height
@param pix_y: y-coordinate in pixel
'''
def getCoordsfromPixelinY(img_h, pix_y):
y = pix_y/img_h
return y
'''
Predict Center of Phone from Input Image
@param image_filepath: Path to image file
@param model: Pretrained Model
@param cfg: Prediction Config Object
'''
def predict_phone_center(image_filepath, model, cfg):
image = skimage.io.imread(image_filepath)
yhat = model.detect([image], verbose=0)[0]
pyplot.imshow(image)
ax = pyplot.gca()
y1,x1,y2,x2 = yhat['rois'][0]
pyplot.plot((x1+x2)/2, (y1+y2)/2, 'ro')
width, height = x2 - x1, y2 - y1
rect = Rectangle((x1, y1), width, height, fill=False, color='red')
ax.add_patch(rect)
pyplot.savefig('prediction_result.png')
center_x = getCoordsfromPixelinX(image.shape[1],(x1+x2)/2)
center_y = getCoordsfromPixelinY(image.shape[0],(y1+y2)/2)
print(center_x, center_y)
def create_arg_parser():
parser = argparse.ArgumentParser(description='Image File Path')
parser.add_argument('inputFile', help='Path to Image File')
return parser
##############################################################################################
# Checking if Query Image File is Provided
##############################################################################################
image_file_path = ''
if len(sys.argv)>1:
image_filepath = sys.argv[1]
else:
print('No query image provided')
raise ValueError('Please Provide an Input Image File')
##############################################################################################
# Loading the Training Dataset
##############################################################################################
train_set = PhoneDataset()
train_set.load_dataset('dataset', is_train=True)
train_set.prepare()
##############################################################################################
# Loading the Test Dataset
##############################################################################################
test_set = PhoneDataset()
test_set.load_dataset('dataset', is_train=False)
test_set.prepare()
##############################################################################################
# Setting and Loading Model Parameters
##############################################################################################
# create config
cfg = PredictionConfig()
# define the model
model = MaskRCNN(mode='inference', model_dir='./', config=cfg)
# load model weights
cfg_folder = ''
for folder in os.listdir():
if 'phone_cfg' in folder:
cfg_folder = folder
model_path = cfg_folder + '/mask_rcnn_phone_cfg_0005.h5'
model.load_weights(model_path, by_name=True)
##############################################################################################
# Evaluating Model Performance
##############################################################################################
# evaluate model on training dataset
# train_mAP = evaluate_model(train_set, model, cfg)
# print("Train mAP: %.3f" % train_mAP)
# evaluate model on test dataset
# test_mAP = evaluate_model(test_set, model, cfg)
# print("Test mAP: %.3f" % test_mAP)
##############################################################################################
# Performing Prediction of I/P image
##############################################################################################
predict_phone_center(image_filepath, model, cfg)