当前位置:   article > 正文

yolov8 区域声光报警+计数

yolov8 区域声光报警+计数

1. 基础

本项目是在 yolov8 区域多类别计数 的基础上实现的,具体区域计数原理可见上边文章

2. 报警功能

设置一个区域region_points,当行人这一类别进入该区域范围内,会触发报警系统,产生警报

2. 1声音报警代码

pygame.mixer.init()  # 初始化
pygame.mixer.music.load('out.wav')  # 加载报警音源
pygame.mixer.music.set_volume(1)   # 设置声音
pygame.mixer.music.play()  # 播放音乐
  • 1
  • 2
  • 3
  • 4

2. 2画面显示报警代码

text = "ALARM"
cv2.putText(annotated_frame, text, (600, 300), cv2.FONT_ITALIC, 2.0,(0, 0, 255), 3)
  • 1
  • 2

请添加图片描述

3. 完整代码


import cv2
import numpy as np
from ultralytics import YOLO
import pygame

def is_inside_region(point, region_points):
    """
    判断点是否在指定区域内
    """
    return cv2.pointPolygonTest(np.array(region_points), point, False) >= 0

def detect():

    model = YOLO("yolov8n.pt")
    cv2.namedWindow('region couter', cv2.WINDOW_NORMAL)
    cv2.resizeWindow('region couter', 960, 540)  # 设置宽高
    cap = cv2.VideoCapture('ultralytics/assets/people.mp4')
    out_video = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(*'XVID'), 30, (1920, 1080))

    region_points = [(640, 270), (640, 540), (1280, 540), (1280, 270)]
    region_points_np = np.array(region_points)

    assert cap.isOpened(), "Error reading video file"
    while cap.isOpened():
        success, im0 = cap.read()
        if not success:
            print("Video frame is empty or video processing has been successfully completed.")
            break
        tracks = model.track(im0, persist=True)
        annotated_frame = tracks[0].plot()
        boxes = tracks[0].boxes.data
        count_class1 = 0
        count_class2 = 0
        for i, box in enumerate(boxes):
            x1, y1, x2, y2 = box[:4]
            x_center = (x1 + x2) / 2
            y_center = (y1 + y2) / 2
            center_point = (int(x_center), int(y_center))
            if is_inside_region(center_point, region_points):
                if box[-1] == 0:  # 类别1的标签.人
                    count_class1 += 1
                    pygame.mixer.init()
                    pygame.mixer.music.load('out.wav')
                    pygame.mixer.music.set_volume(1)
                    pygame.mixer.music.play()
                    text = "ALARM"
                    cv2.putText(annotated_frame, text, (960, 400), cv2.FONT_ITALIC, 2.0,(0, 255, 255), 3)

                elif  box[-1] == 2:  # 类别2的标签,车
                    count_class2 += 1
        # cv2.polylines(annotated_frame, [region_points_np], isClosed=True, color=(255, 0, 0), thickness=2)
        region_mask = np.zeros_like(annotated_frame)
        cv2.fillPoly(region_mask, [region_points_np], color=(255, 0, 0))
        # 使用透明度将填充后的区域与原始帧混合
        alpha = 0.3  # 调整透明度
        annotated_frame = cv2.addWeighted(annotated_frame, 1 - alpha, region_mask, alpha, 0)
        print("Number of objects in class 1:", count_class1)
        print("Number of objects in class 2:", count_class2)

        cv2.imshow("region couter", annotated_frame)
        out_video.write(annotated_frame)
        if cv2.waitKey(1) == ord('q'):
            break
    out_video.release()
    cap.release()
    cv2.destroyAllWindows()
if __name__ == '__main__':
    detect()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

4. 源码

下载链接:https://download.csdn.net/download/qq_45077760/89212535

有问题可以在评论区交流

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/524096
推荐阅读
相关标签
  

闽ICP备14008679号