-
Notifications
You must be signed in to change notification settings - Fork 8
/
guess.py
47 lines (38 loc) · 1.34 KB
/
guess.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
# import the necessary packages
import json
import os
import random
import numpy as np
import cv2 as cv
import keras.backend as K
import numpy as np
import scipy.io
from PIL import Image
import requests
from io import BytesIO
from resnets_utils import load_model
if __name__ == '__main__':
img_width, img_height = 224, 224
model = load_model_and_weights()
model.load_weights('./weights.best.hdf5')
cars_meta = scipy.io.loadmat('devkit/cars_meta')
class_names = cars_meta['class_names'] # shape=(1, 196)
class_names = np.transpose(class_names)
url = "https://picolio.auto123.com/12photo/bmw/2012-bmw-m3_2.jpg"
response = requests.get(url)
img = Image.open(BytesIO(response.content))
results = []
print('Processing image')
img = cv.resize(np.float32(img), (img_width, img_height), cv.INTER_CUBIC)
rgb_img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
rgb_img = np.expand_dims(rgb_img, 0)
preds = model.predict(rgb_img)
prob = np.max(preds)
class_id = np.argmax(preds)
text = ('Predict: {}, prob: {}'.format(class_names[class_id][0][0], prob))
results.append({'label': class_names[class_id][0][0], 'prob': '{:.4}'.format(prob)})
cv.imwrite('images/{}_out.png', img)
print(results)
with open('results.json', 'w') as file:
json.dump(results, file, indent=4)
K.clear_session()