1:下载darknet到本地环境

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

或者

git clone https://codechina.csdn.net/mirrors/pjreddie/darknet?utm_source=csdn_github_accelerator

2:下载预测训练模型

git clone https://pjreddie.com/media/files/yolov3.weights

3:创建python项目 yolo-day01 

4:将darknet目录中的cfg目录下的coco.names文件和yolov3.cfg文件导入到yolo-day01根目录下

5:将预测训练模型yolov3.weights文件导入到yolo-day01根目录下

6:创建01-main.py文件目录结构如下:

7:安装opencv-python依赖库

pip install opencv-python

8:将如下代码导入到py文件中并和运行


import cv2 as cv
import numpy as np

# config 阔值
confThreshold = 0.25
nsmThreshold = 0.40
inpWidth = 416
inpHeight = 416

classesFile = 'coco.names'
classes = None

with open(classesFile, 'rt') as f:
    classes = f.read().rstrip("\n").split("\n")

modelConf = 'yolov3.cfg'
modelWeights = 'yolov3.weights'

net = cv.dnn.readNetFromDarknet(modelConf, modelWeights)
net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)

winName = 'DL OD with OpenCv'
cv.namedWindow(winName, cv.WINDOW_NORMAL)
cv.resizeWindow(winName, 1000, 1000)

cap = cv.VideoCapture(0)


def getOutputsNames(net):
    layersNames = net.getLayerNames()
    print([i for i in net.getUnconnectedOutLayers()])
    return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]


def postprocess(frame, outs):
    frameWidth = frame.shape[0]
    frameHeight = frame.shape[1]

    classIDs = []
    confidences = []
    boxes = []

    for out in outs:
        for detection in out:
            scores = detection[5:]
            classID = np.argmax(scores)
            confidence = scores[classID]

            if confidence > confThreshold:
                centerX = int(detection[0] * frameWidth)
                centerY = int(detection[1] * frameHeight)

                width = int(detection[2] * frameWidth)
                height = int(detection[3] * frameHeight)

                left = int(centerX - width / 2)
                top = int(centerY - height / 2)

                classIDs.append(classID)
                confidences.append(float(confidence))
                boxes.append([left, top, width, height])

    indices = cv.dnn.NMSBoxes(boxes, confidences, confThreshold, nsmThreshold)

    for i in indices:
        i = i[0]
        box = boxes[i]
        left = box[0]
        top = box[1]
        width = box[2]
        height = box[3]

        drawPred(classIDs[i], confidences[i], left, top, left + width, top + height)


def drawPred(classID, conf, left, top, right, bottom):
    cv.rectangle(frame, (left, top), (right, bottom), (255, 178, 50), 3)
    label = "%.2f" % conf

    if classes:
        assert (classID < len(classes))
        label = "%s:%s" % (classes[classID], label)

    cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 3)


while cv.waitKey(1) < 0:
    hasFrame, frame = cap.read()

    blob = cv.dnn.blobFromImage(frame, 1 / 255, (inpWidth, inpHeight), [0, 0, 0], crop=False)

    net.setInput(blob)
    outs = net.forward(getOutputsNames(net))

    postprocess(frame, outs)

    cv.imshow(winName, frame)

运行结果:

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