File size: 4,491 Bytes
6ee5bc6
f4d3cdd
dd085d6
 
 
 
 
 
 
4ce3394
dd085d6
32a9efd
 
 
 
 
 
 
 
dd085d6
 
 
 
 
 
 
 
 
 
 
32a9efd
dd085d6
 
 
 
 
 
 
 
 
 
32a9efd
 
 
 
 
 
 
 
dd085d6
 
 
32a9efd
dd085d6
 
 
 
 
32a9efd
 
 
 
 
 
 
dd085d6
 
 
32a9efd
dd085d6
 
 
 
 
 
5489fde
32a9efd
7dcfb97
32a9efd
 
 
 
 
fc65f53
5489fde
dd085d6
f1a2fa1
32a9efd
 
 
 
 
f1a2fa1
32a9efd
 
f1a2fa1
f4d3cdd
32a9efd
 
 
 
65d10d5
32a9efd
 
 
f4d3cdd
 
5489fde
5ce69a8
dd085d6
f4d3cdd
dd085d6
6b64afb
f4d3cdd
dd085d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
os.system("pip install cython_bbox")
import gradio as gr
import tempfile
import track
import shutil
from pathlib import Path
from yolov5 import detect
from PIL import Image

# 目标检测
def Detect(image, image_type):
    if image_type == "红外图像":
        pt = "best.pt"
        cnf = "FLIR.yaml"
    else:
        pt = "yolov5s.pt"
        cnf = "coco128.yaml"

    # 创建临时文件夹
    temp_path = tempfile.TemporaryDirectory(dir="./")
    temp_dir = temp_path.name
    # 临时图片的路径
    temp_image_path = os.path.join(temp_dir, f"temp.jpg")
    # 存储临时图片
    img = Image.fromarray(image)
    img.save(temp_image_path)
    # 结果图片的存储目录
    temp_result_path = os.path.join(temp_dir, "tempresult")
    # 对临时图片进行检测
    detect.run(source=temp_image_path, data=f"test_image/{cnf}", weights=f"weights/{pt}", project=f'./{temp_dir}',name = 'tempresult', hide_conf=False,  conf_thres=0.35)
    # 结果图片的路径
    temp_result_path = os.path.join(temp_result_path, os.listdir(temp_result_path)[0])
    # 读取结果图片
    result_image = Image.open(temp_result_path).copy()
    # 删除临时文件夹
    temp_path.cleanup()
    return result_image

# 候选图片
example_image= [
    ["./test_image/1.jpg", "红外图像"],
    ["./test_image/2.jpg", "红外图像"],
    ["./test_image/3.jpg", "红外图像"],
    ["./test_image/8.jpg", "红外图像"],
    ["./test_image/5.jpg", "红外图像"],
    # ["./test_image/6.jpg]", "红外图像"],
    ["./test_image/4.jpg", "可见光图像"],
    ["./test_image/7.jpg", "可见光图像"]
    ]

# 目标追踪
def Track(video, video_type, tracking_method):
    # 存储临时视频的文件夹
    temp_dir = "./temp"
    # 先清空temp文件夹
    shutil.rmtree("./temp")
    os.mkdir("./temp")

    # 获取视频的形式
    if video_type == "红外视频":
        pt = "best2.pt"
    else:
        pt = "yolov5s.pt"

    # 获取视频的名字
    video_name = os.path.basename(video)
    # 对视频进行检测
    track.run(source=video, yolo_weights=Path(f"weights/{pt}"),reid_weights=Path("weights/osnet_x0_25_msmt17.pt") , project=Path(f'./{temp_dir}'), name = 'tempresult', tracking_method=tracking_method)
    # 结果视频的路径
    temp_result_path = os.path.join(f'./{temp_dir}', "tempresult", video_name)
    # 返回结果视频的路径
    return temp_result_path

# 候选视频
example_video= [
    ["./video/5.mp4", "红外视频", "bytetrack"],
    ["./video/bicyclecity.mp4","红外视频", "bytetrack"],
    ["./video/9.mp4", "红外视频", "bytetrack"],
    ["./video/8.mp4", "红外视频", "strongsort"],
    ["./video/4.mp4", "红外视频", "bytetrack"],
    ["./video/car.mp4", "红外视频", "strongsort"],
    ["./video/caixukun.mp4", "可见光视频", "bytetrack"],
    ["./video/palace.mp4", "可见光视频", "bytetrack"],
    ]

iface_Image = gr.Interface(fn=Detect,
                           inputs=[gr.Image(label="上传一张图像(jpg格式)"),
                                   gr.Radio(["红外图像", "可见光图像"],
                                            label="image type",
                                            info="选择图片的形式",
                                            value="红外图像")],
                           outputs=gr.Image(label="检测结果"),
                           examples=example_image
                           )

iface_video = gr.Interface(fn=Track,
                           inputs=[gr.Video(label="上传一段视频(mp4格式)"),
                                   gr.Radio(["红外视频", "可见光视频"],
                                            label="video type",
                                            info="选择视频的形式",
                                            value="红外视频"),
                                   gr.Radio(["bytetrack", "strongsort"],
                                            label="track methond",
                                            info="建议使用bytetrack, strongsort在cpu上运行很慢",
                                            value="bytetrack")],
                           outputs=gr.Video(label="追踪结果"),
                           examples=example_video
                          )

demo = gr.TabbedInterface([iface_video, iface_Image], tab_names=["目标追踪", "目标检测"], title="红外目标检测追踪")


demo.launch()