-
大小: 5KB文件類型: .py金幣: 1下載: 0 次發(fā)布日期: 2021-05-13
- 語言: Python
- 標(biāo)簽: 深度學(xué)習(xí)??自動標(biāo)注??目標(biāo)檢測??
資源簡介
該腳本可以通過opencv的dnn模塊調(diào)用darknet模型對數(shù)據(jù)進(jìn)行自動標(biāo)注,生成xml文件,使用時需要自己修改路徑。
代碼片段和文件信息
#!/usr/bin/python
#?-*-?coding=utf-8?-*-
#?author?:?DongLZ
#?date:?2020-05-22
from?xml.etree?import?ElementTree?as?ET
import?cv2
import?os
import?numpy?as?np
from?os?import?getcwd
imgdir?=?‘img‘?
IMAGES_LIST=os.listdir(imgdir)?#?圖片路徑
weightsPath?=?“./face/face.weights“
configPath?=?“./face/face.cfg“
labelsPath?=?“./face/face.names“
LABELS?=?open(labelsPath).read().strip().split(“\n“)
print(LABELS)
net?=?cv2.dnn.readNetFromDarknet(configPath?weightsPath)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
def?coordinate_get(img):
????coordinates_list=[]?#?創(chuàng)建坐標(biāo)列表
????boxes?=?[]
????confidences?=?[]
????classIDs?=?[]
????(H?W)?=?img.shape[:2]
????#?得到?YOLO需要的輸出層
????ln?=?net.getlayerNames()
????ln?=?[ln[i[0]?-?1]?for?i?in?net.getUnconnectedOutlayers()]
????#?從輸入圖像構(gòu)造一個blob,然后通過加載的模型,給我們提供邊界框和相關(guān)概率
????blob?=?cv2.dnn.blobFromImage(img?1?/?255.0?(416?416)?swapRB=True?crop=False)
????net.setInput(blob)
????layerOutputs?=?net.forward(ln)
????#?在每層輸出上循環(huán)
????for?output?in?layerOutputs:
????????#?對每個檢測進(jìn)行循環(huán)
????????for?detection?in?output:
????????????scores?=?detection[5:]
????????????classID?=?np.argmax(scores)
????????????confidence?=?scores[classID]
????????????#?過濾掉那些置信度較小的檢測結(jié)果
????????????if?confidence?>?0.01:
????????????????#?框后接框的寬度和高度
????????????????box?=?detection[0:4]??*?np.array([W?H?W?H])
????????????????(centerX?centerY?width?height)?=?box.astype(“int“)
????????????????#?邊框的左上角
????????????????x?=?int(centerX?-?(width?/?2))
????????????????y?=?int(centerY?-?(height?/?2))
????????????????#?更新檢測出來的框
????????????????boxes.append([x?y?int(width)?int(height)])
????????????????confidences.append(float(confidence))
????????????????classIDs.append(classID)?
????idxs?=?cv2.dnn.NMSBoxes(boxes?confidences?0.2?0.3)
????if?len(idxs)?>?0:
????????for?i?in?idxs.flatten():
????????????(x?y)?=?(boxes[i][0]?boxes[i][1])
????????????(w?h)?=?(boxes[i][2]?boxes[i][3])
????????????xmin?=?int(x)
????????????ymin?=?int(y)
????????????xmax?=?int(x?+?w)
????????????ymax?=?int(y?+?h)
????????????coordinates_list.append([xminyminxmaxymaxclassIDs[i]])
????return?coordinates_list
#創(chuàng)建一級分支object
def?create_object(rootxiyixayaobj_name):???#?參數(shù)依次,樹根,xmin,ymin,xmax,ymax
????#創(chuàng)建一級分支object
????_object=ET.SubElement(root‘object‘)
????#創(chuàng)建二級分支
????name=ET.SubElement(_object‘name‘)
?
- 上一篇:DynaSLAM/src/python/
- 下一篇:爬取b站前百視頻名稱和鏈接
評論
共有 條評論