-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcvss.py
More file actions
183 lines (154 loc) · 5.95 KB
/
cvss.py
File metadata and controls
183 lines (154 loc) · 5.95 KB
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
# force tensorflow to use the CPU
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
# import the necessary packages
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.imagenet_utils import decode_predictions
from tensorflow.keras.applications.resnet import ResNet50, preprocess_input
from imutils.object_detection import non_max_suppression
from cv2 import cv2
import numpy as np
import argparse
import imutils
import time
import matplotlib.pyplot as plt
import imageio, os
from keras.models import load_model
import skimage.transform
import numpy as np
import selective_search as ss
import seaborn as sns
from keras.applications import VGG16
from keras import models
import tensorflow as tf
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("-m", "--mode", choices = ["fast", "slow"], required = True, help = "selective search mode")
ap.add_argument("-c", "--min_conf", type = float, default = 0.9, help = "threshold to filter out weak predictions")
args = vars(ap.parse_args())
# initialize necessary constants
WIDTH = 600
INPUT_SHAPE = (224, 224)
BS = 128
# spped up opencv using multiple threads
cv2.setUseOptimized(True)
cv2.setNumThreads(4)
# load the image and resize it to the appropriate dimensions
img = cv2.imread(args["image"])
img = imutils.resize(img, width = WIDTH)
# initialize the selective search object using the default parameters and set the input image
ss = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
ss.setBaseImage(img)
# switch to the appropriate mode
if args["mode"] == "fast":
ss.switchToSelectiveSearchFast()
else:
ss.switchToSelectiveSearchQuality()
# get the region proposals
start = time.time()
rects = ss.process()
end = time.time()
print(f"[INFO] total number of region proposals = {len(rects)}")
print(f"[INFO] selective search took {(end - start):0.5f} seconds")
def get_regions(img):
ss = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
ss.setBaseImage(img)
ss.switchToSelectiveSearchQuality()
rects = ss.process()
return rects
# initialize a list to store the regions of interest
rois = []
locs = []
# loop through the region proposals
for rect in rects:
# extract the ROI coordinates
x, y, w, h = rect
# extract the region of interest from the image
roi = img[y: y + h, x: x + w]
# preprocess the ROI
roi = cv2.resize(roi, INPUT_SHAPE, interpolation = cv2.INTER_AREA)
roi = img_to_array(roi)
roi = preprocess_input(roi)
# append the current ROI to the ROIs list
rois.append(roi)
# modify the bbox coordinates and append them to the locs list
locs.append((x, y, x + w, y + h))
# convert the ROI list to a numpy array
rois = np.array(rois)
def plot_cadidate_regions_in_training(image1,title):
fig = plt.figure(figsize=(12,12))
fig.subplots_adjust(hspace=0.0001,
wspace=0.0001,
left=0,right=1,bottom=0, top=1)
print(title)
nw, nh = 10, 10
count = 1
for irow in range(100):#np.random.choice(len(image1),nw*nh):
im = image1[irow]
ax = fig.add_subplot(nh,nw,count)
ax.imshow(im)
ax.axis("off")
count += 1
plt.show()
plot_cadidate_regions_in_training(rois,title="plot warped cadidate regions with a Cardf_anno_Car object intraining ")
# modelvgg16 = VGG16(include_top=True,weights='imagenet')
# model = models.Model(inputs = modelvgg16.inputs,
# outputs = modelvgg16.layers[-3].output)
# ## show the deep learning model
# model.summary()
# start = time.time()
# feature = model.predict(rois, verbose=1, batch_size=32)
# end = time.time()
# print("TIME TOOK: {:5.4f}MIN".format((end-start)/60.0))
# feature.shape
# def plt_rectangle(plt,label,x1,y1,x2,y2,color = "yellow", alpha=0.5):
# linewidth = 3
# if type(label) == list:
# linewidth = len(label)*3 + 2
# label = ""
# plt.text(x1,y1,label,fontsize=20,backgroundcolor=color,alpha=alpha)
# plt.plot([x1,x1],[y1,y2], linewidth=linewidth,color=color, alpha=alpha)
# plt.plot([x2,x2],[y1,y2], linewidth=linewidth,color=color, alpha=alpha)
# plt.plot([x1,x2],[y1,y1], linewidth=linewidth,color=color, alpha=alpha)
# plt.plot([x1,x2],[y2,y2], linewidth=linewidth,color=color, alpha=alpha)
# dir_result = "result"
# classifier = load_model(os.path.join(dir_result,"classifier.h5"))
# classifier.summary()
# y_pred = classifier.predict(feature)
# def plot_selected_regions_with_estimated_prob(y_pred,
# method="highest",
# upto=5):
# ## increasing order
# irows = np.argsort(y_pred[:,0])
# if method == "highest":
# irows = irows[::-1]
# count = 1
# const = 4
# fig = plt.figure(figsize=(5*const,np.ceil(upto/5)*const))
# fig.subplots_adjust(hspace=0.13,wspace=0.0001,
# left=0,right=1,bottom=0, top=1)
# for irow in irows:
# prob = y_pred[irow,0]
# r = rects[irow]
# origx , origy , width, height = r["rect"]
# ax = fig.add_subplot(np.ceil(upto/5),5,count)
# ax.imshow(img)
# ax.axis("off")
# plt_rectangle(ax,label="Hydrent",
# x1=origx,
# y1=origy,
# x2=origx + width,
# y2=origy+height,color = "yellow", alpha=0.5)
# #candidate_region = img[origy:origy + height,
# # origx:origx + width]
# #ax.imshow(candidate_region)
# ax.set_title("Prob={:4.3f}".format(prob))
# count += 1
# if count > upto:
# break
# plt.show()
# print("The most likely candidate regions")
# plot_selected_regions_with_estimated_prob(y_pred,method="highest",upto=10)
# print(y_pred)