现在手中数据标签只有Mask,做检测任务需要将其转化为bbox的pascal格式、coco格式,任务如下:
完整代码稍后更新到github
Step1 : 根据mask生成外围框
########## for test
image = cv2.imread('00080.png')
# 灰度图像
gray = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2GRAY)
# contours记录的是所有的框的信息,
ret, thresh = cv2.threshold(gray, 127, 255,cv2.THRESH_BINARY )
thresh_cont, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 每次输出框的坐标(x,y,w,h),并画出带框图片
for i in range(len(contours)):
x, y, w, h = cv2.boundingRect(contours[i])
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imwrite('test.png', image)
Step2 : 记录框的坐标并转化为pascal\coco格式
- pascal格式
######## 生成xml文件
import xml.etree.cElementTree as ET
import numpy as np
import os
import cv2
def beatau(e,level=0):
if len(e)>0:
e.text='\n'+'\t'*(level+1)
for child in e:
beatau(child,level+1)
child.tail=child.tail[:-1]
e.tail='\n' + '\t'*level
def ToXml(name, contours):
root = ET.Element('annotation')#根节点
erow1