Colab中使用
在Colab中使用自行訓練的Yolo模型,需用到自行訓練的權重檔及需偵測的圖片。
上傳自行訓練的權重檔<yolov3-obj_10000.weights>到Google Drive雲端硬碟<Colab Notebooks>資料夾
將圖片<資料夾>也上傳至<Colab Notebooks>,假設資料夾名為images內含多張圖片。
並執行以下程式
!git clone https://github.com/AlexeyAB/darknet.git
%cd darknet
!sed -i "s/GPU=0/GPU=1/g" Makefile
!sed -i "s/CUDNN=0/CUDNN=1/g" Makefile
!sed -i "s/OPENCV=0/OPENCV=1/g" Makefile
!make
%cd /content
!ln -s "/content/drive/My Drive/Colab Notebooks" /godrive
!cp /godrive/yolov3_colab.zip /content
!unzip yolov3_colab.zip
!/content/darknet/darknet detector test cfg/obj.data cfg/yolov3-obj.cfg /godrive/yolov3-obj_10000.weights /godrive/images/test01.jpg
from PIL import Image
Image.open('predictions.jpg')
※test01.jpg為圖片名稱
執行後會將執行結果儲存於目前所在目錄的<predictions.jpg>圖片中,最後2列程式為顯示圖片
Python中使用
將需要使用的權重檔複製至<D:\darknetYolo\test\cfg\weights>資料夾中。
將圖片資料夾複製到<D:\darknetYolo\test>資料夾中,假設資料夾名為images。
打開Spyder並命名為detect,並將路徑設為<D:\darknetYolo\test>
<detect.py>
import cv2
import numpy as np
net = cv2.dnn.readNetFromDarknet("cfg/yolov3-obj.cfg","cfg/weights/yolov3_last .weights") #讀取模型
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] #輸出圖形規格
classes = [line.strip() for line in open("cfg/obj.names")] #使用自己的分類標籤檔
colors = [(255,0,0), (0,255,0), (0,0,255), (127,0,255)] #框選顏色
img = cv2.imread("/images/test.jpg") #讀取圖片
height, width, channels = img.shape
blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), (0, 0, 0), True, crop=False) #圖形預處理以符合輸入圖片規格
net.setInput(blob) #圖片輸入模型
outs = net.forward(output_layers) #偵測結果
class_ids = [] #存標籤索引
confidences = [] #存信心指數
boxes = [] #存矩形坐標
for out in outs:
for detection in out:
tx, ty, tw, th, confidence = detection[0:5] #取得坐標及信心資料
scores = detection[5:]
class_id = np.argmax(scores) #取得標籤索引
if confidence > 0.3: #信心指數大於0.3才算
center_x = int(tx * width)
center_y = int(ty * height)
w = int(tw * width)
h = int(th * height)
# 取得箱子方框座標
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.3, 0.4) #消除重疊框選
font = cv2.FONT_HERSHEY_PLAIN
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
color = colors[class_ids[i]%5]
cv2.rectangle(img, (x, y), (x + w, y + h), color, 1)
cv2.putText(img, label, (x, y - 5), font, 1, color, 2)
cv2.imshow('win', img)
cv2.waitKey(0)
cv2.destroyAllWindows()