欢迎访问宙启技术站
智能推送

利用Python编写DetectionModel()代码实现实时的目标检测

发布时间:2024-01-01 03:52:58

下面是一个使用Python编写的实时目标检测模型的示例代码,可以实时检测摄像头或视频中的目标物体。

import cv2
import numpy as np

class DetectionModel:
    def __init__(self, config_file, weights_file, class_names_file):
        """初始化目标检测模型"""
        self.net = cv2.dnn.readNetFromDarknet(config_file, weights_file)
        self.class_names = []
        with open(class_names_file, 'r') as f:
            self.class_names = [line.strip() for line in f.readlines()]

    def detect_objects(self, image):
        """检测目标物体并返回结果"""
        blob = cv2.dnn.blobFromImage(image, 1/255.0, (416, 416), swapRB=True, crop=False)
        self.net.setInput(blob)
        layer_names = self.net.getLayerNames()
        output_layers = [layer_names[i[0] - 1] for i in self.net.getUnconnectedOutLayers()]
        outs = self.net.forward(output_layers)

        height, width, _ = image.shape
        boxes = []
        confidences = []
        class_ids = []

        for out in outs:
            for detection in out:
                scores = detection[5:]
                class_id = np.argmax(scores)
                confidence = scores[class_id]

                if confidence > 0.5:
                    center_x = int(detection[0] * width)
                    center_y = int(detection[1] * height)
                    w = int(detection[2] * width)
                    h = int(detection[3] * height)

                    x = int(center_x - w / 2)
                    y = int(center_y - h / 2)

                    boxes.append([x, y, w, h])
                    confidences.append(float(confidence))
                    class_ids.append(class_id)

        indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
        results = []
        for i in range(len(boxes)):
            if i in indexes:
                x, y, w, h = boxes[i]
                label = self.class_names[class_ids[i]]
                confidence = confidences[i]
                results.append((label, confidence, (x, y, w, h)))
        return results

# 使用示例
model = DetectionModel('path/to/config_file.cfg', 'path/to/weights_file.weights', 'path/to/class_names_file.txt')

# 打开摄像头
video_capture = cv2.VideoCapture(0)

while True:
    _, frame = video_capture.read()

    # 进行目标检测
    results = model.detect_objects(frame)

    # 在图像中绘制检测结果
    for result in results:
        label, confidence, (x, y, w, h) = result
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        cv2.putText(frame, f'{label}: {confidence:.2f}', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)

    # 展示图像
    cv2.imshow('Object Detection', frame)

    # 退出循环
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# 释放摄像头并销毁窗口
video_capture.release()
cv2.destroyAllWindows()

使用这个示例代码可以实时运行目标检测模型并在图像中绘制检测结果。你需要提供一个YOLOv3目标检测模型的配置文件(config_file)、权重文件(weights_file),以及类别名称文件(class_names_file)。可以从YOLO官方网站下载预训练好的模型及相关文件。

在代码中,我们首先初始化了一个DetectionModel类的实例,然后使用摄像头获取图像,并应用目标检测模型进行检测。最后,我们在图像中绘制检测结果,并通过按下“q”键退出循环。

希望这个示例能对你理解如何使用Python实现实时目标检测有所帮助。