/
sliding_window.py
160 lines (129 loc) · 5.35 KB
/
sliding_window.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
# add the project directory to python path
import sys
sys.path.__add__(["."])
# force tensorflow to use CPU
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
# import the necessary packages
from tensorflow.keras.applications.resnet import ResNet50, preprocess_input
from tensorflow.keras.applications.imagenet_utils import decode_predictions
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from imutils.object_detection import non_max_suppression
from object_detection.helpers import sliding_window
from object_detection.helpers import pyramid
from cv2 import cv2
import numpy as np
import argparse
import imutils
import time
# construct an argument parser to parse the command line arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "path to input image")
ap.add_argument("-s", "--size", type = str, default = "(200, 150)", help = "ROI size (in pixels)")
ap.add_argument("-c", "--min_conf", type = float, default = 0.9, help = "minimum probability to filter weak detections")
ap.add_argument("-v", "--visualize", type = int, default = -1,
help = "whether or not to show extra visualizations for debugging")
args = vars(ap.parse_args())
# initialize some useful constants
WIDTH = 600
PYR_SCALE = 1.5
WIN_STEP = 16
ROI_SIZE = eval(args["size"])
INPUT_SIZE = (224, 224)
# read the input image and resize it to the appropriate size
orig = cv2.imread(args["image"])
orig = imutils.resize(orig, width = WIDTH)
(H, W) = orig.shape[:2]
# initialize the image pyramid
pyr = pyramid(orig, scale = PYR_SCALE, min_size = ROI_SIZE)
# initialize lists to hold the ROIs and their locations
rois = []
locs = []
# time how long it takes to generate the rois
start = time.time()
# loop through the pyramids and slide a window through the images
for img in pyr:
# calculate the scale (to upsample bbox locations)
scale = W / float(img.shape[1])
# slide the window through the current img
for (x, y, orig_roi) in sliding_window(img, WIN_STEP, ROI_SIZE):
# calculate the scaled dimensions
x = int(x * scale)
y = int(y * scale)
w = int(ROI_SIZE[0] * scale)
h = int(ROI_SIZE[1] * scale)
# preprocess the ROI
roi = cv2.resize(orig_roi, INPUT_SIZE)
roi = img_to_array(roi)
roi = preprocess_input(roi)
# update the ROI list along with their correspoding coordinates
rois.append(roi)
locs.append((x, y, x + w, y + h))
# check if the sliding process is to be visualized
if args["visualize"] > 0:
# clone the original image
clone = orig.copy()
cv2.rectangle(clone, (x, y), ((x + w), (y + h)), (0, 0, 255), 2)
# show the visualization and the current ROI
cv2.imshow("Visualization", clone)
cv2.imshow("ROI", roi)
cv2.waitKey(0)
# calculate how long the sliding window process took
end = time.time()
print(f"[INFO] looping over the pyramid took {(end - start):.5f} seconds")
# convert the rois to a numpy array
rois = np.array(rois)
# initialize the pretrained model
model = ResNet50(weights = 'imagenet', input_shape = (224, 224, 3))
# classify each of the rois and calculate how long the process took
print("[INFO] classifying ROIs...")
start = time.time()
preds = model.predict(rois)
end = time.time()
print(f"[INFO] classifying ROIs took {(end - start):.5f} seconds")
# decode the predictions
preds = decode_predictions(preds, top = 1)
# initialize a dictionary that maps class -> ROIs
labels = {}
# loop through the predictions
for i, pred in enumerate(preds):
# grab the prediction information for the current ROI
(synset_id, label, prob) = pred[0]
# filter out the weak predictions
if prob > args["min_conf"]:
# grab the corresponding bounding box
bbox = locs[i]
# add this bbox to the list of predictions for the current label
L = labels.get(label, [])
L.append((bbox, prob))
labels[label] = L
# loop over the labels for each of the detected objects in the image
for label in labels.keys():
# clone the original image (to draw on it)
print(f"[INFO] showing results for {label}")
clone = orig.copy()
# loop over the bounding boxes for the current label
for (bbox, prob) in labels[label]:
# draw the bounding box
(startx, starty, endx, endy) = bbox
cv2.rectangle(clone, (startx, starty), (endx, endy), (0, 0, 255), 2)
# show the results before applying non max suppresion
cv2.imshow("Before Non-Max", clone)
cv2.waitKey(0)
# extract the bounding boxes and class probabilites and apply non max supression
boxes = np.array([p[0] for p in labels[label]])
probs = np.array([p[1] for p in labels[label]])
boxes = non_max_suppression(boxes, probs)
# clone the original image again and loop over all the remaining boxes
clone = orig.copy()
for (startx, starty, endx, endy) in boxes:
# draw the bounding box
cv2.rectangle(clone, (startx, starty), (endx, endy), (0, 0, 255), 2)
# write the class label
y = starty - 10 if starty - 10 > 10 else starty + 10
cv2.putText(clone, label, (startx, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 1)
# show the results after non max suppresion
cv2.imshow("After Non-Max", clone)
cv2.waitKey(0)
# close all the windows
cv2.destroyAllWindows()