Skip to content

Commit

Permalink
update data aug
Browse files Browse the repository at this point in the history
  • Loading branch information
ethon committed Sep 24, 2019
2 parents 9f31166 + 0758454 commit 8cfb061
Show file tree
Hide file tree
Showing 5 changed files with 406 additions and 1 deletion.
126 changes: 126 additions & 0 deletions JsonToCoco.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import os
import json
import numpy as np
import shutil
import pandas as pd

# defect_name2label = {
# '破洞': 1, '水渍': 2, '油渍': 2, '污渍': 2, '三丝': 3, '结头': 4, '花板跳': 5, '百脚': 6, '毛粒': 7,
# '粗经': 8, '松经': 9, '断经': 10, '吊经': 11, '粗维': 12, '纬缩': 13, '浆斑': 14, '整经结': 15, '星跳': 16, '跳花': 16,
# '断氨纶': 17, '稀密档': 18, '浪纹档': 18, '色差档': 18, '磨痕': 19, '轧痕': 19, '修痕': 19, '烧毛痕': 19, '死皱': 20, '云织': 20,
# '双纬': 20, '双经': 20, '跳纱': 20, '筘路': 20, '纬纱不良': 20,
# }
defect_name2label = {
1: 1, 2: 2, 3: 3,4: 4, 5: 5, 6: 6, 7: 7,
8: 8, 9: 9, 10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15,
16:16,17:17,18:18,19:19,20:20
}

class Fabric2COCO:

def __init__(self,mode="train"):
self.images = []
self.annotations = []
self.categories = []
self.img_id = 5912
self.ann_id = 5912
self.mode =mode
if not os.path.exists("coco/images/{}".format(self.mode)):
os.makedirs("coco/images/{}".format(self.mode))

def to_coco(self, anno_file,img_dir):
self._init_categories()
anno_result= pd.read_json(open(anno_file,"r"))
name_list=anno_result["name"].unique()
for img_name in name_list:
img_anno = anno_result[anno_result["name"] == img_name]
bboxs = img_anno["bbox"].tolist()
defect_names = img_anno["category"].tolist()
assert img_anno["name"].unique()[0] == img_name

img_path=os.path.join(img_dir,img_name)
# img =cv2.imread(img_path)
# h,w,c=img.shape
h,w=1000,2446
self.images.append(self._image(img_path,h, w))

self._cp_img(img_path)
# print(defect_names[0].type)
for bbox, defect_name in zip(bboxs, defect_names):
# print(defect_name)
label= defect_name2label[defect_name]
annotation = self._annotation(label, bbox)
self.annotations.append(annotation)
self.ann_id += 1
self.img_id += 1
instance = {}
instance['info'] = 'fabric defect'
instance['license'] = ['none']
instance['images'] = self.images
instance['annotations'] = self.annotations
instance['categories'] = self.categories
return instance

def _init_categories(self):
for v in range(1,21):
print(v)
category = {}
category['id'] = v
category['name'] = str(v)
category['supercategory'] = 'defect_name'
self.categories.append(category)
# for k, v in defect_name2label.items():
# category = {}
# category['id'] = v
# category['name'] = k
# category['supercategory'] = 'defect_name'
# self.categories.append(category)

def _image(self, path,h,w):
image = {}
image['height'] = h
image['width'] = w
image['id'] = self.img_id
image['file_name'] = os.path.basename(path)
return image

def _annotation(self,label,bbox):
area=(bbox[2]-bbox[0])*(bbox[3]-bbox[1])
points=[[bbox[0],bbox[1]],[bbox[2],bbox[1]],[bbox[2],bbox[3]],[bbox[0],bbox[3]]]
annotation = {}
annotation['id'] = self.ann_id
annotation['image_id'] = self.img_id
annotation['category_id'] = label
annotation['segmentation'] = [np.asarray(points).flatten().tolist()]
annotation['bbox'] = self._get_box(points)
annotation['iscrowd'] = 0
annotation['area'] = area
return annotation

def _cp_img(self, img_path):
shutil.copy(img_path, os.path.join("coco/images/{}".format(self.mode), os.path.basename(img_path)))

