YOLO数据集自动标注(预标注)

更新时间:2023-07-13 19:14:50 阅读: 评论:0

YOLO数据集⾃动标注(预标注)最近标注,数据集⼀直很头疼,所以想着预训练下然后⾃动⽣成再微调数据集会省事很多,
虽然达不到很好的效果,⾃动标注的情况主要还是看识别的准确率怎么样
下⾯做下笔记记录,将预测出来的类别直接⽣成xml⽂件,省去重复标注的过程,只需要⼿动微调就⾏
import cv2
import numpy as np
长颈鹿的英文import time,os
import xml.dom.minidom
ElementTree as ET
from xml.dom.minidom import Document
FONT = cv2.FONT_HERSHEY_PLAIN
# 定义检测的置信阈值.
conf_thresh = 0.5
time_start = time.time()
class xmlvalue():
def __init__(lf,fname,folder,path,img):
"""
:param fname: 图⽚名称
:param folder: ⽗级⽂件夹
:param p: 图⽚路径
:param img: 图⽚cooperate
#:param label: 类别词典,(类别:标注框)
"""
lf.filname = fname
lf.folder = folder
lf.path = path
lf.Img = img
def tlabel(lf, label):
lf.Label = label
# -------------------------类别-----------------------
def labelobject(doc,DOCUMENT,labelname,coor):
object = ateElement('object')
punchin# object.ateTextNode("JPEGImages"))
DOCUMENT.appendChild(object)
name = ateElement('name')
name.ateTextNode(str(labelname)))
object.appendChild(name)
po = ateElement('po')
po.ateTextNode("Unspecified"))
object.appendChild(po)
truncated = ateElement('truncated')
truncated.ateTextNode("0"))
object.appendChild(truncated)
difficult = ateElement('difficult')
difficult.ateTextNode("0"))
object.appendChild(difficult)
# """------------------*******坐标*******---------------------------"""
bndbox = ateElement('bndbox')
object.appendChild(bndbox)
xmin = ateElement('xmin')
xmin.ateTextNode(str(coor[1])))
bndbox.appendChild(xmin)
ymin = ateElement('ymin')幼儿园园长六一致辞
ymin.ateTextNode(str(coor[2])))
bndbox.appendChild(ymin)
xmax = ateElement('xmax')
xmax.ateTextNode(str(coor[3])))
bndbox.appendChild(xmax)
ymax = ateElement('ymax')
ymax.ateTextNode(str(coor[4])))
bndbox.appendChild(ymax)
return object
def creatxml(Cxml):
doc = Document()  # 创建DOM⽂档对象
DOCUMENT = ateElement('annotation')  # 创建根元素
# DOCUMENT.tAttribute('content_method',"full")#设置命名空间
# DOCUMENT.tAttribute('xsi:noNamespaceSchemaLocation','DOCUMENT.xsd')#引⽤本地XML Schema
出国外语培训
doc.appendChild(DOCUMENT)
>>##item:Python处理XML之Minidom>>>#
# 创建节点
folder = ateElement('folder')
# 添加值
folder.ateTextNode(Cxml.folder))
# 添加⾄⽗节点中
DOCUMENT.appendChild(folder)
filename = ateElement('filename')
filename.ateTextNode(Cxml.filname))
DOCUMENT.appendChild(filename)
path = ateElement('path')
path.ateTextNode(Cxml.path))
DOCUMENT.appendChild(path)
"""
<source>
日本语
<databa>Unknown</databa>
</source>
"""
"""----------------***source***-----------------------"""
地毯的英文
source = ateElement('source')
DOCUMENT.appendChild(source)
芳草地小学怎么样
databa = ateElement('databa')
databa.ateTextNode("Unknown"))
source.appendChild(databa)
"""----------------***size***-----------------------"""
size = ateElement('size')
DOCUMENT.appendChild(size)
imgshape = Cxml.Img.shape
width = ateElement('width')
width.ateTextNode(str(imgshape[1])))
size.appendChild(width)
height = ateElement('height')
height.ateTextNode(str(imgshape[0])))
size.appendChild(height)
depth = ateElement('depth')
depth.ateTextNode(str(imgshape[2])))
size.appendChild(depth)
"""
<gmented>0</gmented>
"""
gmented = ateElement('gmented')
gmented.ateTextNode("0"))
DOCUMENT.appendChild(gmented)
for value in Cxml.Label:
DOCUMENT.appendChild(labelobject(doc,DOCUMENT, value[0],value))
proofread
>># 将DOM对象doc写⼊⽂件
xmlname = Cxml.path[:-3]+"xml"
print("save to :" ,xmlname)
f = open(xmlname, 'w')
# f.prettyxml(indent = '\t', newl = '\n', encoding = 'utf-8'))
doc.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
f.clo()
def readimg(frame,Cxml):
frame_id = 0
frame_id += 1
# frame = size(frame, (640, 480))
blob = cv2.dnn.blobFromImage(frame, 1 / 255, (416, 416), swapRB=True)
net.tInput(blob)
outs = net.forward(output_layers)
confidences = []
class_ids = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > conf_thresh:
height, width, channels = frame.shape
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = int(center_x - w / 2)
y = int(center_y - h / 2)
confidences.append(float(confidence))
class_ids.append(class_id)
boxes.append([x, y, w, h])
indexes = cv2.dnn.NMSBoxes(boxes, confidences, conf_thresh, 0.4)
mdict = []
for i in range(len(boxes)):
# If the box remained after NMS.
if i in indexes:
x, y, w, h = boxes[i]
label = str(class[class_ids[i]])
mlist = [label,x,y,x+w,y+h]
mdict.append(mlist)
confidence = confidences[i]
color = colors[class_ids[i]]
# Draw the box.
cv2.putText(frame,  label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2)
Cxml.tlabel(mdict)
creatxml(Cxml)
elapd_time = time.time() - time_start
fps = frame_id / elapd_time
cv2.putText(frame, "FPS: " + str(round(fps, 2)), (8, 30), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 0), 2)
# cv2.imshow("Camera", frame)
最好的在线翻译
# cv2.waitKey(2)
if __name__ == '__main__':
JPEGpath = 'image'
# 测试图⽚,
path = os.getcwd()
path = os.path.join(path,JPEGpath)
net = adNet("./model/yolov4-tiny.weights", "./model/yolov4-tiny.cfg")
layer_names = LayerNames()
output_layers = [layer_names[i[0] - 1] for i UnconnectedOutLayers()]
with open("./model/coco.names", "r") as f:
class = [line.strip() for line adlines()]
  colors = np.random.uniform(0, 255, size=(len(class), 3))
for i in os.listdir(path): dswith('jpg') dswith('png'): zpath = os.path.join(path, i) img = cv2.imread(zpath) # newpath = place('test', 'voc2007de') Cxml = xmlvalue(i,JPEGpath,zpath,img) readimg(img,Cxml) # rtsp = "rtsp://admin:cqi   

本文发布于:2023-07-13 19:14:50,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/176385.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:标注   创建   类别
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图