import cv2
from tracker import*# 进行追踪
tracker = EuclideanDistTracker()# 这个函数通过获取同一物体不同时刻的boundingbox的坐标从而实现对其的追踪# 导入想要进行tracking的视频,要求拍摄视频的过程中摄像头是保持静止状态的
cap = cv2.VideoCapture("highway.mp4")# 从导入的视频中找到正在移动的物体
object_detector = cv2.createBackgroundSubtractorMOG2(history=100, varThreshold=40)# 对参数进行调整则会改变捕捉移动物体的精准性whileTrue:
ret, frame = cap.read()
height, width, _ =frame.shape # 得出视频画面的大小,从而去方便计算出感兴趣区域所在的位置print(height, width)# 720,1280# 设置一个感兴趣区域,让处理(对物体的detection和tracking)只关注于感兴趣区域,从而减少一些计算量也让检测变得简单一些
roi = frame[340:720,500:800]# 物体检测 (根据需要可以将该部分代码换成比如行人检测、汽车检测等等)
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 =[]# 用于存放boundingbox的起始点坐标、宽、高for cnt in contours:# 计算出每个轮廓内部的面积,并根据面积的大小去除那些不必要的噪声(比如树、草等等)
area = cv2.contourArea(cnt)if area >100:# cv2.drawContours(roi, [cnt], -1, (0, 255, 0), 2) # 画出移动物体的轮廓
x, y, w, h = cv2.boundingRect(cnt)
detections.append([x, y, w, h])# 物体追踪
boxer_ids = tracker.update(detections)# 同一个物体会有相同的ID# print(boxer_ids)for box_id in boxer_ids:
x, y, w, h,id= box_id
cv2.putText(roi,"Obj"+str(id),(x, y -15), cv2.FONT_ITALIC,0.7,(255,0,0),2)
cv2.rectangle(roi,(x, y),(x + w, y + h),(0,255,0),2)# 根据移动物体的轮廓添加boundingboxprint(detections)
cv2.imshow("Frame", frame)# 打印结果# cv2.imshow("Mask", mask) # 打印出蒙版# cv2.imshow("ROI", roi) # 打印出你想要的ROI在哪
key = cv2.waitKey(30)if key ==27:break
cap.release()
cv2.destroyAllWindows()
tracker.py文件
import math
classEuclideanDistTracker:def__init__(self):# Store the center positions of the objects
self.center_points ={}# Keep the count of the IDs# each time a new object id detected, the count will increase by one
self.id_count =0defupdate(self, objects_rect):# Objects boxes and ids
objects_bbs_ids =[]# Get center point of new objectfor rect in objects_rect:
x, y, w, h = rect
cx =(x + x + w)//2
cy =(y + y + h)//2# Find out if that object was detected already
same_object_detected =Falseforid, pt in self.center_points.items():
dist = math.hypot(cx - pt[0], cy - pt[1])if dist <25:
self.center_points[id]=(cx, cy)print(self.center_points)
objects_bbs_ids.append([x, y, w, h,id])
same_object_detected =Truebreak# New object is detected we assign the ID to that objectif same_object_detected isFalse:
self.center_points[self.id_count]=(cx, cy)
objects_bbs_ids.append([x, y, w, h, self.id_count])
self.id_count +=1# Clean the dictionary by center points to remove IDS not used anymore
new_center_points ={}for obj_bb_id in objects_bbs_ids:
_, _, _, _, object_id = obj_bb_id
center = self.center_points[object_id]
new_center_points[object_id]= center
# Update dictionary with IDs not used removed
self.center_points = new_center_points.copy()return objects_bbs_ids