def _get_box(self, points):
min_x = min_y = np.inf
max_x = max_y = 0
for x, y in points:
min_x = min(min_x, x)
min_y = min(min_y, y)
max_x = max(max_x, x)
max_y = max(max_y, y)
'''coco,[x,y,w,h]'''
return [min_x, min_y, max_x - min_x, max_y - min_y]
def save_coco_json(self, instance, save_path):
import json
with open(save_path, 'w') as fp:
json.dump(instance, fp, indent=1, separators=(',', ': '))


'''转换有瑕疵的样本为coco格式'''
img_dir = "./normal_images_aug"
anno_dir="./annotation_normal.json"
fabric2coco = Fabric2COCO()
train_instance = fabric2coco.to_coco(anno_dir,img_dir)
if not os.path.exists("coco/annotations/"):
os.makedirs("coco/annotations/")
fabric2coco.save_coco_json(train_instance, "coco/annotations/"+'instances_{}.json'.format("train"))
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<<<<<<< HEAD

# MMDetection

Expand Down Expand Up @@ -147,4 +148,8 @@ If you use this toolbox or benchmark in your research, please cite this project.

## Contact

This repo is currently maintained by Kai Chen ([@hellock](http://github.com/hellock)), Jiangmiao Pang ([@OceanPang](https://github.com/OceanPang)), Jiaqi Wang ([@myownskyW7](https://github.com/myownskyW7)) and Yuhang Cao ([@yhcao6](https://github.com/yhcao6)).
This repo is currently maintained by Kai Chen ([@hellock](http://github.com/hellock)), Jiangmiao Pang ([@OceanPang](https://github.com/OceanPang)), Jiaqi Wang ([@myownskyW7](https://github.com/myownskyW7)) and Yuhang Cao ([@yhcao6](https://github.com/yhcao6)).
=======
# Tianchi_data_aug
normal-data-augmentation
>>>>>>> 07584541bbba36e48c7cc85c8e20a463b797d2f1
71 changes: 71 additions & 0 deletions crop_object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

import os
import cv2
import re
#原文:https://blog.csdn.net/angelbeats11/article/details/88427314
pattens = ['name','xmin','ymin','xmax','ymax']
import matplotlib.pyplot as plt
import numpy as np
count=0
def get_annotations(xml_path):
bbox = []
with open(xml_path,'r') as f:
text = f.read().replace('\n','return')
p1 = re.compile(r'(?<=<object>)(.*?)(?=</object>)')
result = p1.findall(text)
for obj in result:
tmp = []
for patten in pattens:
p = re.compile(r'(?<=<{}>)(.*?)(?=</{}>)'.format(patten,patten))
if patten == 'name':
tmp.append(p.findall(obj)[0])
else:
tmp.append(int(float(p.findall(obj)[0])))
bbox.append(tmp)
return bbox

def save_viz_image(image_path,xml_path,save_path):
global count
bbox = get_annotations(xml_path)
image = cv2.imread(image_path)
if not os.path.exists(save_path):
os.mkdir(save_path)

for info in bbox:
# print(info[0])

count+=1
# print(os.path.join(save_path,str(count),image_path.split('/')[-1]))
# print(image[info[2]:info[4],info[1]:info[3]].shape)
# plt.imshow(image[info[2]:info[4],info[1]:info[3]])
# plt.show()

# print(info[1],info[2],info[3],info[4])
# cv2.rectangle(image,(info[1],info[2]),(info[3],info[4]),(0,255,0),thickness=2)
# cv2.putText(image,info[0],(info[1],info[2]),cv2.FONT_HERSHEY_PLAIN,2,(0,0,255),2)
# gray=cv2.cvtColor(image[info[2]:info[4],info[1]:info[3]],cv2.COLOR_BGR2GRAY)
# # ret2, th2 = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# im = np.float32(gray) / 255.0

# # Calculate gradient
# gx = cv2.Sobel(im, cv2.CV_32F, 1, 0, ksize=1)
# gy = cv2.Sobel(im, cv2.CV_32F, 0, 1, ksize=1)
# mag, angle = cv2.cartToPolar(gx, gy, angleInDegrees=True)
#
# plt.imshow(mag)
# plt.show()
# print(save_path+str(count)+'.jpg')
cv2.imwrite(save_path+"/"+str(count)+'_'+str(info[0])+'.jpg',image[info[2]:info[4],info[1]:info[3]])
#cv2.imwrite(save_path + str(count) + '.jpg', mag)

if __name__ == '__main__':
image_dir = './VOC2020/JPEGImages'
xml_dir = './VOC2020/Annotations'
save_dir = './detect_images'
image_list = os.listdir(image_dir)
for i in image_list:
image_path = os.path.join(image_dir,i)
xml_path = os.path.join(xml_dir,i.replace('.jpg','.xml'))
save_viz_image(image_path,xml_path,save_dir)


95 changes: 95 additions & 0 deletions tianchi_aug_normal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from skimage import io,data,color
from matplotlib import pyplot as plt
import numpy as np
import cv2
import os
import random
import skimage
from skimage.exposure import histogram
from skimage import feature
import json
detect_img_root='./detect_images/'
normal_img_root='./normal_images/'
aug_dir='./normal_images_aug/'

detect_name=os.listdir(detect_img_root)
normal_name=os.listdir(normal_img_root)
# # for test
# img=io.imread(img_root+file_name[0])
# # img=np.array(img)
# print(img.shape)
# cv2.imshow('image',img)
# cv2.waitKey(0)
# random.shuffle(normal_name)
# print(img.shape)
# print(img.shape) #显示尺寸
# print(img.shape[0]) #图片高度
# print(img.shape[1]) #图片宽度
# print(img.shape[2]) #图片通道数
# print(img.size) #显示总像素个数
# print(img.max()) #最大像素值
# print(img.min()) #最小像素值
# print(img.mean())

random.seed(2019)#设定随机数种子
# 正常样本扩增
result=[]
for i in range(len(normal_name)):
# print(normal_name[i])
img=cv2.imread(normal_img_root+normal_name[i])
# print(img.shape)
height,width=img.shape[0],img.shape[1]
# print(height,width)
defect_img=cv2.imread(detect_img_root+detect_name[i])
# print(detect_name[i].split('.')[0].split('_')[1])
label=detect_name[i].split('.')[0].split('_')[1]
defect_h,defect_w=defect_img.shape[0],defect_img.shape[1]
# print(defect_h)
# print((height-defect_h))
# h_range=abs(height-defect_h)
# print((width-defect_w))
# w_range=abs(width-defect_w)
for j in range(5):
xmin=random.randint(1,2446)
ymin=random.randint(1,1000)
xmax=xmin+defect_w
ymax=ymin+defect_h
print(xmin,ymin,xmax,ymax)
print(img[xmin:xmax,ymin:ymax].shape,defect_img.shape)
img=np.array(img)
defect_img=np.array(defect_img)
try:
img[ymin:ymax,xmin:xmax]=defect_img
result.append({'name': normal_name[i], 'category': label, 'bbox': [xmin,ymin,xmax,ymax]})
# cv2.rectangle(img, (xmin,ymin), (xmax,ymax), (0,255,0), 2)
# cv2.imshow('image',img)
# cv2.waitKey(0)
except:
continue
cv2.imwrite(aug_dir+normal_name[i],img)
json_name='annotation_normal.json'
with open(json_name,'w') as fp:
json.dump(result, fp, indent = 4, separators=(',', ': '))
# if (defect_w*defect_h)<(32*32):






#带桌面数据处理,检测垂直边
# kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5)) # 十字形结构
# for i in range(len(normal_name)):
# img=cv2.imread(normal_img_root+normal_name[i])
# img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# img=skimage.filters.sobel_v(img_gray)
# img=cv2.dilate(img, kernel)
# print(img.shape)
# row_index=[]
# for i in range(img.shape[1]):
# count_list=list(img[:,i]*255>50)
# row_index.append(count_list.count(True))
# print(np.argmax(row_index))



Loading

0 comments on commit 8cfb061

Please sign in to comment.