主要还是用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 /