-
Notifications
You must be signed in to change notification settings - Fork 0
/
objdetector.py
97 lines (80 loc) · 2.98 KB
/
objdetector.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
import argparse
import sys
sys.path.insert(0, 'Camshift') # 将'folder1'添加到搜索路径中
import torch
import numpy as np
from models.experimental import attempt_load
from utils.general import non_max_suppression, scale_coords
from utils.datasets import letterbox
from utils.torch_utils import select_device
from utils.general import (LOGGER, check_file, check_img_size, check_imshow, check_requirements, colorstr,
increment_path, non_max_suppression, print_args, scale_coords, strip_optimizer, xyxy2xywh)
import objtracker
import random
OBJ_LIST = ['car', 'bus', 'van', 'others','person']
class baseDet(object):
def __init__(self):
self.img_size = 640
self.threshold = 0.3
self.stride = 1
def build_config(self):
self.frameCounter = 0
def feedCap(self, im):
retDict = {
'frame': None,
'list_of_ids': None,
'obj_bboxes': []
}
self.frameCounter += 1
im, obj_bboxes, single_info = objtracker.update(self, im)
retDict['frame'] = im
retDict['obj_bboxes'] = obj_bboxes
return retDict, single_info
def init_model(self):
raise EOFError("Undefined model type.")
def preprocess(self):
raise EOFError("Undefined model type.")
def detect(self):
raise EOFError("Undefined model type.")
class Detector(baseDet):
def __init__(self):
super(Detector, self).__init__()
self.opt = None
self.build_config()
self.num_stop = 1 # 暂停与播放辅助信号,note:通过奇偶来控制暂停与播放
self.count = 0
self.flag = True
self.vid_writer = None
# 权重初始文件名
self.openfile_name_model = None
self.videoWriter = None
def preprocess(self, img):
img0 = img.copy()
img = letterbox(img, new_shape=self.img_size)[0]
img = img[:, :, ::-1].transpose(2, 0, 1)
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(self.device)
img = img.half() # 半精度
img /= 255.0 # 图像归一化
if img.ndimension() == 3:
img = img.unsqueeze(0)
return img0, img
def detect(self, im):
im0, img = self.preprocess(im)
pred = self.m(img, augment=False)[0]
pred = pred.float()
pred = non_max_suppression(pred, self.threshold, 0.4)
pred_boxes = []
for det in pred:
if det is not None and len(det):
det[:, :4] = scale_coords(
img.shape[2:], det[:, :4], im0.shape).round()
for *x, conf, cls_id in det:
lbl = self.names[int(cls_id)]
if not lbl in OBJ_LIST:
continue
x1, y1 = int(x[0]), int(x[1])
x2, y2 = int(x[2]), int(x[3])
pred_boxes.append(
(x1, y1, x2, y2, lbl, conf))
return im, pred_boxes