Skip to content

Commit 3701a35

Browse files
committed
RPD-00: DeepSORT Implemented
0 parents  commit 3701a35

23 files changed

+2960
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
__pycache__
2+
**/__pycache__/*
3+
.vscode
4+
# remove evertything inside models and datasets folder
5+
/models/*
6+
/datasets/*
7+
/runs/*

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# YOLOv8 OpenVINO Optimized Demo
2+
This is a demo of YOLOv8 object detection model optimized with OpenVINO Toolkit.
3+
4+
## How to run
5+
1. Install the requirements:
6+
```bash
7+
pip install -r requirements.txt
8+
```
9+
2. Create the optimized model by running the notebook cells at `convert.ipynb` to convert the model to OpenVINO format. **(IMPORTANT)**
10+
2. Run the demo:
11+
```bash
12+
python demo.py
13+
```
14+
**OR** You can run the notebook version of the demo from `demo.ipynb` file.
15+
16+
_Note: Demo won't work without creating the optimized model._
17+
18+
## Demo (CPU)
19+
![demo2](demoImages/demo2.png)
20+
![demo1](demoImages/demo1.png)
21+
![demo1](demoImages/demo3.png)
22+
![demo1](demoImages/demo4.png)

convert.ipynb

+736
Large diffs are not rendered by default.

deepSort/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# vim: expandtab:ts=4:sw=4

deepSort/detection.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# vim: expandtab:ts=4:sw=4
2+
import numpy as np
3+
4+
5+
class Detection(object):
6+
"""
7+
This class represents a bounding box detection in a single image.
8+
9+
Parameters
10+
----------
11+
tlwh : array_like
12+
Bounding box in format `(x, y, w, h)`.
13+
confidence : float
14+
Detector confidence score.
15+
feature : array_like
16+
A feature vector that describes the object contained in this image.
17+
18+
Attributes
19+
----------
20+
tlwh : ndarray
21+
Bounding box in format `(top left x, top left y, width, height)`.
22+
confidence : ndarray
23+
Detector confidence score.
24+
class_name : ndarray
25+
Detector class.
26+
feature : ndarray | NoneType
27+
A feature vector that describes the object contained in this image.
28+
29+
"""
30+
31+
def __init__(self, tlwh, confidence, class_name, feature):
32+
self.tlwh = np.asarray(tlwh, dtype=np.float)
33+
self.confidence = float(confidence)
34+
self.class_name = class_name
35+
self.feature = np.asarray(feature, dtype=np.float32)
36+
37+
def get_class(self):
38+
return self.class_name
39+
40+
def to_tlbr(self):
41+
"""Convert bounding box to format `(min x, min y, max x, max y)`, i.e.,
42+
`(top left, bottom right)`.
43+
"""
44+
ret = self.tlwh.copy()
45+
ret[2:] += ret[:2]
46+
return ret
47+
48+
def to_xyah(self):
49+
"""Convert bounding box to format `(center x, center y, aspect ratio,
50+
height)`, where the aspect ratio is `width / height`.
51+
"""
52+
ret = self.tlwh.copy()
53+
ret[:2] += ret[2:] / 2
54+
ret[2] /= ret[3]
55+
return ret

deepSort/iou_matching.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# vim: expandtab:ts=4:sw=4
2+
from __future__ import absolute_import
3+
import numpy as np
4+
from . import linear_assignment
5+
6+
7+
def iou(bbox, candidates):
8+
"""Computer intersection over union.
9+
10+
Parameters
11+
----------
12+
bbox : ndarray
13+
A bounding box in format `(top left x, top left y, width, height)`.
14+
candidates : ndarray
15+
A matrix of candidate bounding boxes (one per row) in the same format
16+
as `bbox`.
17+
18+
Returns
19+
-------
20+
ndarray
21+
The intersection over union in [0, 1] between the `bbox` and each
22+
candidate. A higher score means a larger fraction of the `bbox` is
23+
occluded by the candidate.
24+
25+
"""
26+
bbox_tl, bbox_br = bbox[:2], bbox[:2] + bbox[2:]
27+
candidates_tl = candidates[:, :2]
28+
candidates_br = candidates[:, :2] + candidates[:, 2:]
29+
30+
tl = np.c_[np.maximum(bbox_tl[0], candidates_tl[:, 0])[:, np.newaxis],
31+
np.maximum(bbox_tl[1], candidates_tl[:, 1])[:, np.newaxis]]
32+
br = np.c_[np.minimum(bbox_br[0], candidates_br[:, 0])[:, np.newaxis],
33+
np.minimum(bbox_br[1], candidates_br[:, 1])[:, np.newaxis]]
34+
wh = np.maximum(0., br - tl)
35+
36+
area_intersection = wh.prod(axis=1)
37+
area_bbox = bbox[2:].prod()
38+
area_candidates = candidates[:, 2:].prod(axis=1)
39+
return area_intersection / (area_bbox + area_candidates - area_intersection)
40+
41+
42+
def iou_cost(tracks, detections, track_indices=None,
43+
detection_indices=None):
44+
"""An intersection over union distance metric.
45+
46+
Parameters
47+
----------
48+
tracks : List[deep_sort.track.Track]
49+
A list of tracks.
50+
detections : List[deep_sort.detection.Detection]
51+
A list of detections.
52+
track_indices : Optional[List[int]]
53+
A list of indices to tracks that should be matched. Defaults to
54+
all `tracks`.
55+
detection_indices : Optional[List[int]]
56+
A list of indices to detections that should be matched. Defaults
57+
to all `detections`.
58+
59+
Returns
60+
-------
61+
ndarray
62+
Returns a cost matrix of shape
63+
len(track_indices), len(detection_indices) where entry (i, j) is
64+
`1 - iou(tracks[track_indices[i]], detections[detection_indices[j]])`.
65+
66+
"""
67+
if track_indices is None:
68+
track_indices = np.arange(len(tracks))
69+
if detection_indices is None:
70+
detection_indices = np.arange(len(detections))
71+
72+
cost_matrix = np.zeros((len(track_indices), len(detection_indices)))
73+
for row, track_idx in enumerate(track_indices):
74+
if tracks[track_idx].time_since_update > 1:
75+
cost_matrix[row, :] = linear_assignment.INFTY_COST
76+
continue
77+
78+
bbox = tracks[track_idx].to_tlwh()
79+
candidates = np.asarray([detections[i].tlwh for i in detection_indices])
80+
cost_matrix[row, :] = 1. - iou(bbox, candidates)
81+
return cost_matrix

0 commit comments

Comments
 (0)