Files
AiDA_Python/app/service/super_resolution/test.py

32 lines
1.1 KiB
Python
Raw Normal View History

2024-03-20 11:44:15 +08:00
import time
import cv2
import numpy as np
import torch
import tritonclient.http as httpclient
2024-03-21 11:12:01 +08:00
import tritonclient.grpc as grpcclient
2024-03-20 11:44:15 +08:00
from PIL import Image
2024-03-21 11:12:01 +08:00
triton_client = grpcclient.InferenceServerClient(url=f"10.1.1.150:7001")
2024-03-20 11:44:15 +08:00
2024-03-21 11:12:01 +08:00
sample = cv2.imread("1709713346.806274.png", cv2.IMREAD_COLOR).astype(np.float32) / 255.
2024-03-20 11:44:15 +08:00
sample = np.transpose(sample if sample.shape[2] == 1 else sample[:, :, [2, 1, 0]], (2, 0, 1))
sample = torch.from_numpy(sample).float().unsqueeze(0).numpy()
inputs = [
2024-03-21 11:12:01 +08:00
grpcclient.InferInput("input", sample.shape, datatype="FP32")
2024-03-20 11:44:15 +08:00
]
2024-03-21 11:12:01 +08:00
inputs[0].set_data_from_numpy(sample
# , binary_data=True
)
2024-03-20 11:44:15 +08:00
start_time = time.time()
2024-03-21 11:12:01 +08:00
results = triton_client.async_infer(model_name="super_resolution", inputs=inputs)
2024-03-20 11:44:15 +08:00
print(time.time() - start_time)
sr_output = torch.from_numpy(results.as_numpy(f"output"))
output = sr_output.data.squeeze().float().cpu().clamp_(0, 1).numpy()
if output.ndim == 3:
output = np.transpose(output[[2, 1, 0], :, :], (1, 2, 0)) # CHW-RGB to HCW-BGR
output = (output * 255.0).round().astype(np.uint8)
2024-03-21 11:12:01 +08:00
cv2.imshow("", output)
cv2.waitKey(0)