|
import torch |
|
from PIL import Image |
|
import cv2 |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
import torchvision.transforms as T |
|
|
|
def inference_and_save(model, input_image_path, output_image_path, threshold=0.3): |
|
|
|
img = Image.open(input_image_path).convert("RGB") |
|
img_tensor = T.ToTensor()(img).unsqueeze(0) |
|
|
|
|
|
with torch.no_grad(): |
|
predictions = model(img_tensor.to(torch.device('cpu'))) |
|
|
|
|
|
img_np = np.array(img) |
|
img_np = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR) |
|
|
|
for i, box in enumerate(predictions[0]['boxes']): |
|
score = predictions[0]['scores'][i].item() |
|
if score > threshold: |
|
x1, y1, x2, y2 = map(int, box) |
|
label = predictions[0]['labels'][i].item() |
|
cv2.rectangle(img_np, (x1, y1), (x2, y2), (0, 255, 0), 2) |
|
cv2.putText(img_np, f'Label: {label} Score: {score:.2f}', (x1, y1 - 10), |
|
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) |
|
|
|
|
|
cv2.imwrite(output_image_path, img_np) |
|
print(f'Result saved at {output_image_path}') |
|
|
|
|
|
input_image_path = 'test_video.mp4' |
|
output_image_path = 'r.mp4' |
|
|
|
|
|
model_path = '15road_best_model.pt' |
|
model = torch.load(model_path, map_location=torch.device('cpu')) |
|
model.eval() |
|
|
|
|
|
inference_and_save(model, input_image_path, output_image_path, threshold=0.3) |
|
|