-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracking_process.py
148 lines (110 loc) · 4.61 KB
/
Tracking_process.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
from io import BytesIO
from confluent_kafka import Consumer, KafkaError, Producer, TopicPartition, OFFSET_END
from ultralytics import YOLO
# from ultralytics.utils.plotting import Annotator
import numpy as np
import cv2
import json
import time
from pathlib import Path
from boxmot import OCSORT, DeepOCSORT
from config import *
class KafkaHumanDetection:
def __init__(self, bootstrap_servers=BOOTSTRAP_SERVERS, detection_topic='Frames', result_topic='Bboxes', group_id='detection') -> None:
self.bootstrap_servers = bootstrap_servers
self.detection_topic = detection_topic
self.result_topic = result_topic
self.group_id = group_id
self.received_frames = [] # store all received frame
self.model = YOLO('yolov8s.pt')
# ========== Tracker ==========
# self.tracker = DeepOCSORT(
# # which ReID model to use
# model_weights=Path('osnet_x0_25_msmt17.pt'),
# device='cuda:0',
# fp16=False,
# )
self.tracker = OCSORT()
# ======== CONSUMER ===========
self.consumer_config = {
'bootstrap.servers': self.bootstrap_servers,
'group.id': self.group_id,
'auto.offset.reset': 'earliest'
}
self.consumer = Consumer(self.consumer_config)
self.consumer.subscribe([self.detection_topic])
self.latest_offset = 0
# ======== PRODUCER =============
# write sth here
self.producer_config = {
'bootstrap.servers': self.bootstrap_servers
}
self.producer = Producer(self.producer_config)
def delivery_report(self, err, msg):
if err is not None:
print('Message delivery failed: {}'.format(err))
else:
print("Result delivered to topic: {}, partition: {}, offset: {}".format(
msg.topic(), msg.partition(), msg.offset()))
def process_frame(self, frame_data, offset):
results = self.model(frame_data, classes=0)
# prepare data to send
detection_data = {
'offset': offset,
}
bboxes = []
for r in results:
boxes = r.boxes
for box in boxes:
# get box coordinates (l, t, r, b, cf, cls)
b = box.data[0].cpu().numpy()
bboxes.append(b)
# ! Check if bboxes is empty or not
if not (not bboxes):
bboxes = np.stack(bboxes, axis=0)
detection_data['detection_results'] = bboxes
# print(bboxes)
print(np.array(bboxes).shape)
tracks = self.tracker.update(np.array(bboxes), frame_data)
print(id(self.tracker))
print('this is tracks')
# print(tracks)
inds = tracks[:, 4].astype('int')
print(inds.shape)
print(inds)
detection_data['inds'] = inds
self.producer.produce(self.result_topic, value=json.dumps(
detection_data, default=lambda x: x.tolist()).encode('utf-8'), callback=self.delivery_report)
self.producer.poll(1)
def receive_and_process_frames(self):
try:
while True:
message = self.consumer.poll(0)
time.sleep(0.6)
if message is None:
print('Waiting...')
# continue
elif message.error():
if message.error().code() == KafkaError._PARTITION_EOF:
continue
else:
print(message.error())
break
else:
# Drop frame
# get the laest offset of every poll
latest_offset_topic = TopicPartition(self.detection_topic,partition=0, offset=OFFSET_END)
self.consumer.assign([latest_offset_topic])
frame_data = cv2.imdecode(np.frombuffer(
message.value(), 'u1'), cv2.IMREAD_UNCHANGED)
self.latest_offset = message.offset() # Get the offset of receive frame
self.process_frame(frame_data=frame_data,
offset=self.latest_offset)
print(f'============={self.latest_offset}===========')
except KeyboardInterrupt:
pass
finally:
self.consumer.close()
if __name__ == '__main__':
kafka_detection = KafkaHumanDetection()
kafka_detection.receive_and_process_frames()