AI社交距离检测器:寻找过于接近的人

恬静的小魔龙 2020-12-15 17:11:22 7642

上一篇文章AI社交距离检测器:计算被检测人的中心点我们学习了如何计算在视频序列中检测到的人的中心位置。

在本文中,将计算每个对象边界框中心点的位置,并且使用这些中心来估计人与人之间的距离,并指出那些距离太近的人。

接下来,就要将AI模型与计算人们违反社交距离的规则联系起来。

在本文中,我们继续为AI社交距离检测器开发Python控制台应用程序。在学习了如何在图像中检测人的位置后,我们就可以计算出它们之间的距离,并指出哪些人太近了。

如下图所示,如果两个人之间的距离低于一个预定义的阈值,那么这些人将被用红色矩形勾画出来。

计算两点之间的距离

我们首先创建一个方法来计算两个人之间的距离。为此,我使用了Euclidean(欧几里德算法)计算了两个包围盒中心之间距离:

def calculate_distance_between_rectangle_centers(rect_center_1, rect_center_2):
# Calculate absolute difference between x and y coordinates
    x_abs_diff = abs(rect_center_1[0] - rect_center_2[0])
    y_abs_diff = abs(rect_center_1[1] - rect_center_2[1])

    # Calculate Euclidean distance
    return math.sqrt(x_abs_diff**2 + y_abs_diff**2)

上面的函数是distance_analyzer模块。

找那些太亲近的人

为了找到太近的人,我们需要检查每个人之间的距离。实际上,我们需要执行一个双循环。嵌套for循环中的迭代次数从j=1..N减少到j=i+1..N。这里是一个方法的完整实现,该方法给定检测人员的列表,查找那些太近的人:

def find_people_that_are_too_close(detection_results, distance_threshold):
    # Prepare results' list
    results = []

    # Get rectangle centers
    rectangle_centers = DistanceAnalyzer.get_rectangle_centers(detection_results

    # Analyze distance between each object
    N = len(detection_results)
    for i in range(N):
        for j in range(i+1, N):
            rect_center_1 = rectangle_centers[i]
            rect_center_2 = rectangle_centers[j]

            distance = DistanceAnalyzer.calculate_distance_between_rectangle_centers(
                rect_center_1, rect_center_2)

            # If distance between objects is too close
            # append centers to the results' list
            if(distance <= distance_threshold):
                results.append((detection_results[i]['rectangle'],
                    detection_results[j]['rectangle']))

    return results

该方法接受两个参数:

  • detection_results-AI返回的被检测人员名单。注意,该列表中的每个元素都包含一个保存检测信任值、边界框和标签的对象。
  • distance_threshold-指定人们在图像中的距离(以像素为单位)。如果计算出的人与人之间的距离低于此值,则将边框添加到返回的results名单。

给定输入参数,该方法找出包围盒的中心,然后计算它们之间的距离。它返回围绕在太近的人周围的矩形。让我们看看如何在图像中显示这些矩形。

显示谁离得太近

为了表示太近的人,我们可以使用OpenCV的rectangle函数,如前所述。代码看起来可能非常类似于我们用来为图像中检测到的对象绘制边界框和标签的代码。该方法返回一个列表,其中每个元素包含两个矩形。这些矩形是违反社交距离规则的人的包围盒(distance_threshold).

我们需要迭代列表中的元素并一次显示两个矩形:

def indicate_people_that_are_too_close(image, people_that_are_too_close, delay=0):
    # Prepare window
    opencv.namedWindow(common.WINDOW_NAME, opencv.WINDOW_GUI_NORMAL)

    # Iterate over objects (that is a pair of rectangle_points)
    for i in range(len(people_that_are_too_close)):

        # Draw each rectangle
        for j in range(len(people_that_are_too_close[i])):
            rectangle_points = people_that_are_too_close[i][j]

            opencv.rectangle(image, rectangle_points[0], rectangle_points[1],
                common.RED, common.LINE_THICKNESS)

    # Display image
    opencv.imshow(common.WINDOW_NAME, image)

    # Wait until the user presses any key
    opencv.waitKey(delay)

如上所示,我们使用namedWindow函数来创建窗口和imshow以显示图像。其他组件几乎与指示检测对象的情况相同。

把东西放在一起

对于以上所有的部分,我们可以如下所示创建主脚本Main.py

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

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) 

        # Find people that are too close
        proximity_distance_threshold = 50
        people_that_are_too_close = analyzer.find_people_that_are_too_close(
            results, proximity_distance_threshold)

        # Indicate those objects in the image
        imgHelper.indicate_people_that_are_too_close(
            frame, people_that_are_too_close, delay_between_frames)

脚本设置AI模型,打开示例视频文件,然后查找太近的人。在这里,我将距离阈值设置为50像素。你可以自由试验这个参数。在运行Main.py,您将得到在导言中显示的结果。

总结

我们最终将AI与计算人们违反社会距离的规则联系起来。然而,我们一直使用的模型并不是非常健壮,有时解决方案不能正确地指示太近的人。我会在下一篇文章通过加入最先进的YOLO探测器来解决这个问题。

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

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

举报反馈

举报类型

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

详细说明

审核成功

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

审核失败

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

小包子的红包

恭喜发财,大吉大利

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

    易百纳技术社区