Ubuntu18.04配置darknet环境实现YOLOv4目标检测(三)——基于python进行YOLOv4 inference


完成第一篇 darknet环境配置后,可基于python进行YOLOv4的inference,查看YOLOv4的检测结果。需要用到的库有 darknet目录下的 darknet.py文件,以及编译出来的 libdarknet.so

1. 需要用的的库

import os
import cv2
import numpy as np
import random
import darknet

需要导入的库主要是opencv-python和darknet,darknet即darknet.py文件。

2. 加载网络

加载网络需要读取cfg文件,weights文件,data文件和names文件,如果缺少文件,会报读取失败的错误。yolov4.cfgcoco.data文件可以在darknet文件夹下的cfg文件夹中找到,coco.names文件可以在data文件夹中找到,yolov4.weights文件可以在这篇文章中介绍的网盘中下载。

netMain = None
metaMain = None
altNames = None

configPath = "./cfg/yolov4.cfg"
weightPath = "./weights/yolov4.weights"
metaPath = "./data/coco.data"
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 = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1)  # batch size = 1
if metaMain is None:
    metaMain = darknet.load_meta(metaPath.encode("ascii"))
if altNames is 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

3. 加载图片

首先通过opencv读取图片,读取图片之后需要将图片由BGR格式转为RGB格式,接着resize到608x608大小并将图片转为darknet的IMAGE类型,输入到网络之后,检测结果保存在detections里面。

image_name = './data/dog.jpg'
src_img = cv2.imread(image_name)
bgr_img = src_img[:, :, ::-1]
height, width = bgr_img.shape[:2]
rsz_img = cv2.resize(bgr_img, (darknet.network_width(netMain), darknet.network_height(netMain)),
                     interpolation=cv2.INTER_LINEAR)
darknet_image, _ = darknet.array_to_image(rsz_img)
detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25)

4. 可视化结果

需要先将检测结果转为x1y1x2y2的形式,再使用opencv的rectangle函数画图。

# convert xywh to xyxy
def convert_back(x, y, w, h):
    xmin = int(round(x - (w / 2)))
    xmax = int(round(x + (w / 2)))
    ymin = int(round(y - (h / 2)))
    ymax = int(round(y + (h / 2)))
    return xmin, ymin, xmax, ymax


# Plotting functions
def plot_one_box(x, img, color=None, label=None, line_thickness=None):
    # Plots one bounding box on image img
    tl = line_thickness or round(0.001 * max(img.shape[0:2])) + 1  # line thickness
    color = color or [random.randint(0, 255) for _ in range(3)]
    c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
    cv2.rectangle(img, c1, c2, color, thickness=tl)
    if label:
        tf = max(tl - 1, 1)  # font thickness
        t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
        c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
        cv2.rectangle(img, c1, c2, color, -1)  # filled
        cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)


random.seed(1)
colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(metaMain.classes)]
for detection in detections:
    x, y, w, h = detection[2][0], \
                 detection[2][1], \
                 detection[2][2], \
                 detection[2][3]
    conf = detection[1]
    x *= width / darknet.network_width(netMain)
    w *= width / darknet.network_width(netMain)
    y *= height / darknet.network_height(netMain)
    h *= height / darknet.network_height(netMain)
    xyxy = np.array([x - w / 2, y - h / 2, x + w / 2, y + h / 2])
    label = detection[0].decode()
    index = altNames.index(label)
    label = f'{label} {conf:.2f}'
    plot_one_box(xyxy, src_img, label=label, color=colors[index % metaMain.classes])
cv2.imwrite('result.jpg', src_img)

最终结果保存在result.jpg图片里,结果如下图所示。
result.jpg
结果显示图片中dog的置信度为0.98,bicycle的置信度为0.92,truck的置信度为0.92,pottedplant的置信度没有显示,通过运行代码可知为0.34。通过darknet命令./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -thresh 0.25 ./data/dog.jpg运行的结果如下,结果基本一致。

data/dog.jpg: Predicted in 21.041000 milli-seconds.
bicycle: 92%
dog: 98%
truck: 92%
pottedplant: 33%

猜你喜欢

转载自blog.csdn.net/linghu8812/article/details/105750434