首页 > 解决方案 > 如何在 OpenCv 中读取 YoloV3/YoloV4 以获取检测结果?

问题描述

我有OpenCv版本cv2.__version__= '4.0.1',我正在尝试使用 OpenCv 来使用 Yolov3 和 Yolov4。我使用了来自AlexyAB 的 DarkNet git repo 的权重和配置

Yolov4.config Yolov4.weights

coco.names/classes.txt一样的类

我使用了许多不同的模块,但无法打开模型。

class DLFeaturesExtractor():
    '''
    Classs to Extract features from Image and plot on a Deep Learning Model
    '''
    def __init__(self,weight:str,config:str,cuda:bool=False,size:[tuple,list]=(416,416)):
        '''
        args:
            weight: path of weight file
            config: path to config file
            cuda: whether to use GPU and CudNN algos
            size: size of the input image
        '''
        self.weight = weight
        self.config = config
        net = cv2.dnn.readNet(self.weight,self.config)
        if cuda:
            net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
            net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA_FP16)
        self.model = cv2.dnn_DetectionModel(net)
        self.model.setInputParams(size=size,scale=1/255)

运行代码给我的错误是:

error: OpenCV(4.0.1) C:\ci\opencv-suite_1573470242804\work\modules\dnn\src\darknet\darknet_importer.cpp:207: error: (-212:Parsing error) Failed to parse NetParameter file: yolov4.cfg in function 'cv::dnn::dnn4_v20181221::readNetFromDarknet'

所以我使用了不同的东西:

cv2.dnn.readNetFromDarknet('yolov4.cfg','yolov4.weights',)

给我

error: OpenCV(4.0.1) C:\ci\opencv-suite_1573470242804\work\modules\dnn\src\darknet\darknet_io.cpp:552: error: (-212:Parsing error) Unsupported activation: mish in function 'cv::dnn::darknet::ReadDarknetFromCfgStream'

我认为可能存在参数交换,所以我也这样做了,同样的错误不断出现。

有人可以帮助我如何打开模型并从模型中推断出来吗?

我用这个要点来理解这个想法,但似乎没有任何效果。

标签: pythonopencvdeep-learningcomputer-visionobject-detection

解决方案


请注意,Opencv 4.2.0 和 4.3.0 不支持 Yolov4。尝试下载 master 分支支持 YoloV4 中的最后一个版本(根据KyloEntro

我个人已经将opencv-python升级到4.5.3.56版本,再也没有报错了!


推荐阅读