Beautification of the sound alarm after YOLOv5 detects the target - YOLOv5 detection interface based on PyQt5

The previous blog post wrote how to enable YOLOv5 to give a sound alarm after detecting the target. This blog post is to optimize its interface to make it more convenient and easy to use, and it is one step closer to the product.

I mainly refer to the code of this boss, and according to my own needs, I made improvements to add voice alarms.
Here is the video link of the boss: YOLOv5 detection interface - PyQt5 implementation
The source code of the boss is here: I have moved the source code to github here.
easy

Note: My YOLOv5 detection algorithm is based on the 6.1 version of YOLOv5, and it is an s-model, from which the model is trained to obtain the training weight; if it is not 6.1 version YOLOv5, when running my set of things, various problems may occur. Various program errors. The YOLOv5 used in the last blog post I wrote is also version 6.1: How to make YOLOv5 sound an alarm after detecting the target?

1. Interface effect display

Because the detection result saving function of the YOLOv5 algorithm is used, the saved video file has no sound. So I used QQ screen recording to record all the effects. It was recorded in the laboratory, and the background was a bit noisy.

Detection interface effect display

2. Improvements made

1. First, add the voice alarm code in the main.py file, and the adding position is as follows:
①Add these three lines of code at this position. insert image description here
The code is as follows:

t0 = time.time() 
count = 0 
tplay = 0

②Add the following code at this position
insert image description here
The code is as follows:

 value = det[:, 4].max().item()  # 解决的是看到视频就报警,每取四个置信度的值,找到最大,大于0.5才播放告警语音
 if value > 0.5:  # 大于0.5播放警示语音
    count += 1  # count相当于一个滤波器,小延时功能,防止误报(累计5个数之后再报语音)。
    if count > 5:
        count = 0
        if time.time() - tplay > 2.0:  # 防止告警声音的叠加播放,2s播放完再继续播报
            import os
            os.system('start /b D:/YOLOv5-5.0-PYQT-series/pyqt5-yolov5-yolov5_v6.1/ffmpeg/bin/ffplay.exe -autoexit -nodisp D:/YOLOv5-5.0-PYQT-series/pyqt5-yolov5-yolov5_v6.1/Smoking-warning.mp3')  # 音乐播放函数
          # 参数含义: start /b 后台启动    ffplay音乐播放软件的位置      -autoexit 播放完毕自动退出 -nodisp不显示窗口        mp3语音的位置
            tplay = time.time()  # 当前系统时钟

An important detail, many people have not noticed that
after the above two codes are added, run the main.py program, and the interface that comes out will have a stuck problem when switching the detection mode (for example, switching from detection video to camera real-time detection, There is an error reporting stutter problem), the error is: local variable 'tplay' referenced before assignment This error will occur if the global variable is changed inside the function.
The solution is to add this line of code here
insert image description here
:

global tplay #将tplay设置为全局变量,解决了报错:local variable 'tplay' referenced before assignment 的问题。 注:在函数内部更改全局变量就会出现此错误

Regarding the file name and path,
do not use Chinese! Don't use Chinese! Don't use Chinese!
Change all to English to name all folders of your project.
If there is Chinese in the file path, an error will be reported: can't open/read file: check file path/integrity
**Solution: **Modify the path name, Chinese will never appear, and sometimes English capitalization is not allowed.

After the code improvement is completed, you can run the program

3. Future Outlook

You can also package the open source projects of the above big guys and my improvements to the project into one file, and run it as an .exe executable program.
The advantage of this is 1. After the project is transplanted to other computer devices, there is no need to reconfigure the support environment required by the YOLOv5 detection algorithm (as we all know, the configuration problem of the support environment is a hurdle). After packaging the exe, click on the file to use it. ;2. It is easier to display, you don’t need to look at a bunch of codes, click to run on the IDE editor, in other words, after packaging into an .exe file, it is more like a product (when showing the project effect to Party A, always You can't let them watch a bunch of codes running, it looks informal), and there is a certain confidentiality effect, other people can't see your code, and can't easily copy it (of course, reverse cracking can definitely be done) , not 100% confidential).
At present, exe packaging has been implemented, but the file is a bit large.
insert image description here
The overall effect is exactly the same as in the above video.

Guess you like

Origin blog.csdn.net/Mr_LanGX/article/details/130408059