forked from zyayoung/K210-Tracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_record.py
79 lines (69 loc) · 2.74 KB
/
read_record.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
import cv2
import numpy as np
from PIL import Image
import io
class Record:
def __init__(self, ann_filename):
ann_filename = ann_filename.replace(".mjpeg", ".txt")
ann_raw = open(ann_filename).read().split("T")
self.ann = []
self.p = []
self.times = []
self.det_counts = []
self.dets = {}
for i in range(len(ann_raw)-1):
a = ann_raw[i].split()
if a:
time = int(a[0])
if len(a)>2:
ann_tmp = np.array(a[2:], dtype=float).reshape(-1, 5)
ann_tmp[..., 4] *= 100
self.ann.append((time, int(a[1]), int(ann_raw[i+1].split()[1]), np.array(np.uint16(ann_tmp))))
else:
self.ann.append((time, int(a[1]), int(ann_raw[i+1].split()[1]), []))
self.times.append(time)
self.det_counts.append(self.ann[-1][-1][..., 4].sum() if len(self.ann[-1][-1]) else 0)
self.frame_count = len(self.ann)
self.vid_file = open(ann_filename[:-4]+".mjpeg", "rb")
self.cur_pos = 0
self.threshold = 0.8
def get_frame(self, index=None):
if index is None:
# self.cur_pos += 1
index = self.cur_pos
time, p, pend, dets = self.ann[index]
self.vid_file.seek(p)
im_b = self.vid_file.read(pend-p+4)
im = cv2.imdecode(np.asarray(bytearray(im_b), dtype="uint8"), cv2.IMREAD_COLOR)
return im
def get_dets(self, index=None):
if index is None:
index = self.cur_pos
time, p, pend, dets = self.ann[index]
if len(dets) == 0:
return []
keep = dets[..., 4]>self.threshold * 100
return dets[keep]
def get_time(self, index=None):
if index is None:
index = self.cur_pos
return self.times[index]
def get_interval(self, index=None):
if index is None:
index = self.cur_pos
return (self.times[index+1] - self.times[index]) if (0 < index + 1 < self.frame_count) else (1000 / self.get_mean_fps())
def get_mean_fps(self):
return len(self.times) * 1000 / (self.times[-1] - self.times[0])
if __name__ == "__main__":
rec = Record("25.txt")
ann = rec.ann
for i in range(rec.frame_count):
im = rec.get_frame(i)
dets = rec.get_dets(i)
time = rec.get_time(i)
cv2.putText(im, f"{(time//60000)%60}:{(time//1000)%60}", (20, 20), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 0.6, (0,255,0))
for det in dets:
x_min, y_min, x_max, y_max = np.int32(det[:-1])
cv2.rectangle(im, (x_min, y_min), (x_max, y_max), (255, 0, 0))
cv2.imshow("im", im)
cv2.waitKey(1)