darknet-yolov4 python接口封装

class DarknetYolov4:
    def __init__(self,configPath,weightPath,metaPath,thresh):
        # Import the global variables. This lets us instance Darknet once, then just call performDetect() again without instancing again
        global metaMain, netMain, altNames  # pylint: disable=W0603
        assert 0 < thresh < 1, "Threshold should be a float between zero and one (non-inclusive)"
        if not os.path.exists(configPath):
            raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`")
        if not os.path.exists(weightPath):
            raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`")
        if not os.path.exists(metaPath):
            raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`")
        if netMain is None:
            netMain = load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1)  # batch size = 1
        if metaMain is None:
            metaMain = load_meta(metaPath.encode("ascii"))
        if altNames is None:
            # In Python 3, the metafile default access craps out on Windows (but not Linux)
            # Read the names file and create a list to feed to detect
            try:
                with open(metaPath) as metaFH:
                    metaContents = metaFH.read()
                    import re
                    match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE)
                    if match:
                        result = match.group(1)
                    else:
                        result = None
                    try:
                        if os.path.exists(result):
                            with open(result) as namesFH:
                                namesList = namesFH.read().strip().split("\n")
                                altNames = [x.strip() for x in namesList]
                    except TypeError:
                        pass
            except Exception:
                pass

        self.netMain=netMain
        self.metaMain=metaMain
        self.thresh=thresh



    def getdetresults(self,imagePath):
        def createdictlist(dictlist, key, newlist):
            if key in dictlist.keys():
                dictlist[key].append(newlist)
            else:
                dictlist[key] = [newlist]

        netMain, metaMain, thresh = self.netMain, self.metaMain, self.thresh
        detections = detect(netMain, metaMain, imagePath.encode("ascii"), thresh)

        detresults={}

        for detection in detections:
            label = detection[0]
            confidence = detection[1]
            bounds = detection[2]

            yExtent = int(bounds[3])
            xEntent = int(bounds[2])
            # Coordinates are around the center
            xCoord = int(bounds[0] - bounds[2] / 2)
            yCoord = int(bounds[1] - bounds[3] / 2)

            scorex1y1x2y2=[round(confidence,2),xCoord, yCoord,xCoord + xEntent, yCoord + yExtent]

            createdictlist(detresults, label, scorex1y1x2y2)

        return detresults

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值