-
Notifications
You must be signed in to change notification settings - Fork 25
/
tracker_cv2.py
153 lines (122 loc) · 4.27 KB
/
tracker_cv2.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
152
153
"""
Main file to run DeepSORT local tracking
"""
import os
import sys
import cv2
import json
import argparse
from collections import defaultdict
sys.path.append('yolov4')
from yolov4.annotate import Detector
from deep_sort.tracker import Tracker
from tools import generate_detections as gdet
from deep_sort import nn_matching
from absl import app, flags, logging
import time, random
import tensorflow as tf
from absl.flags import FLAGS
from deep_sort.detection import Detection
import matplotlib.pyplot as plt
import numpy as np
from deep_sort import preprocessing
from tqdm import tqdm
## Suppress Deprecated Warnings
import sys
if not sys.warnoptions:
import warnings
warnings.simplefilter("ignore")
mask_map = {
"Ceiling_Cam" : "ceiling",
"Pen_B" : "penb",
"Pen_C" : "penc"
}
def annotate_video(video_path, view):
video_prefix = video_path.split('.')[0]
video_name = video_prefix.split('/')[-1]
if os.path.exists(f"{video_prefix}.json"):
return
output_dict = {
"videoFileName": video_name,
"fullVideoFilePath": video_path,
"stepSize": 0.1,
"config": {
"stepSize": 0.1,
"playbackRate": 0.4,
"imageMimeType": "image/jpeg",
"imageExtension": ".jpg",
"framesZipFilename": "extracted-frames.zip",
"consoleLog": "0"
},
"objects":[]
}
# Definition of the parameters
max_cosine_distance = 0.5
nn_budget = None
nms_max_overlap = 1.0
#initialize deepsort
model_filename = 'networks//mars-small128.pb'
encoder = gdet.create_box_encoder(model_filename, batch_size=1)
metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
## Initialize Object Detector and Tracker
cap = cv2.VideoCapture(video_path)
w, h, video_fps = int(cap.get(3)), int(cap.get(4)), cap.get(5)
det = Detector(w, h)
deep_sort_tracker = Tracker(metric, det, encoder)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
#out = cv2.VideoWriter(f"{video_prefix}-annotated.mp4", fourcc, video_fps, (w, h))
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
total_frames = 0
pbar = tqdm(total=length, file=sys.stdout)
mask_path = f"./yolov4/masks/{mask_map[view]}-maskfilter.png"
print(f"Loading mask {mask_path}")
mask_filter = cv2.imread(mask_path)
if mask_filter.shape[:2] != (h, w):
mask_filter = cv2.resize(mask_filter, (w, h))
id_frames = defaultdict(list)
## Read frames from stream
while(True):
ret, frame = cap.read()
if not ret:
break
## Tracker consumes a frame and spits out an annotated_frame
if mask_filter is not None:
frame = cv2.bitwise_and(frame, mask_filter)
annotated_frame, bbox_dict = deep_sort_tracker.consume(frame)
## Save the annotated frame
#out.write(annotated_frame)
pbar.update(1)
for pig_id in bbox_dict:
xmin, ymin, xmax, ymax = bbox_dict[pig_id]
x = int((xmin+xmax)/2)
y = int((ymin+ymax)/2)
width, height = int(xmax-xmin), int(ymax-ymin)
id_frames[pig_id].append({
"frameNumber": total_frames,
"bbox": {
"x": x,
"y": y,
"width": width,
"height": height
},
"isGroundTruth": "1",
"visible": "1",
"behaviour": "other"
})
total_frames += 1
#out.release()
cap.release()
del det, deep_sort_tracker
for pig_id, frames in id_frames.items():
output_dict["objects"].append({
"frames": frames,
"id": pig_id
})
with open(f"{video_prefix}.json", "w") as f:
json.dump(output_dict, f)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description = "Detect, Track and Count")
parser.add_argument('--stream_source', '-s', required=True, help="Source video stream. Default stream is the webcam")
parser.add_argument('--view', '-v', required=True, help="Specify Camera view")
args = parser.parse_args()
annotate_video(args.stream_source, args.view)