-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
53 lines (44 loc) · 1.46 KB
/
model.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
from vortex.runtime.helper import InferenceHelper
import numpy as np
import matplotlib.pyplot as plt
import cv2
from PIL import Image
size = (480,640)
def test_image(filename, as_numpy=True):
img = Image.open(filename)
if as_numpy:
img = np.array(img)
img = np.expand_dims(img,0)
return img
def inference(input_path, filename='model/aquanet.onnx'):
# prepare test image, as NHWC with BGR channel order
img = test_image(input_path)
img = np.flip(img,-1)
img = cv2.resize(img[0],size[::-1])[None,...]
# construct runtime model with visualization
kwargs = dict(
model_path=filename,
runtime='cpu',
)
rt = InferenceHelper.create_runtime_model(**kwargs)
# prepare arguments for inference,
# note that the name 'score_threshold'
# will be forwarded to the actual runtime model
# hence the name should match the actual model itself.
kwargs = dict(
score_threshold=0.3,
visualize=True,
)
result = rt(img,**kwargs)
print(result['prediction'])
if 'visualization' in result:
# visualize first batch
visual = result['visualization'][0]
visual = np.flip(visual,2)
print(visual)
# cv2.imwrite('results/predictions.jpg', visual)
result = Image.fromarray((visual).astype(np.uint8), mode='RGB')
result.save('results/predictions.jpg')
# plt.imshow(visual)
# plt.show()
# inference('test_images/shark.jpeg')