-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTracks.py
58 lines (45 loc) · 2 KB
/
Tracks.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
import cv2
from tracker import *
# Create tracker object
tracker = EuclideanDistTracker()
cap = cv2.VideoCapture(r"C:\Users\End User\PycharmProjects\cloudchamber\video\first one min.mp4")
# Object detection from Stable camera
object_detector = cv2.createBackgroundSubtractorMOG2(history=100, varThreshold=40)
while True:
ret, frame = cap.read()
height, width, _ = frame.shape
# Extract Region of interest
roi = frame[280: 1580, 560: 1720]
# 1. Object Detection
mask = object_detector.apply(roi)
_, mask = cv2.threshold(mask, 254, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
detections = []
for cnt in contours:
# Calculate area and remove small elements
area = cv2.contourArea(cnt)
if area > 100:
# cv2.drawContours ( roi , [ cnt ] , -1 , ( 0 , 255 , 0 ) , 2 )
x, y, w, h = cv2.boundingRect(cnt)
cv2.rectangle(roi, (x, y), (x + w, y + h), (0, 255, 0), 3)
print(x, y, w, h)
detections.append([x, y, w, h])
# 2.Object Tracking
boxes_ids = tracker.update(detections)
print(boxes_ids)
for box_id in boxes_ids:
X, Y, W, h, id = box_id
#cv2.putText(roi, str(id), (x, y - 15), cv2.FONT_HERSHEY_PLAIN, 4, (255, 0, 0), 4)
cv2.putText(roi, str('particles'), (x, y - 15), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 4)
# cv2.putText(roi, str('muon'), (x, y - 25), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 4)
# cv2.putText(roi, str('electron'), (x, y - 35), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 4)
# cv2.putText(roi, str('photoelectron'), (x, y - 45), cv2.FONT_HERSHEY_PLAIN, 4, (255, 0, 0), 4)
cv2.rectangle(roi, (x, y), (x + W, y + h), (0, 255, 0), 3)
cv2.imshow(" roi ", roi)
cv2.imshow(" Frame ", frame)
cv2.imshow(" Mask ", mask)
key = cv2.waitKey(30)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()