-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAIDetector_pytorch.py
151 lines (118 loc) · 4.57 KB
/
AIDetector_pytorch.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from yolox.utils.boxes import postprocess
from yolox.data.data_augment import preproc
import torch
import torch.nn as nn
import numpy as np
from BaseDetector import baseDet
import os
from yolox.utils import fuse_model
from yolox.data.datasets import COCO_CLASSES
import math
def estimateSpeed(location1, location2, fps):
d_pixels = math.sqrt(math.pow(location2[0] - location1[0], 2) + math.pow(location2[1] - location1[1], 2))
# ppm = location2[2] / carWidht
ppm = 10 #$$$$
d_meters = d_pixels / ppm
#print("d_pixels=" + str(d_pixels), "d_meters=" + str(d_meters))
#fps = 10#$$$$
print(fps)
speed = d_meters * 10 * 3.6 #3600/1000
return speed
def select_device(device='', batch_size=None):
# device = 'cpu' or '0' or '0,1,2,3'
cpu = device.lower() == 'cpu'
if cpu:
os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False
elif device: # non-cpu device requested
os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable
# check availability
assert torch.cuda.is_available(), f'CUDA unavailable, invalid device {device} requested'
cuda = not cpu and torch.cuda.is_available()
if cuda:
devices = device.split(',') if device else range(torch.cuda.device_count()) # i.e. 0,1,6,7
n = len(devices) # device count
if n > 1 and batch_size: # check batch_size is divisible by device_count
assert batch_size % n == 0, f'batch-size {batch_size} not multiple of GPU count {n}'
return torch.device('cuda:0' if cuda else 'cpu')
class Detector(baseDet):
def __init__(self):
super(Detector, self).__init__()
self.build_config()
self.mdepth = 0.33
self.mwidth = 0.50
self.confthre=0.5
self.nmsthre=0.65
self.test_size=(640, 640)
self.rgb_means = (0.485, 0.456, 0.406)
self.std = (0.229, 0.224, 0.225)
self.init_model()
def init_model(self):
from yolox.models import YOLOX, YOLOPAFPN, YOLOXHead
def init_yolo(M):
for m in M.modules():
if isinstance(m, nn.BatchNorm2d):
m.eps = 1e-3
m.momentum = 0.03
if getattr(self, "model", None) is None:
in_channels = [256, 512, 1024]
backbone = YOLOPAFPN(self.mdepth, self.mwidth, in_channels=in_channels)
head = YOLOXHead(80, self.mwidth, in_channels=in_channels)
model = YOLOX(backbone, head)
model.apply(init_yolo)
model.head.initialize_biases(1e-2)
self.weights = 'yolox_s.pth'
self.device = '0' if torch.cuda.is_available() else 'cpu'
self.device = select_device(self.device)
ckpt = torch.load(self.weights)
# load the model state dict
model.load_state_dict(ckpt["model"])
model.to(self.device).eval()
model = fuse_model(model)
self.m = model
self.names = COCO_CLASSES
self.num_classes = len(self.names)
def preprocess(self, img):
img_info = {"id": 0}
img_info["file_name"] = None
height, width = img.shape[:2]
img_info["height"] = height
img_info["width"] = width
img_info["raw_img"] = img
img, ratio = preproc(img, self.test_size, self.rgb_means, self.std)
img_info["ratio"] = ratio
img = torch.from_numpy(img).unsqueeze(0)
if torch.cuda.is_available():
img = img.cuda()
return img_info, img
def detect(self, im):
img_info, img = self.preprocess(im)
outputs = self.m(img)
outputs = postprocess(
outputs, self.num_classes, self.confthre, self.nmsthre
)[0]
pred_boxes = []
ratio = img_info["ratio"]
img = img_info["raw_img"]
boxes = outputs[:, 0:4]
# preprocessing: resize
boxes /= ratio
cls_ids = outputs[:, 6]
scores = outputs[:, 4] * outputs[:, 5]
# 本次获取的所有框格数据的逐一标注
for i in range(len(boxes)):
box = boxes[i].cpu()
lbl = self.names[int(cls_ids[i])]
if lbl == 'truck' or 'bus':
lbl = 'car'
conf = scores[i]
if conf < self.confthre:
continue
x1 = int(box[0])
y1 = int(box[1])
x2 = int(box[2])
y2 = int(box[3])
pred_boxes.append(
(x1, y1, x2, y2, lbl, conf))
return im, pred_boxes
if __name__ == '__main__':
det = Detector()