原文: YOLOV3 基于OpenCV DNN 的目标检测实现 - AIUAI
这里主要是对 基于 YOLOV3 和 OpenCV的目标检测(PythonC++)[译] Python 完整实现的整理.
OpenCV DNN支持图像分类、对象检测、图像分割常见通用网络模型.
1. YOLOV3 简述
YOLOV3 网络结构如图:
YOLOV3 采用全卷积结构,有效减少了模型参数总数,取消了softmax层,模型输出结构如图:
2. YOLOV3 DNN 实现
模型下载:
# coco.names
wget https://github.com/pjreddie/darknet/blob/master/data/coco.names?raw=true -O ./coco.names
# YOLOV3
wget https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg?raw=true -O ./yolov3.cfg
wget https://pjreddie.com/media/files/yolov3.weights
# YOLOV3-tiny
wget https://github.com/pjreddie/darknet/blob/master/cfg/yolov3-tiny.cfg
wget https://pjreddie.com/media/files/yolov3-tiny.weights
完整实现:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import cv2
import sys
import numpy as np
import os
import matplotlib.pyplot as plt
import time
class general_yolov3(object):
def __init__(self, modelpath, is_tiny=False):
self.conf_threshold = 0.5 # Confidence threshold
self.nms_threshold = 0.4 # NMS threshold
self.net_width = 416 # 网络输入图像宽度
self.net_height = 416 # 网络输入图像高度
self.classes = self.get_coco_names()
self.yolov3_model = self.get_yolov3_model(modelpath, is_tiny)
self.outputs_names = self.get_outputs_names()
def get_coco_names(self):
# COCO 物体类别名
classesFile = "/path/to/coco.names"
classes = None
with open(classesFile, 'rt') as f:
classes = f.read().rstrip('\n').split('\n')
return classes
def get_yolov3_model(self, modelpath, is_tiny):
if is_tiny:
cfg_file = os.path.join(modelpath, "yolov3-tiny.cfg