forked from wffjwbbf/ComDesignProject
123 lines
5.1 KiB
Python
123 lines
5.1 KiB
Python
import torch
|
|
from torchvision import transforms
|
|
import numpy as np
|
|
import cv2
|
|
import os
|
|
|
|
import param
|
|
from detect import YOLOv7
|
|
from deep_sort import deep_sort as dsort
|
|
from test_model import ReIDNet
|
|
|
|
def get_img_test(raw_img:np.array):
|
|
raw_transform = transforms.Compose([transforms.ToPILImage(),
|
|
transforms.Resize((360,640)),
|
|
transforms.Pad((0,(640-360)//2)),])
|
|
return raw_transform(raw_img)
|
|
|
|
def opencv_box_plot(img:cv2.Mat,pred_img:np.array):
|
|
pred_img = pred_img.astype(np.uint)
|
|
print(type(img))
|
|
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)
|
|
print(type(img))
|
|
cv2.imshow("test",img)
|
|
cv2.waitKey(0)
|
|
cv2.imwrite("result.png",img)
|
|
|
|
routine = {}
|
|
|
|
def opencv_sort_plot(img:cv2.Mat,pred_yolo:np.array,pred_sort:np.array):
|
|
for info in pred_yolo:
|
|
x1,y1,x2,y2 = int(info[0]),int(info[1]),int(info[2]),int(info[3])
|
|
cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),1)
|
|
for info in pred_sort:
|
|
x1,y1,x2,y2,id = info[0],info[1],info[2],info[3],info[4]
|
|
cxy = (int((x1+x2)/2),int((y1+y2)/2))
|
|
color = (int(id%3*100),int(id%4*75),int(id%5*50))
|
|
cv2.putText(img,str(id),cxy, cv2.FONT_HERSHEY_PLAIN, 1.0, color, 2)
|
|
if routine.get(id) is None:
|
|
routine[id] = [cxy]
|
|
else:
|
|
for i in range(1,len(routine[id])):
|
|
cv2.line(img,routine[id][i-1],routine[id][i],color,1)
|
|
cv2.line(img,routine[id][-1],cxy,color,1)
|
|
routine[id].append(cxy)
|
|
cv2.imshow("test",img)
|
|
cv2.waitKey(1)
|
|
return img
|
|
|
|
def extractidfeature(id_path:str,extractor):
|
|
img_id = cv2.imread(id_path)
|
|
tensor_id = extractor([img_id])
|
|
return tensor_id
|
|
|
|
# example
|
|
if __name__ == '__main__':
|
|
my_option = param.Parameters()
|
|
print(torch.__version__)
|
|
print(my_option.device)
|
|
|
|
# dsort_path = "./deep_sort/deep/checkpoint/market_bot_R50-ibn.pth"
|
|
ids = []
|
|
paths = os.listdir(f"query/cam{my_option.query_index}/")
|
|
|
|
model = YOLOv7(my_option) # load model in head
|
|
deepsort = dsort.DeepSort(model_path=my_option.weights_reid,model_config="self",use_cuda=(torch.device("cuda:0") == my_option.device))
|
|
|
|
# get query feature id
|
|
for p in paths:
|
|
ids.append(extractidfeature(f"query/cam{my_option.query_index}/"+p,deepsort.extractor))
|
|
|
|
with torch.no_grad():
|
|
sources = cv2.VideoCapture(f"video/cam{my_option.gallary_index}.mp4")
|
|
target = cv2.VideoWriter("output.mp4",cv2.VideoWriter_fourcc('m', 'p', '4', '2'),24,(640,640))
|
|
frame_counter = 0
|
|
while True:
|
|
ret,frame = sources.read()
|
|
if ret is False:
|
|
break
|
|
img = get_img_test(frame) # get image as torch.Tensor with size of [1,1,640,640]
|
|
img_tensor = transforms.ToTensor()(img).unsqueeze(dim=0).to(my_option.device)
|
|
result = model.detect(img_tensor) # get the sequence of result
|
|
result = result[0].detach().cpu().numpy() # the single img is index 0
|
|
bbox_xywhs = []
|
|
confs = []
|
|
for xyxycc in result:
|
|
xywh = deepsort._xyxy_to_xywh(xyxycc[0:4])
|
|
conf = xyxycc[4]
|
|
clas = int(xyxycc[5])
|
|
bbox_xywhs.append(xywh[:])
|
|
confs.append(conf)
|
|
dpsort, features = deepsort.update(bbox_xywh=np.array(bbox_xywhs),confidences=np.array(confs),ori_img=np.array(img))
|
|
img_drawing = np.array(img)
|
|
|
|
# ReID match
|
|
for i in range(len(features)):
|
|
feature_distances = []
|
|
for j in range(len(ids)):
|
|
feature_distances.append(np.mean(np.power(features[i]-ids[j],2)))
|
|
# print(similarity)
|
|
min_fdis = min(feature_distances)
|
|
if min_fdis < 0.2:
|
|
cx = int((result[i][0]+result[i][2])/2)
|
|
cy = int((result[i][1]+result[i][3])/2)
|
|
index_near = feature_distances.index(min_fdis)
|
|
color = (int(index_near%2*100),int(index_near%3*75),int(index_near%4*50))
|
|
cv2.putText(img_drawing,f"{paths[index_near]}",(cx-10,cy-10), cv2.FONT_HERSHEY_PLAIN, 1, color, 2)
|
|
cv2.putText(img_drawing,f"{min_fdis:>.4f}",(cx-10,cy-30), cv2.FONT_HERSHEY_PLAIN, 1, color, 2)
|
|
print(index_near)
|
|
# print(f"result:{result}")
|
|
# print(f"dpsort:{dpsort}")
|
|
|
|
# Final plot and show video
|
|
frame_t = opencv_sort_plot(img_drawing,result,dpsort)
|
|
target.write(frame_t)
|
|
frame_counter += 1
|
|
|
|
# save the video and end the program
|
|
target.release()
|
|
# 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 |