-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinference.py
58 lines (44 loc) · 1.67 KB
/
inference.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
import tensorflow as tf
import matplotlib.pyplot as plt
import json
import requests
import pymysql
import pandas as pd
import base64
import cv2
from utils.utils import anchor_to_coordinate
def main():
conn = pymysql.connect(host='mysql', user='root', charset='utf8')
cursor = conn.cursor(pymysql.cursors.DictCursor)
sql = "USE dacon;"
cursor.execute(sql)
sql = '''SELECT image_id, image FROM images'''
cursor.execute(sql)
result = cursor.fetchall()
df = pd.json_normalize(result)
img = base64.decodebytes(df.iloc[0]['image'])
img = tf.image.decode_jpeg(img, channels=3)
img = tf.expand_dims(tf.image.resize(img, [432, 768])/255, 0)
data = json.dumps({"signature_name": "serving_default", "instances": img.numpy().tolist()})
headers = {"content-type": "application/json"}
json_response = requests.post('http://host.docker.internal:8501/v1/models/frcnn:predict', data=data, headers=headers)
predictions = json.loads(json_response.text)['predictions']
max_output_size = 3
img_ = img[0].numpy().copy()
scores_order = tf.argsort(predictions[0]['output_1'], direction='DESCENDING', axis=0)
boxes = tf.squeeze(tf.gather(predictions[0]['output_2'], scores_order))
boxes = boxes[boxes[:, 2] > 16]
boxes = boxes[boxes[:, 3] > 16][:max_output_size]
boxes = tf.math.reduce_mean(boxes, axis=0)
anchor = anchor_to_coordinate(boxes.numpy())
cv2.rectangle(
img_,
(int(anchor[0]), int(anchor[2])), (int(anchor[1]), int(anchor[3])),
(1, 0, 0),
thickness=1
)
plt.imshow(img_)
plt.axis('off')
plt.savefig('./test.png', dpi=200)
if __name__ == '__main__':
main()