|
2 | 2 | import cv2 as cv
|
3 | 3 | import numpy as np
|
4 | 4 | import scipy.io
|
5 |
| -from utils import load_model |
| 5 | +from utils import load_model, draw_str |
6 | 6 | import argparse
|
7 | 7 | import keras.backend as K
|
| 8 | +import os |
| 9 | +import random |
8 | 10 |
|
9 | 11 |
|
10 | 12 | if __name__ == '__main__':
|
|
15 | 17 | class_names = cars_meta['class_names'] # shape=(1, 196)
|
16 | 18 | class_names = np.transpose(class_names)
|
17 | 19 |
|
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 | + |
34 | 41 |
|
35 | 42 | K.clear_session()
|
36 | 43 |
|
0 commit comments