Skip to content

Commit 047e30d

Browse files
committed
update demo script
1 parent e063170 commit 047e30d

13 files changed

+33
-18
lines changed

demo.py

+24-17
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
import cv2 as cv
33
import numpy as np
44
import scipy.io
5-
from utils import load_model
5+
from utils import load_model, draw_str
66
import argparse
77
import keras.backend as K
8+
import os
9+
import random
810

911

1012
if __name__ == '__main__':
@@ -15,22 +17,27 @@
1517
class_names = cars_meta['class_names'] # shape=(1, 196)
1618
class_names = np.transpose(class_names)
1719

18-
ap = argparse.ArgumentParser()
19-
ap.add_argument("-i", "--image", help="path to the image file")
20-
args = vars(ap.parse_args())
21-
22-
filename = args["image"]
23-
if filename is None:
24-
filename = 'images/samples/07647.jpg'
25-
26-
bgr_img = cv.imread(filename)
27-
rgb_img = cv.cvtColor(bgr_img, cv.COLOR_BGR2RGB)
28-
rgb_img = np.expand_dims(rgb_img, 0)
29-
preds = model.predict(rgb_img)
30-
prob = np.max(preds)
31-
class_id = np.argmax(preds)
32-
print('class_name: ' + str(class_names[class_id][0][0]))
33-
print('prob: ' + str(prob))
20+
test_path = 'cars_test/'
21+
test_images = [f for f in os.listdir(test_path) if
22+
os.path.isfile(os.path.join(test_path, f)) and f.endswith('.jpg')]
23+
samples = random.sample(test_images, 10)
24+
25+
for i in range(len(samples)):
26+
image_name = samples[i]
27+
filename = os.path.join('data/test', image_name)
28+
print('Start processing image: {}'.format(filename))
29+
30+
orig_img = cv.imread(os.path.join(test_path, image_name))
31+
bgr_img = cv.imread(filename)
32+
rgb_img = cv.cvtColor(bgr_img, cv.COLOR_BGR2RGB)
33+
rgb_img = np.expand_dims(rgb_img, 0)
34+
preds = model.predict(rgb_img)
35+
prob = np.max(preds)
36+
class_id = np.argmax(preds)
37+
text = ('Predict: {}, prob: {}'.format(class_names[class_id][0][0], prob))
38+
draw_str(orig_img, (20, 20), text)
39+
cv.imwrite('images/{}_out.png'.format(i), orig_img)
40+
3441

3542
K.clear_session()
3643

images/0_out.png

373 KB
Loading

images/1_out.png

420 KB
Loading

images/2_out.png

506 KB
Loading

images/3_out.png

1.67 MB
Loading

images/4_out.png

98 KB
Loading

images/5_out.png

1.29 MB
Loading

images/6_out.png

496 KB
Loading

images/7_out.png

94.6 KB
Loading

images/8_out.png

971 KB
Loading

images/9_out.png

1020 KB
Loading

result.txt

Whitespace-only changes.

utils.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import cv2 as cv
2+
13
from resnet_152 import resnet152_model
24

35

@@ -8,4 +10,10 @@ def load_model():
810
num_classes = 196
911
model = resnet152_model(img_height, img_width, num_channels, num_classes)
1012
model.load_weights(model_weights_path, by_name=True)
11-
return model
13+
return model
14+
15+
16+
def draw_str(dst, target, s):
17+
x, y = target
18+
cv.putText(dst, s, (x + 1, y + 1), cv.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), thickness=2, lineType=cv.LINE_AA)
19+
cv.putText(dst, s, (x, y), cv.FONT_HERSHEY_PLAIN, 1.0, (255, 255, 255), lineType=cv.LINE_AA)

0 commit comments

Comments
 (0)