def j2xConvert(jsonFilePath: str): with open(jsonFilePath, 'r', encoding='utf-8') as f: jsonObj = json.load(f) if isinstance(jsonObj, str): jsonObj = json.loads(jsonObj) tmpPath = jsonFilePath.replace('.json', '.xml') aimPath = tmpPath folder = os.path.abspath(jsonFilePath) filename = jsonObj['imagePath'] path = tmpPath width = jsonObj['imageWidth'] height = jsonObj['imageHeight'] objs = [] shapes = jsonObj['shapes'] # print(type(shapes)) for shape in shapes: label = shape['label'] points = shape['points'] # print(type(points)) tmp = np.array(points) # print(tmp) obj = dict() obj['name'] = label obj['difficult'] = 0 bndbox = dict() bndbox['xmin'] = np.min(tmp[:, 0]) if np.min( tmp[:, 0] ) > 0 else 1 # https://github.com/AlexeyAB/darknet the bounding box cannot be 0 bndbox['xmax'] = np.max(tmp[:, 0]) bndbox['ymin'] = np.min(tmp[:, 1]) if np.min(tmp[:, 1]) > 0 else 1 bndbox['ymax'] = np.max(tmp[:, 1]) obj['bndbox'] = bndbox if bndbox['ymax'] - bndbox['ymin'] < 10 or bndbox['xmax'] - bndbox[ 'xmin'] < 10: pass else: objs.append(obj) img2xml_multiobj(tmpPath, aimPath, folder, filename, path, width, height, objs) # print(objs) logger.info('Done! See {}'.format(tmpPath))
def getMultiObjs_voc(oriImgPath, labelPath, savePath): # pass labelImg = io.imread(labelPath) # print(labelImg.shape) (_, fileName) = os.path.split(labelPath) # print(fileName) # fileName = labelPath.split(os.sep)[-1] imgShape = labelImg.shape imgHeight = imgShape[0] imgWidth = imgShape[1] imgPath = oriImgPath logger.warning("auto detecting class numbers") if len(imgShape) == 3: labelImg = labelImg[:, :, 0] labelImg[labelImg > 0] = 255 _, labels, stats, centroids = cv2.connectedComponentsWithStats( labelImg.astype(np.uint8)) statsShape = stats.shape objs = [] for i in range(1, statsShape[0]): st = stats[i, :] width = st[2] height = st[3] xmin = st[0] ymin = st[1] xmax = xmin + width ymax = ymin + height ob = {} ob['name'] = 'class{}'.format(i) ob['difficult'] = 0 # ob['name'] = 'weld' bndbox = {} bndbox['xmin'] = xmin bndbox['ymin'] = ymin bndbox['xmax'] = xmax bndbox['ymax'] = ymax ob['bndbox'] = bndbox objs.append(ob) saveXmlPath = savePath + os.sep + fileName[:-4] + '.xml' img2xml_multiobj(saveXmlPath, saveXmlPath, "TEST", fileName, imgPath, imgWidth, imgHeight, objs)
def writeXml(imgname, objlist, savepath, folder, oriImgPath=''): # imgname = lines[start].split('/')[-1] xmlname = imgname.replace('.jpg', '.xml').replace('\n', '') tmpPath = savepath + xmlname path = imgname if oriImgPath == '': # logger.warning('Origin image needed!') width = 0 height = 0 else: oriImg = io.imread(oriImgPath + os.sep + imgname) width = oriImg.shape[1] height = oriImg.shape[0] del oriImg objs = [] # objlist = lines[start + 2:end] for ob in objlist: # print(ob) if len(ob) > 20: tmp = ob.split(' ') xmin = tmp[0] ymin = tmp[1] w = tmp[2] h = tmp[3] xmax = int(xmin) + int(w) ymax = int(ymin) + int(h) occlusion = int(tmp[-2]) blur = int(tmp[4]) if blur > 0: name = 'blur' elif occlusion > 0: name = 'occlusion' else: name = 'face' obj = dict() obj['name'] = name obj['diffcult'] = 0 bndbox = dict() bndbox['xmin'] = xmin bndbox['ymin'] = ymin bndbox['xmax'] = xmax bndbox['ymax'] = ymax obj['bndbox'] = bndbox objs.append(obj) img2xml_multiobj(tmpPath, tmpPath, folder, imgname, path, width, height, objs)
def getMultiObjs_voc_withYaml(oriImgPath, labelPath, yamlPath=''): if os.path.exists(yamlPath): f = open(yamlPath, encoding='utf-8') y = yaml.load(f, Loader=yaml.FullLoader) f.close() label_masks = y['label_names'] else: raise FileNotFoundError('yaml file not found!') # print(label_masks) savePath = os.path.abspath(os.path.dirname(oriImgPath)) + os.sep + 'xml' if not os.path.exists(savePath): os.mkdir(savePath) fileName = oriImgPath.split(os.sep)[-1] saveXmlPath = savePath + os.sep + fileName[:-4] + '.xml' labelImg = io.imread(labelPath) if isinstance(labelPath, str) else labelPath fileName = oriImgPath.split(os.sep)[-1] imgShape = labelImg.shape imgHeight = imgShape[0] imgWidth = imgShape[1] imgPath = oriImgPath objs = [] for k, v in label_masks.items(): # print(k) # print(v) ma = copy.deepcopy(labelImg) ma[ma != int(v)] = 0 if np.sum(ma) > 0: # print(v) ma1 = copy.deepcopy(labelImg) ma1[ma1 != int(v)] = 0 ma1[ma1 != 0] = 255 _, labels, stats, centroids = cv2.connectedComponentsWithStats(ma1) del ma1 statsShape = stats.shape for i in range(1, statsShape[0]): st = stats[i, :] width = st[2] height = st[3] xmin = st[0] ymin = st[1] # print('area = {}'.format(st[4])) # print('width = {},height = {}'.format(width,height)) xmax = xmin + width ymax = ymin + height ob = {} ob['name'] = k ob['difficult'] = 0 bndbox = {} bndbox['xmin'] = xmin bndbox['ymin'] = ymin bndbox['xmax'] = xmax bndbox['ymax'] = ymax ob['bndbox'] = bndbox if width > 10 and height > 10 and st[4] >= 0.75 * (width * height): objs.append(ob) # print(ob) del ma # print(objs) # print("............................") img2xml_multiobj(saveXmlPath, saveXmlPath, "TEST", fileName, imgPath, imgWidth, imgHeight, objs) objs.clear()
def mosiacScript_no_reshape(imgs: list, xmls: list, savePath: str, flag=False): heightFactor = random.uniform(0.1, 0.5) widthFactor = random.uniform(0.1, 0.5) img1, img2, img3, img4 = imgs[0], imgs[1], imgs[2], imgs[3] if not type(imgs) is list or not type(xmls) is list: logger.error('Input must be list!') return imgname = getName(xmls) # imgname = 'test1123' folder = savePath mosiacImg, res, _, _ = mosiac_img_no_reshape(imgs, heightFactor, widthFactor) front = res[0] # print(front) # print(heightFactor_,widthFactor_) heightFactor = min(heightFactor, 1 - heightFactor) widthFactor = min(widthFactor, 1 - widthFactor) # print(heightFactor, widthFactor) tree1 = ET.parse(xmls[0]) tree2 = resizeScript(img2, xmls[1], heightFactor=img1.shape[0] / img2.shape[0], widthFactor=img1.shape[1] / img2.shape[1], flag=False) tree3 = resizeScript(img3, xmls[2], heightFactor=img1.shape[0] / img3.shape[0], widthFactor=img1.shape[1] / img3.shape[1], flag=False) tree4 = resizeScript(img4, xmls[3], heightFactor=img1.shape[0] / img4.shape[0], widthFactor=img1.shape[1] / img4.shape[1], flag=False) root1 = tree1.getroot() root2 = tree2.getroot() for box in root2.iter('bndbox'): xmin = float(box.find('xmin').text) ymin = float(box.find('ymin').text) xmax = float(box.find('xmax').text) ymax = float(box.find('ymax').text) box.find('xmin').text = str( int(xmin + widthFactor * mosiacImg.shape[1])) box.find('xmax').text = str( int(xmax + widthFactor * mosiacImg.shape[1])) root3 = tree3.getroot() for box in root3.iter('bndbox'): xmin = float(box.find('xmin').text) ymin = float(box.find('ymin').text) xmax = float(box.find('xmax').text) ymax = float(box.find('ymax').text) box.find('ymin').text = str( int(ymin + heightFactor * mosiacImg.shape[0])) box.find('ymax').text = str( int(ymax + heightFactor * mosiacImg.shape[0])) root4 = tree4.getroot() for box in root4.iter('bndbox'): xmin = float(box.find('xmin').text) ymin = float(box.find('ymin').text) xmax = float(box.find('xmax').text) ymax = float(box.find('ymax').text) box.find('xmin').text = str( int(xmin + widthFactor * mosiacImg.shape[1])) box.find('xmax').text = str( int(xmax + widthFactor * mosiacImg.shape[1])) box.find('ymin').text = str( int(ymin + heightFactor * mosiacImg.shape[0])) box.find('ymax').text = str( int(ymax + heightFactor * mosiacImg.shape[0])) boxes = [] r1, r2, r3, r4 = getBoxes(front, root1, root2, root3, root4, mosiacImg.shape, heightFactor=heightFactor, widthFactor=widthFactor) for box in r1.iter('object'): # print(box) boxes.append(box) for box in r2.iter('object'): # print(box) boxes.append(box) for box in r3.iter('object'): # print(box) boxes.append(box) for box in r4.iter('object'): # print(box) boxes.append(box) # print(len(boxes)) imgshape = mosiacImg.shape objs = [] for o in boxes: obj = dict() name = o.find('name').text difficult = 0 xmlbox = o.find('bndbox') b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text)) tmp = dict() tmp['xmin'] = str(int(b[0])) tmp['ymin'] = str(int(b[2])) tmp['xmax'] = str(int(b[1])) tmp['ymax'] = str(int(b[3])) # print(tmp) if not (int(tmp['xmin']) == 0 and int(tmp['xmax']) == 0 and int(tmp['ymin']) == 0 and int(tmp['ymax']) == 0): obj['name'] = name obj['difficult'] = difficult obj['bndbox'] = tmp objs.append(obj) del tmp # print(len(objs)) tmpPath = savePath + os.sep + imgname + '.xml' filepath = tmpPath.replace('.xml', '.jpg') filename = imgname + '.jpg' img2xml_multiobj(tmpPath, tmpPath, folder, filename, filepath, imgshape[1], imgshape[0], objs) logger.info('Saved to {}.'.format(tmpPath)) if flag: skimage.io.imsave(filepath, mosiacImg)
def mosiacScript(imgs: list, xmls: list, savePath: str, flag=False): heightFactor = random.uniform(0.3, 0.7) widthFactor = random.uniform(0.3, 0.7) if not type(imgs) is list or not type(xmls) is list: logger.error('Input must be list!') return # imgs if len(imgs) == 0: logger.error('None image found!') return if len(imgs) == 1: for _ in range(0, 3): imgs.append(imgs[0]) if len(imgs) == 2: for _ in range(0, 2): imgs.append(imgs[0]) if len(imgs) == 3: for _ in range(0, 1): imgs.append(imgs[0]) # xmls if len(xmls) == 0: logger.error('None xml found!') return if len(xmls) == 1: for _ in range(0, 3): xmls.append(xmls[0]) if len(xmls) == 2: for _ in range(0, 2): xmls.append(xmls[0]) if len(xmls) == 3: for _ in range(0, 1): xmls.append(xmls[0]) imgname = getName(xmls) folder = savePath mHeight, mWidth = getMeanSize(imgs) mosiacImg = mosiac_img(imgs, heightFactor, widthFactor) objs = [] imgshape = mosiacImg.shape for idx in range(len(xmls)): in_file = open(xmls[idx]) tree = ET.parse(in_file) root = tree.getroot() for o in root.iter('object'): obj = dict() name = o.find('name').text difficult = 0 xmlbox = o.find('bndbox') b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text)) # print('===========================') # print(b) bb = x2yVert((mWidth, mHeight), b) x, y, w, h = bb[0], bb[1], bb[2], bb[3] if idx == 0: bbox = y2xVert( (imgshape[1] * widthFactor, imgshape[0] * heightFactor), x, y, w, h) elif idx == 1: bbox = y2xVert((imgshape[1] * (1 - widthFactor), imgshape[0] * heightFactor), x, y, w, h) bbox[0] = bbox[0] + int(widthFactor * imgshape[1]) bbox[1] = bbox[1] + int(widthFactor * imgshape[1]) elif idx == 2: bbox = y2xVert((imgshape[1] * widthFactor, imgshape[0] * (1 - heightFactor)), x, y, w, h) bbox[2] = bbox[2] + int(heightFactor * imgshape[0]) bbox[3] = bbox[3] + int(heightFactor * imgshape[0]) else: bbox = y2xVert((imgshape[1] * (1 - widthFactor), imgshape[0] * (1 - heightFactor)), x, y, w, h) bbox[0] = bbox[0] + int(widthFactor * imgshape[1]) bbox[2] = bbox[2] + int(heightFactor * imgshape[0]) bbox[1] = bbox[1] + int(widthFactor * imgshape[1]) bbox[3] = bbox[3] + int(heightFactor * imgshape[0]) # print(x, y, w, h) # w = w # h = h # bbox = y2xVert((imgshape[1],imgshape[0]), x, y, w, h) tmp = dict() tmp['xmin'] = str(int(bbox[0])) tmp['ymin'] = str(int(bbox[2])) tmp['xmax'] = str(int(bbox[1])) tmp['ymax'] = str(int(bbox[3])) obj['name'] = name obj['difficult'] = difficult obj['bndbox'] = tmp del tmp objs.append(obj) tmpPath = savePath + os.sep + imgname + '.xml' filepath = tmpPath.replace('.xml', '.jpg') filename = imgname + '.jpg' img2xml_multiobj(tmpPath, tmpPath, folder, filename, filepath, imgshape[1], imgshape[0], objs) logger.info('Saved to {}.'.format(tmpPath)) if flag: skimage.io.imsave(filepath, mosiacImg)
def y2xConvert(txtPath, imgPath, labelPath): logger.info('only *.jpg supported right now!') labels = readLabels(labelPath) if not os.path.exists(txtPath): raise FileNotFoundError('file not found') else: if os.path.isfile(txtPath): # pass parent_path = os.path.dirname(txtPath) filename = os.path.split(imgPath)[1] imgname = os.path.splitext(filename)[0] logger.info('single file found') image = io.imread(imgPath) folder = os.path.dirname(imgPath) imgShape = image.shape objs = [] with open(txtPath, 'r', encoding='utf-8') as f: contents = f.readlines() if len(contents) > 0: for c in contents: obj = dict() tmp = c.split(' ') clas, x, y, w, h = int( tmp[0]), tmp[1], tmp[2], tmp[3], tmp[4] bbox = convert(imgShape, float(x), float(y), float(w), float(h)) # print(bbox) obj['name'] = labels[clas] obj['difficult'] = 0 obj['bndbox'] = { 'xmin': bbox[0], 'ymin': bbox[2], 'xmax': bbox[1], 'ymax': bbox[3] } objs.append(obj) tmpPath = parent_path + os.sep + '_xmls_' + os.sep + imgname + '.xml' if not os.path.exists(parent_path + os.sep + '_xmls_'): os.mkdir(parent_path + os.sep + '_xmls_') img2xml_multiobj(tmpPath, tmpPath, folder, filename, imgPath, imgShape[1], imgShape[0], objs) logger.info('Done! See {} .'.format(tmpPath)) else: logger.info('Multiple files found') parent_path = os.path.dirname(txtPath) if not os.path.exists(parent_path + os.sep + '_xmls_'): os.mkdir(parent_path + os.sep + '_xmls_') txts = glob.glob(txtPath + os.sep + "*.txt") for i in tqdm(txts): filename = os.path.split(i)[1] imgname = os.path.splitext(filename)[0] i_imgPath = imgPath + os.sep + imgname + '*.jpg' if not os.path.exists(i_imgPath): logger.error('image not found!') return image = io.imread(i_imgPath) folder = imgPath imgShape = image.shape objs = [] with open(i, 'r', encoding='utf-8') as f: contents = f.readlines() if len(contents) > 0: for c in contents: obj = dict() tmp = c.split(' ') clas, x, y, w, h = int( tmp[0]), tmp[1], tmp[2], tmp[3], tmp[4] bbox = convert(imgShape, float(x), float(y), float(w), float(h)) # print(bbox) obj['name'] = labels[clas] obj['difficult'] = 0 obj['bndbox'] = { 'xmin': bbox[0], 'ymin': bbox[2], 'xmax': bbox[1], 'ymax': bbox[3] } objs.append(obj) tmpPath = parent_path + os.sep + '_xmls_' + os.sep + imgname + '.xml' img2xml_multiobj(tmpPath, tmpPath, folder, filename, imgPath, imgShape[1], imgShape[0], objs) logger.info('Done! See {} .'.format(parent_path + os.sep + '_xmls_'))
def convertWiderface(filepath: str, savepath='', mProcess=False): """Annotation of widerface is like 0--Parade/0_Parade_marchingband_1_849.jpg 1 449 330 122 149 0 0 0 0 0 0 The format of txt ground truth. \n File name \n Number of bounding box \n x1, y1, w, h, blur, expression, illumination, invalid, occlusion, pose which is not suitable for labelImg. Besides, convertion may take a really long time using single process. """ with open(filepath, 'r', encoding='utf-8') as f: lines = f.readlines() ids = list(index for (index, d) in enumerate(lines) if d.endswith('.jpg\n')) # print(len(ids)) if savepath == '': savepath = BASE_DIR + os.sep + 'xmls_' if not os.path.isdir(savepath): os.mkdir(savepath) folder = 'face' name = 'face' lastImg = lines[ids[-1]] imgname = lastImg.split('/')[-1].replace('\n', '') xmlname = imgname.replace('.jpg', '.xml') tmpPath = savepath + xmlname path = imgname width = 0 height = 0 objs = [] objlist = lines[ids[-1] + 2:] for ob in objlist: if len(ob) > 20: tmp = ob.split(' ') xmin = tmp[0] ymin = tmp[1] w = tmp[2] h = tmp[3] xmax = int(xmin) + int(w) ymax = int(ymin) + int(h) obj = dict() obj['name'] = name obj['diffcult'] = 0 bndbox = dict() bndbox['xmin'] = xmin bndbox['ymin'] = ymin bndbox['xmax'] = xmax bndbox['ymax'] = ymax obj['bndbox'] = bndbox objs.append(obj) img2xml_multiobj(tmpPath, tmpPath, folder, imgname, path, width, height, objs) if not mProcess: for i in tqdm(range(len(ids) - 1)): start = ids[i] end = ids[i + 1] imgname = lines[start].split('/')[-1] xmlname = imgname.replace('.jpg', '.xml').replace('\n', '') tmpPath = savepath + xmlname path = imgname width = 0 # for test height = 0 # for test objs = [] objlist = lines[start + 2:end] for ob in objlist: # print(ob) if len(ob) > 20: tmp = ob.split(' ') xmin = tmp[0] ymin = tmp[1] w = tmp[2] h = tmp[3] xmax = int(xmin) + int(w) ymax = int(ymin) + int(h) obj = dict() obj['name'] = name obj['diffcult'] = 0 bndbox = dict() bndbox['xmin'] = xmin bndbox['ymin'] = ymin bndbox['xmax'] = xmax bndbox['ymax'] = ymax obj['bndbox'] = bndbox objs.append(obj) img2xml_multiobj(tmpPath, tmpPath, folder, imgname, path, width, height, objs) logger.info('Done! See {}.'.format(savepath)) else: pool = Pool(__CPUS__ - 1) pool_list = [] for i in tqdm(range(len(ids) - 1)): start = ids[i] end = ids[i + 1] imgname = lines[start].split('/')[-1] objlist = lines[start + 2:end] resultsPool = pool.apply_async( writeXml, (imgname, objlist, savepath, folder, name)) pool_list.append(resultsPool) for pr in tqdm(pool_list): re_list = pr.get() logger.info('Done! See {}.'.format(savepath))
def readTxt(filepath: str, savepath='', mProcess=False): with open(filepath, 'r', encoding='utf-8') as f: lines = f.readlines() # test # for (index, d) in enumerate(lines): # print(str(index) + d) ids = list(index for (index, d) in enumerate(lines) if d.endswith('.jpg\n')) # print(len(ids)) if savepath == '': savepath = BASE_DIR + os.sep + 'xmls_' if not os.path.isdir(savepath): os.mkdir(savepath) folder = 'face' name = 'face' lastImg = lines[ids[-1]] imgname = lastImg.split('/')[-1].replace('\n', '') xmlname = imgname.replace('.jpg', '.xml') tmpPath = savepath + xmlname path = imgname width = 0 height = 0 objs = [] objlist = lines[ids[-1] + 2:] for ob in objlist: if len(ob) > 20: tmp = ob.split(' ') xmin = tmp[0] ymin = tmp[1] w = tmp[2] h = tmp[3] xmax = int(xmin) + int(w) ymax = int(ymin) + int(h) obj = dict() obj['name'] = name obj['diffcult'] = 0 bndbox = dict() bndbox['xmin'] = xmin bndbox['ymin'] = ymin bndbox['xmax'] = xmax bndbox['ymax'] = ymax obj['bndbox'] = bndbox objs.append(obj) img2xml_multiobj(tmpPath, tmpPath, folder, imgname, path, width, height, objs) if not mProcess: for i in tqdm(range(len(ids) - 1)): start = ids[i] end = ids[i + 1] imgname = lines[start].split('/')[-1] xmlname = imgname.replace('.jpg', '.xml').replace('\n', '') tmpPath = savepath + xmlname path = imgname width = 0 # for test height = 0 # for test objs = [] objlist = lines[start + 2:end] for ob in objlist: # print(ob) if len(ob) > 20: tmp = ob.split(' ') xmin = tmp[0] ymin = tmp[1] w = tmp[2] h = tmp[3] xmax = int(xmin) + int(w) ymax = int(ymin) + int(h) obj = dict() obj['name'] = name obj['diffcult'] = 0 bndbox = dict() bndbox['xmin'] = xmin bndbox['ymin'] = ymin bndbox['xmax'] = xmax bndbox['ymax'] = ymax obj['bndbox'] = bndbox objs.append(obj) img2xml_multiobj(tmpPath, tmpPath, folder, imgname, path, width, height, objs) else: pool = Pool(__CPUS__ - 1) pool_list = [] for i in tqdm(range(len(ids) - 1)): start = ids[i] end = ids[i + 1] imgname = lines[start].split('/')[-1] objlist = lines[start + 2:end] resultsPool = pool.apply_async( writeXml, (imgname, objlist, savepath, folder, name)) pool_list.append(resultsPool) for pr in tqdm(pool_list): re_list = pr.get()