Ubuntu18.04下基于darknet的YOLOv4环境配置及测试

下载yolov4

git clone https://github.com/AlexeyAB/darknet.git

在这里插入图片描述

编译

进入darknet目录下
在编译darknet前首先需要修改Makefile

cd darknet
gedit Makefile

打开Makefile
在这里插入图片描述
注意

在这里插入图片描述
然后

make

在这里插入图片描述

测试

检测图片

输入下面的命令

./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -thresh 0.25 ./data/dog.jpg

在这里插入图片描述
测试成功!
在这里插入图片描述
终端输出的结果如下,输出了每种类别的置信度
在这里插入图片描述
查看大图
在这里插入图片描述

可基于python进行YOLOv4的inference

注意这里的版本是python3,如果在python2上面就会出现问题
新建一个test.py文件写入:

import os
import cv2
import numpy as np
import random
import darknet
netMain = None
metaMain = None
altNames = None

configPath = "./cfg/yolov4.cfg"
weightPath = "./yolov4.weights"
metaPath = "./cfg/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
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)
# 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)

在这里插入图片描述

查看YOLOv4的检测结果
输入(注意这里的版本是python3,如果在python2上面就会出现问题)

python3 test.py

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

参考博客

1.YOLOv4 资源环境配置和测试样例效果
2.Ubuntu18.04配置darknet环境实现YOLOv4目标检测(三)——基于python进行YOLOv4 inference
3.Ubuntu18.04配置darknet环境实现YOLOv4目标检测(一)——配置YOLOv4环境darknet

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值