-
Notifications
You must be signed in to change notification settings - Fork 25
/
rcnn_predict.py
107 lines (95 loc) · 3.27 KB
/
rcnn_predict.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
import selectivesearch.selectivesearch
from all_config import *
from image_utils.image_utils import *
from keras.models import load_model
import os
img_rows, img_cols = IMG_ROW, IMG_COL
model = load_model(model_name)
model._make_predict_function()
def get_prediction(img_binary, rects):
"""
predict roi of images, gives two scores for both class
:param img_binary: image of binary type
:param rects: rects of proposals
:return: rects,score list of rects
"""
rectangles = []
score_list = []
img_binary = cv2.cvtColor(img_binary, cv2.COLOR_BGR2GRAY)
for rect in rects:
roi = get_roi_image(img_binary, rect)
roi = cv2.resize(roi, (img_rows, img_cols))
score = model.predict(numpy.asarray([roi], numpy.float32).reshape(1, img_rows, img_cols, 1) / 255)
if score[0][1] > 0.8:
# print(score)
rectangles.append(rect)
score_list.append(score[0][1])
return rectangles, score_list
def image_view(image):
"""
view predicted image on window of cv2.show()
:param image: image to show
:return:
"""
show_width = 320
img = image
if image.shape[1] > show_width:
scale = show_width/image.shape[1]
img = cv2.resize(image, (0, 0), fx=scale, fy=scale)
cv2.imshow("img", img)
cv2.waitKey(0)
def get_proposals(img):
"""
show generated proposals on input image
:param img: input image
:return:
"""
img = cv2.imread(img)
scale = 800 / img.shape[1]
img = cv2.resize(img, (0, 0), fx=scale, fy=scale)
gray, binary = get_binary_image(img)
mg_lbl, regions = selectivesearch.selective_search(binary, SCALE, SIGMA, MIN_SIZE)
regions = get_proposal(regions, img.shape)
print("proposals:", len(regions))
cv2.drawContours(binary, regions, -1, (255, 145, 30), 2)
image_view(binary)
def model_predict(img_file, view):
"""
:param img_file: image file path
:param view: true if you want show the result
:return:
"""
res_obj = {
"score": 0,
"position": ""
}
prediction = "predict_image.png"
img = cv2.imread(img_file) if view else cv2.imdecode(numpy.fromstring(img_file, numpy.uint8), 1)
scale = 500 / img.shape[1]
img = cv2.resize(img, (0, 0), fx=scale, fy=scale)
gray, binary = get_binary_image(img)
if get_gray_score(binary):
mg_lbl, regions = selectivesearch.selective_search(binary, SCALE, SIGMA, MIN_SIZE)
regions = get_proposal(regions, img.shape)
rectangles, score_list = get_prediction(binary, regions)
if len(score_list) > 0:
score = round(max(score_list), 2)
rect = rectangles[score_list.index(max(score_list))]
position = get_pos(rect, scale)
res_obj["score"] = float(score)
res_obj["position"] = position
cv2.drawContours(img, [rect], -1, (255, 145, 30), 2)
if not os.path.exists(PREDICT_PATH):
os.mkdir(PREDICT_PATH)
cv2.imwrite(PREDICT_PATH+prediction, img)
if view:
print(res_obj)
print('result saved:'+PREDICT_PATH+prediction)
image_view(img)
return res_obj
"""
:parameter image path
"""
if __name__ == "__main__":
model_predict("image.png", view=True)
# get_proposals("image.png")