Home  >  Article  >  Technology peripherals  >  Train YOLOv7 model and develop AI fire detection

Train YOLOv7 model and develop AI fire detection

王林
王林forward
2023-05-11 13:43:06857browse

1. Prepare the data set

The data set uses open source images, a total of 6k fire images, marked with ​​thick smoke​​​and​​ Fire​Two categories.

Train YOLOv7 model and develop AI fire detection

Train YOLOv7 model and develop AI fire detection

## thick smoke

The project adopts​

​YOLO​​​Training, I have converted the data into​​YOLO​​​format, and separated the training set and validation set, see the ​​dataset​​directory.

Train YOLOv7 model and develop AI fire detection

2. Training

For the training process, please refer to the YOLOv7 official website documentation.

Modify the data/coco.yaml file and configure the path and category of the training data.

Download the pre-trained model yolov7.pt, and then you can start training

Train YOLOv7 model and develop AI fire detection

3. Fire monitoring

After the training is completed, In the run directory under the yolov7 directory, find the generated model file—best.pt.

The model I trained is placed in the source code weights directory, named fire.pt, and you can use it directly.

With the model, we use it to develop a monitoring program.

First of all, you need to download the yolov7 source code to the current project.

Train YOLOv7 model and develop AI fire detection

Then, install pytorch and use pytorch to load the trained yolov7 model.

fire_detector = torch.hub.load('./yolov7', 'custom', './weights/fire.pt', source='local')

There is a fire video fire_video.mp4 in the source code, which can be read with opencv to test the detection effect.

ret, frame = cap.read()
results = self.fire_detector(img_cvt)
pd = results.pandas().xyxy[0]

# 绘制检测框
for obj in pd.to_numpy():
box_l, box_t = int(obj[0]), int(obj[1])
box_r, box_b = int(obj[2]), int(obj[3])
obj_name = obj[6]

if obj_name == 'smoke':
box_color = (0, 0, 255)
box_txt = '检测到浓烟'
else:
box_color = (0, 255, 0)
box_txt = '检测到大火'

frame = cv2.rectangle(frame, (box_l, box_t), (box_r, box_b), box_color, 2)
frame = cv2_add_chinese_text(frame, box_txt, (box_l, box_t-40), box_color, 25)

After running successfully, the effect will be the same as the video at the beginning of the article.

You can deploy the project to an embedded GPU, such as jetson nano, for real-time detection. Develop a cloud communication service to call the police in the event of a fire.

At the same time, an APP can also be developed to transmit the live video stream back to the server. The APP can see the monitoring effect in real time and help make decisions.

4. Difficulties

In fact, there are still some difficulties in using target detection for fire detection. For example, there are many interfering samples, which can easily lead to false detections. For another example, inconsistent labeling prevents efficient calculation of mAP.

So, it is best if we can customize the loss function and the calculation method of the accuracy. Taking recall as an example, if we can detect a fire in the picture, it will be considered successful, but it does not necessarily have to detect how many flames and how many smokes.

Of course, this type of task does not necessarily have to be done through target detection. A friend suggested to me the use of classification tasks and segmentation tasks, and I think I can try both.

The above is the detailed content of Train YOLOv7 model and develop AI fire detection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete