如何把yolov3封装到Docker里面运行 | Dockerfile的编写与编译

主要还是用yolov3-dnn这一套去调用,分两个版本,一个是CPU版,一个是GPU版。

一、CPU版本的Dockerfile怎么写

其实CPU下面的很简单,需要一个requirements,系统环境其实限制不大。

requirements.txt :

opencv-python==4.2.0.32
numpy

然后说一下项目目录都有哪些东西:

/docker-yolo
    /cfg
        voc.names
        yolov3.cfg
        yolov3.weights
    Dockerfile
    requirements.txt
    yolo.py

其中cfg下面的东西如果你看过我的其他帖子应该很容易明白从哪里获得这些文件。

下面说一下yolo.py,这个东西也是之前的flask那个版本里面做出来的,去掉了flask相关的东西,把YOLO直接写成一个函数:(代码不可运行,我屏蔽了一些不方便放出来的东西)

# -*- coding: utf-8 -*-#
import os
import cv2 as cv
import cv2
import time
import json
import numpy as np

def initialization():
    yolo_dir = './cfg'
    weightsPath_1 = os.path.join(yolo_dir, 'yolov3_1.weights')
    configPath_1 = os.path.join(yolo_dir, 'yolov3_1.cfg')
    labelsPath_1 = os.path.join(yolo_dir, 'voc_x.names')
    CONFIDENCE_1 = 0.50
    THRESHOLD_1 = 0.45
    net_2 = cv.dnn.readNetFromDarknet(configPath_1, weightsPath_1)
    weightsPath_2 = os.path.join(yolo_dir, 'yolov3_2.weights')
    configPath_2 = os.path.join(yolo_dir, 'yolov3_2.cfg')
    labelsPath_2 = os.path.join(yolo_dir, 'coco_origin.names')
    CONFIDENCE_2 = 0.50
    THRESHOLD_2 = 0.45
    net_1 = cv.dnn.readNetFromDarknet(configPath_2, weightsPath_2)
    return net_2,net_1,CONFIDENCE_1,THRESHOLD_1,CONFIDENCE_2,THRESHOLD_2,labelsPath_1,labelsPath_2
def compute_iou(rec1, rec2):
        pass
def NMS_2th(a,thresh):
        pass

def yolo(imgPath,net_2,net_1,CONFIDENCE_1,THRESHOLD_1,CONFIDENCE_2,THRESHOLD_2,labelsPath_1,labelsPath_2):
    s = time.time()
    img = cv.imread(imgPath)
    blobImg = cv.dnn.blobFromImage(img, 1.0/255.0, (416, 416), None, True, False)
    net_2.setInput(blobImg)
    # For GPU
    # net_2.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
    # net_2.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
    outInfo = net_2.getUnconnectedOutLayersNames()
    layerOutputs = net_2.forward(outInfo)
    (H, W) = img.shape[:2]
    boxes = []
    confidences = []
    classIDs = []
    for out in layerOutputs:
        for detection in out:
            scores = detection[5:]
            classID = np.argmax(scores)
            confidence = scores[classID]
            if confidence > CONFIDENCE_1:
                box = detection[0:4] * np.array([W, H, W, H])  
                (centerX, centerY, width, height) = box.astype("int")
                x = int(centerX - (width / 
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值