Darknet-YoloV4空预测

2024-05-28 23:48:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用YoloV4和Darknet在python中检测图像中的对象。 问题是它没有检测到图像中的任何对象。这是我的代码:

configPath = "./cfg/yolov4.cfg"                                 
weightPath = "./yolov4.weights"                                 
metaPath = "./cfg/coco.data"

netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1)
metaMain = darknet.load_meta(metaPath.encode("ascii"))
altNames = None
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

frame_width = darknet.network_width(netMain)
frame_height = darknet.network_height(netMain)

frame = cv2.imread("b.jpg")

darknet_image = darknet.make_image(frame_width, frame_height, 3)
darknet.copy_image_from_bytes(darknet_image, frame.tobytes())
detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25)
print(detections) #returns []

程序打印“[]”。图像(“b.jpg”)是764x756


Tags: 对象图像imagerematchasciiresultwidth
1条回答
网友
1楼 · 发布于 2024-05-28 23:48:25

您需要以适当的大小调整图像的大小

frame = cv2.resize(cv2.imread("b.jpg"), (frame_width, frame_height))

确保图像中有您要查找的对象

在使用python脚本之前,请使用darknet test确保模型可以检测对象

相关问题 更多 >

    热门问题