forked from wffjwbbf/ComDesignProject
119 lines
4.8 KiB
Python
119 lines
4.8 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
|
|
|
|
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)
|
|
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)
|
|
|
|
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)
|
|
|
|
query_path = f"./mydataset/query/cam{my_option.query_index}/" # query from .jpg photos
|
|
gallary_path = f"mydataset/video/cam{my_option.gallary_index}.mp4" # source from mp4 via yolo
|
|
output_path = f"output{my_option.gallary_index}_{my_option.query_index}.mp4" # output file path
|
|
|
|
ids = []
|
|
paths = os.listdir(query_path)
|
|
|
|
# load detection model and deepsort model
|
|
model = YOLOv7(my_option)
|
|
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(query_path+p,deepsort.extractor))
|
|
|
|
with torch.no_grad():
|
|
sources = cv2.VideoCapture(gallary_path)
|
|
target = cv2.VideoWriter(output_path,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)))
|
|
|
|
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)
|
|
|
|
# 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() |