python移动追踪目标检测

 main.py

import cv2
from tracker import *
import time

# 进行追踪
tracker = EuclideanDistTracker()  # 这个函数通过获取同一物体不同时刻的boundingbox的坐标从而实现对其的追踪

# 导入想要进行tracking的视频,要求拍摄视频的过程中摄像头是保持静止状态的
cap = cv2.VideoCapture(0)

# 从导入的视频中找到正在移动的物体
object_detector = cv2.createBackgroundSubtractorMOG2(history=100, varThreshold=40)  # 对参数进行调整则会改变捕捉移动物体的精准性

while True:
    ret, frame = cap.read()
    height, width, _ =frame.shape  # 得出视频画面的大小,从而去方便计算出感兴趣区域所在的位置
    print(height, width)  # 720,1280

    # 设置一个感兴趣区域,让处理(对物体的detection和tracking)只关注于感兴趣区域,从而减少一些计算量也让检测变得简单一些
    roi = frame[200: 600, 200: 600]

    # 物体检测  (根据需要可以将该部分代码换成比如行人检测、汽车检测等等)
    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])
    time.sleep(1)


    # 物体追踪
    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)  # 根据移动物体的轮廓添加boundingbox

    print(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()

track.py

import math


class EuclideanDistTracker:
    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 = 0


    def update(self, objects_rect):
        # Objects boxes and ids
        objects_bbs_ids = []

        # Get center point of new object
        for 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 = False
            for id, 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 = True
                    break

            # New object is detected we assign the ID to that object
            if same_object_detected is False:
                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

https://download.csdn.net/download/Doomer_0/81095332icon-default.png?t=M1FBhttps://download.csdn.net/download/Doomer_0/81095332 

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
IPI(Infrared Passive Imaging)是被动红外(PIR)成像技术的一种,用于在夜间或低光照条件下检测和追踪人体热源。小目标检测算法在Python中实现通常会涉及到以下几个步骤: 1. **数据采集**:使用红外热像仪(如Flir Lepton系列)获取红外图像数据。 2. **预处理**: - **图像校准**:由于环境温度影响,可能会进行温度补偿。 - **降噪**:应用滤波器(如高斯滤波、中值滤波)去除噪声。 - **图像增强**:通过直方图均衡化或自适应阈值方法提高对比度。 3. **目标检测**: - **边缘检测**:如Canny边缘检测,识别感兴趣区域。 - **特征提取**:可能用到模板匹配、Haar特征或深度学习的卷积神经网络(如YOLO或SSD)。 - **大小和位置估计**:根据检测到的目标特征确定其大小和大致位置。 4. **小目标处理**: - **大小过滤**:筛选出较小且疑似人体的红外斑点。 - **跟踪**:如果连续帧中有相同位置的小目标,可能是移动的人体,可以采用卡尔曼滤波等方法进行跟踪。 5. **输出结果**:最后可能生成热图或者简单的位置坐标信息。 在Python中,常用库包括OpenCV、Pillow、scikit-image等用于图像处理,以及TensorFlow或PyTorch等深度学习框架用于模型训练和执行。要实现IPI小目标检测算法,你可能需要以下资源: - OpenCV教程:https://opencv-python-tutroals.readthedocs.io/en/latest/ - Python图像处理教程:https://realpython.com/image-processing-python-opencv/ - PyTorch或TensorFlow官方文档:https://pytorch.org/docs/stable/ 或 https://www.tensorflow.org/ 如果你具体想了解某个部分的代码实现细节,可以提供更详细的问题,我会进一步解释或给出示例代码。下面是一些相关问题供你参考: 1. 是否熟悉Python的基本图像处理操作? 2. 对深度学习在红外目标检测中的应用感兴趣吗? 3. 需要了解哪些Python库在IPI算法中的作用?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Doomer_0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值