yolov4模型使用脚本

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



# 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)




image_name = 'data/dog.jpg'

image_name = os.path.join(imgpath,imgna)
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)

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 = '{} {:.2f}'.format(label,conf)
   plot_one_box(xyxy, src_img, label=label, color=colors[index % metaMain.classes])
# cv2.imwrite('result.jpg', src_img)
src_img = cv2.resize(src_img,(int(width*0.5),int(height*0.5)))
cv2.imshow('result',src_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值