AI社交距离检测器:计算被检测人的中心点

恬静的小魔龙 2020-12-15 16:59:08 7847

上一篇文章AI社交距离检测器:在视频帧中检测人我们学习了如何在视频帧中去除并不是人的对象,检测特定目标对象,以便只检测人这个对象。

在本文中,将计算每个对象边界框中心点的位置,这将作为计算距离的基础。

到目前为止,我们学习了如何检测人和找到包围框来指示人们的位置。通常,可以使用边界框的最近顶点来估计距离,但是为了保持准确性,我使用边界框的中心,如下图所示。然后,我用Euclidean(欧几里德公式)计算了它们之间的距离。

矩形中心的计算

当前应用程序可以返回检测对象的列表,该列表中的每个元素都提供标签、矩形(边框)和识别信任值。下面就使用矩形的左上角和右下角来计算中心点。

为了计算矩形的中心,我们计算它的宽度和高度,然后将它们除以2:

def get_rectangle_center(rectangle):
    # Get top and bottom right corner of the rectangle
    top_left_corner = rectangle[0]
    bottom_right_corner = rectangle[1] 

    # Calculate width and height of the rectangle
    width = bottom_right_corner[0] - top_left_corner[0]
    height = bottom_right_corner[1] - top_left_corner[1]

    # Calculate and return the center
    center = (int(width/2 + top_left_corner[0]), int(height/2 + top_left_corner[1]))

    return center

如上所述,函数获得左上角和右下角,然后执行计算。在get_rectangle_center函数的基础上加上get_rectangle_centers函数,它遍历检测结果列表:

@staticmethod
def get_rectangle_centers(detection_results):
    # Prepare the list
    rectangle_centers = []

    # Iterate over detection results, and determine center of each rectangle
    for i in range(len(detection_results)):
        rectangle = detection_results[i]['rectangle']            

        center = DistanceAnalyzer.get_rectangle_center(rectangle)

        rectangle_centers.append(center)

    # Return rectangle centers
    return rectangle_centers

为了确保正确计算中心,我将使用OpenCV在视频序列帧上绘制这些位置。

显示包围盒的中心

给定矩形中心列表,我可以使用OpenCV中的圆形函数在图像上绘制它们。该函数的工作方式类似于矩形,因为它接受输入图像、圆心、厚度、颜色等参数。

下面是使用该函数绘制半径为15像素的黄色圆圈的示例。我把厚度设为-1来填充圆圈:

def draw_rectangle_centers(image, rectangle_centers):        
    for i in range(len(rectangle_centers)):  
        opencv.circle(image, 
            rectangle_centers[i], 
            common.CIRCLE_RADIUS, 
            common.YELLOW, 
            common.THICKNESS_FILL)

上面的函数是作为Image_helper模块中的静态方法实现的。

把东西放在一起

我们现在已经准备好把一切都整合在一起。我们实现Main.py文件如下:

import sys
sys.path.insert(1, '../Part_03/')
sys.path.insert(1, '../Part_05/')

from inference import Inference as model
from image_helper import ImageHelper as imgHelper
from video_reader import VideoReader as videoReader
from distance_analyzer import DistanceAnalyzer as analyzer

if __name__ == "__main__": 
    # Load and prepare model
    model_file_path = '../Models/01_model.tflite'
    labels_file_path = '../Models/02_labels.txt'

    # Initialize model
    ai_model = model(model_file_path, labels_file_path)    

    # Initialize video reader
    video_file_path = '../Videos/01.mp4'
    video_reader = videoReader(video_file_path)

    # Detection and preview parameters
    score_threshold = 0.4    
    delay_between_frames = 5

    # Perform object detection in the video sequence
    while(True):
        # Get frame from the video file
        frame = video_reader.read_next_frame()

        # If frame is None, then break the loop
        if(frame is None):
            break

        # Perform detection        
        results = ai_model.detect_people(frame, score_threshold)

        # Get centers of the bounding boxes (rectangle centers)
        rectangle_centers = analyzer.get_rectangle_centers(results)

        # Draw centers before displaying results
        imgHelper.draw_rectangle_centers(frame, rectangle_centers)   

        # Display detection results
        imgHelper.display_image_with_detected_objects(frame, results, delay_between_frames)

在为前面开发的模块配置输入路径后,我们初始化AI模型并进行计算以检测人员。然后,产生的检测传递给DistanceAnalyzer类的get_rectangle_centers函数,然后使用draw_rectangle_centers函数从视频文件中读取视频帧,然后使用display_image_with_detected_objects绘制边框和标签。

运行Main.py后,您将得到上述结果。

总结

在本文中,我们学习了如何计算在视频序列中检测到的人的中心位置。在下一篇文章我们将利用这些中心来估计人与人之间的距离,并指出那些距离太近的人。

声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包 49 6 评论 打赏
评论
0个
内容存在敏感词
手气红包
    易百纳技术社区暂无数据
相关专栏
置顶时间设置
结束时间
删除原因
  • 广告/SPAM
  • 恶意灌水
  • 违规内容
  • 文不对题
  • 重复发帖
打赏作者
易百纳技术社区
恬静的小魔龙
您的支持将鼓励我继续创作!
打赏金额:
¥1易百纳技术社区
¥5易百纳技术社区
¥10易百纳技术社区
¥50易百纳技术社区
¥100易百纳技术社区
支付方式:
微信支付
支付宝支付
易百纳技术社区微信支付
易百纳技术社区
打赏成功!

感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~

举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

审核成功

发布时间设置
发布时间:
是否关联周任务-专栏模块

审核失败

失败原因
备注
拼手气红包 红包规则
祝福语
恭喜发财,大吉大利!
红包金额
红包最小金额不能低于5元
红包数量
红包数量范围10~50个
余额支付
当前余额:
可前往问答、专栏板块获取收益 去获取
取 消 确 定

小包子的红包

恭喜发财,大吉大利

已领取20/40,共1.6元 红包规则

    易百纳技术社区