|
|
@@ -19,8 +19,6 @@ import os.path as osp
|
|
|
import random
|
|
|
import re
|
|
|
import numpy as np
|
|
|
-import cv2
|
|
|
-import json
|
|
|
from collections import OrderedDict
|
|
|
import xml.etree.ElementTree as ET
|
|
|
import paddlex.utils.logging as logging
|
|
|
@@ -72,24 +70,22 @@ class VOCDetection(Dataset):
|
|
|
annotations['categories'] = []
|
|
|
annotations['annotations'] = []
|
|
|
|
|
|
- self.cname2cid = OrderedDict()
|
|
|
- self.cid2cname = OrderedDict()
|
|
|
+ cname2cid = OrderedDict()
|
|
|
label_id = 1
|
|
|
with open(label_list, 'r', encoding=get_encoding(label_list)) as fr:
|
|
|
for line in fr.readlines():
|
|
|
- self.cname2cid[line.strip()] = label_id
|
|
|
- self.cid2cname[label_id] = line.strip()
|
|
|
+ cname2cid[line.strip()] = label_id
|
|
|
label_id += 1
|
|
|
self.labels.append(line.strip())
|
|
|
logging.info("Starting to read file list from dataset...")
|
|
|
- for k, v in self.cname2cid.items():
|
|
|
+ for k, v in cname2cid.items():
|
|
|
annotations['categories'].append({
|
|
|
'supercategory': 'component',
|
|
|
'id': v,
|
|
|
'name': k
|
|
|
})
|
|
|
ct = 0
|
|
|
- self.ann_ct = 0
|
|
|
+ ann_ct = 0
|
|
|
with open(file_list, 'r', encoding=get_encoding(file_list)) as fr:
|
|
|
while True:
|
|
|
line = fr.readline()
|
|
|
@@ -150,7 +146,9 @@ class VOCDetection(Dataset):
|
|
|
name_tag = pattern.findall(str(ET.tostringlist(obj)))[0][
|
|
|
1:-1]
|
|
|
cname = obj.find(name_tag).text.strip()
|
|
|
- gt_class[i][0] = self.cname2cid[cname]
|
|
|
+ if cname in ['bu_dao_dian', 'jiao_wei_lou_di']:
|
|
|
+ cname = 'lou_di'
|
|
|
+ gt_class[i][0] = cname2cid[cname]
|
|
|
pattern = re.compile('<difficult>', re.IGNORECASE)
|
|
|
diff_tag = pattern.findall(str(ET.tostringlist(obj)))[0][
|
|
|
1:-1]
|
|
|
@@ -191,11 +189,11 @@ class VOCDetection(Dataset):
|
|
|
'image_id': int(im_id[0]),
|
|
|
'bbox': [x1, y1, x2 - x1 + 1, y2 - y1 + 1],
|
|
|
'area': float((x2 - x1 + 1) * (y2 - y1 + 1)),
|
|
|
- 'category_id': self.cname2cid[cname],
|
|
|
- 'id': self.ann_ct,
|
|
|
+ 'category_id': cname2cid[cname],
|
|
|
+ 'id': ann_ct,
|
|
|
'difficult': _difficult
|
|
|
})
|
|
|
- self.ann_ct += 1
|
|
|
+ ann_ct += 1
|
|
|
|
|
|
im_info = {
|
|
|
'im_id': im_id,
|
|
|
@@ -273,162 +271,6 @@ class VOCDetection(Dataset):
|
|
|
self.file_list.append([im_fname, coco_rec])
|
|
|
self.num_samples = len(self.file_list)
|
|
|
|
|
|
- def generate_image(self, templates, background, save_dir='dataset_clone'):
|
|
|
- """将目标物体粘贴在背景图片上生成新的图片,并加入到数据集中
|
|
|
-
|
|
|
- Args:
|
|
|
- templates (list|tuple):可以将多张图像上的目标物体同时粘贴在同一个背景图片上,
|
|
|
- 因此templates是一个列表,其中每个元素是一个dict,表示一张图片的目标物体。
|
|
|
- 一张图片的目标物体有`image`和`annos`两个关键字,`image`的键值是图像的路径,
|
|
|
- 或者是解码后的排列格式为(H, W, C)且类型为uint8且为BGR格式的数组。
|
|
|
- 图像上可以有多个目标物体,因此`annos`的键值是一个列表,列表中每个元素是一个dict,
|
|
|
- 表示一个目标物体的信息。该dict包含`polygon`和`category`两个关键字,
|
|
|
- 其中`polygon`表示目标物体的边缘坐标,例如[[0, 0], [0, 1], [1, 1], [1, 0]],
|
|
|
- `category`表示目标物体的类别,例如'dog'。
|
|
|
- background (dict): 背景图片可以有真值,因此background是一个dict,包含`image`和`annos`
|
|
|
- 两个关键字,`image`的键值是背景图像的路径,或者是解码后的排列格式为(H, W, C)
|
|
|
- 且类型为uint8且为BGR格式的数组。若背景图片上没有真值,则`annos`的键值是空列表[],
|
|
|
- 若有,则`annos`的键值是由多个dict组成的列表,每个dict表示一个物体的信息,
|
|
|
- 包含`bbox`和`category`两个关键字,`bbox`的键值是物体框左上角和右下角的坐标,即
|
|
|
- [x1, y1, x2, y2],`category`表示目标物体的类别,例如'dog'。
|
|
|
- save_dir (str):新图片及其标注文件的存储目录。默认值为`dataset_clone`。
|
|
|
-
|
|
|
- """
|
|
|
- if not osp.exists(save_dir):
|
|
|
- os.makedirs(save_dir)
|
|
|
- image_dir = osp.join(save_dir, 'JPEGImages_clone')
|
|
|
- anno_dir = osp.join(save_dir, 'Annotations_clone')
|
|
|
- json_path = osp.join(save_dir, "annotations.json")
|
|
|
- logging.info("Gegerated images will be saved in {}".format(image_dir))
|
|
|
- logging.info(
|
|
|
- "Annotation of generated images will be saved as xml files in {}".
|
|
|
- format(anno_dir))
|
|
|
- logging.info(
|
|
|
- "Annotation of images (loaded before and generated now) will be saved as a COCO json file {}".
|
|
|
- format(json_path))
|
|
|
- if not osp.exists(image_dir):
|
|
|
- os.makedirs(image_dir)
|
|
|
- if not osp.exists(anno_dir):
|
|
|
- os.makedirs(anno_dir)
|
|
|
-
|
|
|
- num_objs = len(background['annos'])
|
|
|
- for temp in templates:
|
|
|
- num_objs += len(temp['annos'])
|
|
|
-
|
|
|
- gt_bbox = np.zeros((num_objs, 4), dtype=np.float32)
|
|
|
- gt_class = np.zeros((num_objs, 1), dtype=np.int32)
|
|
|
- gt_score = np.ones((num_objs, 1), dtype=np.float32)
|
|
|
- is_crowd = np.zeros((num_objs, 1), dtype=np.int32)
|
|
|
- difficult = np.zeros((num_objs, 1), dtype=np.int32)
|
|
|
- i = -1
|
|
|
- for i, back_anno in enumerate(background['annos']):
|
|
|
- gt_bbox[i] = back_anno['bbox']
|
|
|
- gt_class[i][0] = self.cname2cid[back_anno['category']]
|
|
|
-
|
|
|
- max_img_id = max(self.coco_gt.getImgIds())
|
|
|
- max_img_id += 1
|
|
|
-
|
|
|
- back_im = background['image']
|
|
|
- if isinstance(back_im, np.ndarray):
|
|
|
- if len(back_im.shape) != 3:
|
|
|
- raise Exception(
|
|
|
- "background image should be 3-dimensions, but now is {}-dimensions".
|
|
|
- format(len(back_im.shape)))
|
|
|
- else:
|
|
|
- try:
|
|
|
- back_im = cv2.imread(back_im, cv2.IMREAD_UNCHANGED)
|
|
|
- except:
|
|
|
- raise TypeError('Can\'t read The image file {}!'.format(
|
|
|
- back_im))
|
|
|
- back_annos = background['annos']
|
|
|
- im_h, im_w, im_c = back_im.shape
|
|
|
- for temp in templates:
|
|
|
- temp_im = temp['image']
|
|
|
- if isinstance(temp_im, np.ndarray):
|
|
|
- if len(temp_im.shape) != 3:
|
|
|
- raise Exception(
|
|
|
- "template image should be 3-dimensions, but now is {}-dimensions".
|
|
|
- format(len(temp_im.shape)))
|
|
|
- else:
|
|
|
- try:
|
|
|
- temp_im = cv2.imread(temp_im, cv2.IMREAD_UNCHANGED)
|
|
|
- except:
|
|
|
- raise TypeError('Can\'t read The image file {}!'.format(
|
|
|
- temp_im))
|
|
|
- temp_annos = temp['annos']
|
|
|
- for temp_anno in temp_annos:
|
|
|
- temp_mask = np.zeros(temp_im.shape, temp_im.dtype)
|
|
|
- temp_poly = np.array(temp_anno['polygon'], np.int32)
|
|
|
- temp_category = temp_anno['category']
|
|
|
- cv2.fillPoly(temp_mask, [temp_poly], (255, 255, 255))
|
|
|
- x_list = [temp_poly[i][0] for i in range(len(temp_poly))]
|
|
|
- y_list = [temp_poly[i][1] for i in range(len(temp_poly))]
|
|
|
- temp_poly_w = max(x_list) - min(x_list)
|
|
|
- temp_poly_h = max(y_list) - min(y_list)
|
|
|
- found = False
|
|
|
- while not found:
|
|
|
- center_x = random.randint(1, im_w - 1)
|
|
|
- center_y = random.randint(1, im_h - 1)
|
|
|
- if center_x < temp_poly_w / 2 or center_x > im_w - temp_poly_w / 2 - 1 or \
|
|
|
- center_y < temp_poly_h / 2 or center_y > im_h - temp_poly_h / 2 - 1:
|
|
|
- found = False
|
|
|
- continue
|
|
|
- if len(back_annos) == 0:
|
|
|
- found = True
|
|
|
- for back_anno in back_annos:
|
|
|
- x1, y1, x2, y2 = back_anno['bbox']
|
|
|
- category = back_anno['category']
|
|
|
- if center_x > x1 and center_x < x2 and center_y > y1 and center_y < y2:
|
|
|
- found = False
|
|
|
- continue
|
|
|
- found = True
|
|
|
- center = (center_x, center_y)
|
|
|
- back_im = cv2.seamlessClone(temp_im, back_im, temp_mask,
|
|
|
- center, cv2.MIXED_CLONE)
|
|
|
- i += 1
|
|
|
- x1 = center[0] - temp_poly_w / 2
|
|
|
- x2 = center[0] + temp_poly_w / 2
|
|
|
- y1 = center[1] - temp_poly_h / 2
|
|
|
- y2 = center[1] + temp_poly_h / 2
|
|
|
- gt_bbox[i] = [x1, y1, x2, y2]
|
|
|
- gt_class[i][0] = self.cname2cid[temp_category]
|
|
|
- self.ann_ct += 1
|
|
|
- self.coco_gt.dataset['annotations'].append({
|
|
|
- 'iscrowd': 0,
|
|
|
- 'image_id': max_img_id,
|
|
|
- 'bbox': [x1, y1, x2 - x1 + 1, y2 - y1 + 1],
|
|
|
- 'area': float((x2 - x1 + 1) * (y2 - y1 + 1)),
|
|
|
- 'category_id': self.cname2cid[temp_category],
|
|
|
- 'id': self.ann_ct,
|
|
|
- 'difficult': 0,
|
|
|
- })
|
|
|
- im_info = {
|
|
|
- 'im_id': np.array([max_img_id]).astype('int32'),
|
|
|
- 'image_shape': np.array([im_h, im_w]).astype('int32'),
|
|
|
- }
|
|
|
- label_info = {
|
|
|
- 'is_crowd': is_crowd,
|
|
|
- 'gt_class': gt_class,
|
|
|
- 'gt_bbox': gt_bbox,
|
|
|
- 'gt_score': gt_score,
|
|
|
- 'difficult': difficult,
|
|
|
- 'gt_poly': [],
|
|
|
- }
|
|
|
- self.coco_gt.dataset['images'].append({
|
|
|
- 'height': im_h,
|
|
|
- 'width': im_w,
|
|
|
- 'id': max_img_id,
|
|
|
- 'file_name': "clone_{:06d}.jpg".format(max_img_id)
|
|
|
- })
|
|
|
- coco_rec = (im_info, label_info)
|
|
|
- im_fname = osp.join(image_dir, "clone_{:06d}.jpg".format(max_img_id))
|
|
|
- cv2.imwrite(im_fname, back_im.astype('uint8'))
|
|
|
- self._write_xml(im_fname, im_h, im_w, im_c, label_info, anno_dir)
|
|
|
-
|
|
|
- self.file_list.append([im_fname, coco_rec])
|
|
|
- self.num_samples = len(self.file_list)
|
|
|
- self._write_json(self.coco_gt.dataset, save_dir)
|
|
|
-
|
|
|
def iterator(self):
|
|
|
self._epoch += 1
|
|
|
self._pos = 0
|
|
|
@@ -454,64 +296,3 @@ class VOCDetection(Dataset):
|
|
|
self._pos += 1
|
|
|
sample = [f[0], im_info, label_info]
|
|
|
yield sample
|
|
|
-
|
|
|
- def _write_xml(self, im_fname, im_h, im_w, im_c, label_info, anno_dir):
|
|
|
- is_crowd = label_info['is_crowd']
|
|
|
- gt_class = label_info['gt_class']
|
|
|
- gt_bbox = label_info['gt_bbox']
|
|
|
- gt_score = label_info['gt_score']
|
|
|
- gt_poly = label_info['gt_poly']
|
|
|
- difficult = label_info['difficult']
|
|
|
- import xml.dom.minidom as minidom
|
|
|
- xml_doc = minidom.Document()
|
|
|
- root = xml_doc.createElement("annotation")
|
|
|
- xml_doc.appendChild(root)
|
|
|
- node_filename = xml_doc.createElement("filename")
|
|
|
- node_filename.appendChild(xml_doc.createTextNode(im_fname))
|
|
|
- root.appendChild(node_filename)
|
|
|
- node_size = xml_doc.createElement("size")
|
|
|
- node_width = xml_doc.createElement("width")
|
|
|
- node_width.appendChild(xml_doc.createTextNode(str(im_w)))
|
|
|
- node_size.appendChild(node_width)
|
|
|
- node_height = xml_doc.createElement("height")
|
|
|
- node_height.appendChild(xml_doc.createTextNode(str(im_h)))
|
|
|
- node_size.appendChild(node_height)
|
|
|
- node_depth = xml_doc.createElement("depth")
|
|
|
- node_depth.appendChild(xml_doc.createTextNode(str(im_c)))
|
|
|
- node_size.appendChild(node_depth)
|
|
|
- root.appendChild(node_size)
|
|
|
- for i in range(label_info['gt_class'].shape[0]):
|
|
|
- node_obj = xml_doc.createElement("object")
|
|
|
- node_name = xml_doc.createElement("name")
|
|
|
- label = self.cid2cname[gt_class[i][0]]
|
|
|
- node_name.appendChild(xml_doc.createTextNode(label))
|
|
|
- node_obj.appendChild(node_name)
|
|
|
- node_diff = xml_doc.createElement("difficult")
|
|
|
- node_diff.appendChild(xml_doc.createTextNode(str(difficult[i][0])))
|
|
|
- node_obj.appendChild(node_diff)
|
|
|
- node_box = xml_doc.createElement("bndbox")
|
|
|
- node_xmin = xml_doc.createElement("xmin")
|
|
|
- node_xmin.appendChild(xml_doc.createTextNode(str(gt_bbox[i][0])))
|
|
|
- node_box.appendChild(node_xmin)
|
|
|
- node_ymin = xml_doc.createElement("ymin")
|
|
|
- node_ymin.appendChild(xml_doc.createTextNode(str(gt_bbox[i][1])))
|
|
|
- node_box.appendChild(node_ymin)
|
|
|
- node_xmax = xml_doc.createElement("xmax")
|
|
|
- node_xmax.appendChild(xml_doc.createTextNode(str(gt_bbox[i][2])))
|
|
|
- node_box.appendChild(node_xmax)
|
|
|
- node_ymax = xml_doc.createElement("ymax")
|
|
|
- node_ymax.appendChild(xml_doc.createTextNode(str(gt_bbox[i][3])))
|
|
|
- node_box.appendChild(node_ymax)
|
|
|
- node_obj.appendChild(node_box)
|
|
|
- root.appendChild(node_obj)
|
|
|
- img_name_part = osp.split(im_fname)[-1].split('.')[0]
|
|
|
- with open(osp.join(anno_dir, img_name_part + ".xml"), 'w') as fxml:
|
|
|
- xml_doc.writexml(
|
|
|
- fxml, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
|
|
|
-
|
|
|
- def _write_json(self, coco_gt, save_dir):
|
|
|
- from paddlex.tools.base import MyEncoder
|
|
|
- json_path = osp.join(save_dir, "annotations.json")
|
|
|
- f = open(json_path, "w")
|
|
|
- json.dump(coco_gt, f, indent=4, cls=MyEncoder)
|
|
|
- f.close()
|