• 入侵检测代码


    人工智能中有个入侵检测:当检测到的目标位于指定区域内才算是入侵,思路很简单,判断相关坐标即可:

    1. from matplotlib import pyplot as plt, patches
    2. from shapely.geometry import Polygon, Point
    3. def is_intrusion(target_box, intrusion_area):
    4. # 判断目标框的多个个角是否在入侵区域内
    5. for point in [(target_box[0], target_box[1]), (target_box[2], target_box[3]),
    6. (target_box[0], target_box[3]), (target_box[2], target_box[1])]:
    7. if point_in_polygon(point, intrusion_area):
    8. return True
    9. # 判断入侵区域是否与目标框相交
    10. intrusion_polygon = Polygon(intrusion_area)
    11. target_polygon = Polygon([(target_box[0], target_box[1]), (target_box[2], target_box[1]),
    12. (target_box[2], target_box[3]), (target_box[0], target_box[3])])
    13. return intrusion_polygon.intersects(target_polygon)
    14. def point_in_polygon(point, polygon):
    15. # 使用 shapely 库检查点是否在多边形内
    16. point = Point(point)
    17. polygon = Polygon(polygon)
    18. return point.within(polygon)
    19. # 示例用法
    20. target_box = (100, 100, 200, 200) # 目标框坐标 (x_min, y_min, x_max, y_max)
    21. intrusion_area_hexagon = [(150, 150), (200, 190), (250, 150), (250, 200), (200, 220), (150, 1000), (100, 220), (150, 190)] # 六边形区域坐标
    22. result = is_intrusion(target_box, intrusion_area_hexagon)
    23. print("是否入侵:", result)
    24. # 创建一个新的图
    25. fig, ax = plt.subplots()
    26. # 绘制目标框
    27. target_rect = patches.Rectangle((target_box[0], target_box[1]), target_box[2] - target_box[0],
    28. target_box[3] - target_box[1], linewidth=1, edgecolor='r', facecolor='none')
    29. ax.add_patch(target_rect)
    30. # 绘制六边形区域
    31. intrusion_area_polygon = patches.Polygon(intrusion_area_hexagon, closed=True, linewidth=1, edgecolor='b',
    32. facecolor='none')
    33. ax.add_patch(intrusion_area_polygon)
    34. # 设置图的坐标轴范围
    35. ax.set_xlim(0, 1920)
    36. ax.set_ylim(0, 1080)
    37. # 显示图
    38. plt.show()

    效果如下:

  • 相关阅读:
    分布式块存储 ZBS 的自主研发之旅 | 架构篇
    2023年windows DockerDeskTop最新款4.18.0 全程保姆级安装
    【supplemental】On_Distillation_of_CVPR_2023_supplemental
    串口通信的基本原理
    MQTT,JSON,VSCODE(C语言编程环境)心得
    T31开发笔记:metaipc测试
    【ICASSP 2023】ST-MVDNET++论文阅读分析与总结
    华为机试题目
    Python网络编程多线程实现异步服务端
    【附源码】计算机毕业设计SSM网上鲜花店系统
  • 原文地址:https://blog.csdn.net/qq_33023933/article/details/133883100