forked from wffjwbbf/ComDesignProject
38 lines
1.4 KiB
Python
38 lines
1.4 KiB
Python
import torch
|
|
from torchvision import transforms
|
|
import numpy as np
|
|
import cv2
|
|
|
|
import param
|
|
from detect import YOLOv7
|
|
|
|
def get_img_test(path_src:str):
|
|
raw_img = cv2.imread(path_src)
|
|
raw_transform = transforms.Compose([transforms.ToPILImage(),
|
|
transforms.Resize((360,640)),
|
|
transforms.Pad((0,(640-360)//2)),
|
|
transforms.ToTensor()])
|
|
return raw_transform(raw_img).unsqueeze(dim=0)
|
|
|
|
def opencv_box_plot(img:cv2.Mat,pred_img:np.array):
|
|
pred_img = pred_img.astype(np.uint)
|
|
|
|
for info in pred_img:
|
|
x1,y1,x2,y2 = info[0],info[1],info[2],info[3]
|
|
cv2.rectangle(img,(x1,y1-140),(x2,y2-140),(255,0,0),2)
|
|
|
|
cv2.imshow("test",img)
|
|
cv2.waitKey(0)
|
|
cv2.imwrite("result.png",img)
|
|
|
|
# example
|
|
if __name__ == '__main__':
|
|
my_option = param.Parameters()
|
|
model = YOLOv7(my_option) # load model in head
|
|
with torch.no_grad():
|
|
img = get_img_test("test.png") # get image as torch.Tensor with size of [1,1,640,640]
|
|
result = model.detect(img) # get the sequence of result
|
|
result = result[0].detach().cpu().numpy() # the single img is index 0
|
|
img_src = cv2.imread("test.png") # re-read for imshow
|
|
img_src = cv2.resize(img_src,(640,360))
|
|
opencv_box_plot(img_src,result) # show the result in picture |