• 入侵检测代码


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

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

    效果如下:

  • 相关阅读:
    Vue3.0路由拦截
    javaWeb项目基于tomcat运行部署后访问方案总结
    程序员笔记本电脑选 windows 还是 MAC
    EtherCAT总线运动控制学习笔记(RXXW_Dor)
    基于Javaweb的流动市场地摊管理系统
    MAX17058_MAX17059 STM32 iic 驱动设计
    SpringBoot开发实用篇2---与数据层技术有关的替换和整合
    手动模式配置链路聚合
    Java 21 新特性:虚拟线程(Virtual Threads)
    Java培训:自定义代码生成器
  • 原文地址:https://blog.csdn.net/qq_33023933/article/details/133883100