Dry goods - YOLOv5 detects fireworks

Fire is a common natural disaster and accident, which often threatens the safety of people's lives and property. In the case that manpower cannot detect the fire in time, the fire may spread rapidly and cause irreparable losses. Therefore, it is very important to detect fire in time and take effective rescue measures.

Using computer vision technology to detect fire has the following implications:

  1. Improve the efficiency of fire detection : The use of computer vision technology can realize automatic fire detection, which greatly improves the efficiency of fire detection.

  1. Shorten the time of fire discovery : The use of computer vision technology can detect fires in time, shorten the time of fire discovery, and provide more rescue time for fire rescue departments.

  1. Reduce casualties and property losses : Computer vision technology can be used to detect fires in time and take corresponding rescue measures to reduce casualties and property losses.

  1. Reduce labor costs : Using computer vision technology can realize automatic fire detection, reduce manual inspection costs, and improve detection efficiency.

  1. Provide technical support for fire prevention and control : use computer vision technology to analyze fire data and provide technical support for fire prevention and control.

To sum up, using computer vision technology to detect fire has important practical significance and social value.

import torch
import cv2
import numpy as np
from pathlib import Path

# 导入YOLOv5模型
model_path = Path('path/to/yolov5')
model = torch.hub.load(model_path, 'custom', path='yolov5s.pt')

# 加载烟火检测图像
img_path = Path('path/to/image')
img = cv2.imread(str(img_path))

# 缩放图像,使其符合模型输入要求
img = cv2.resize(img, (640, 640))

# 图像预处理
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img.transpose(2, 0, 1)
img = img[np.newaxis, ...] / 255.0

# 将图像输入模型进行检测
result = model(torch.tensor(img).float())

# 处理检测结果
boxes = result.xyxy[0].cpu().numpy()
scores = result.conf[0].cpu().numpy()
labels = result.names

# 在图像中绘制检测结果
for box, score, label in zip(boxes, scores, labels):
    if label == 'firework':
        x1, y1, x2, y2 = box.astype(np.int)
        cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
        cv2.putText(img, f'{label}: {score:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

# 显示检测结果
cv2.imshow('image', img[0].transpose(1, 2, 0))
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above code, first use the torch.hub.load() function to load the YOLOv5 model locally, and then use the cv2.imread() function to load the fireworks image to be detected. Next, the image is scaled to a size of 640×640 and preprocessed. Input the preprocessed image into the model for detection, and obtain the coordinates, confidence and category of each detection frame. Finally, the detection results are plotted on the original image and displayed. Among them, the detected fireworks category is 'firework'.

Guess you like

Origin blog.csdn.net/m0_58508552/article/details/129790095