def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [ image.height(), image.width(), 1 if image.isGrayscale() else 3 ] writer = PascalVocWriter(imgFolderName, imgFileNameWithoutExt, imageShape, localImgPath=imagePath) writer.verified = self.verified for shape in shapes: points = shape['points'] label = shape['label'] # Add Chris difficult = int(shape['difficult']) direction = shape['direction'] isRotated = shape['isRotated'] # if shape is normal box, save as bounding box # print('direction is %lf' % direction) if not isRotated: bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult) else: #if shape is rotated box, save as rotated bounding box robndbox = LabelFile.convertPoints2RotatedBndBox(shape) writer.addRotatedBndBox(robndbox[0], robndbox[1], robndbox[2], robndbox[3], robndbox[4], label, difficult) writer.save(targetFile=filename) return
def savePascalVocFormat(self, username, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [ image.height(), image.width(), 1 if image.isGrayscale() else 3 ] writer = PascalVocWriter(username, imgFolderName, imgFileNameWithoutExt, imageShape, localImgPath=imagePath) # print imgFileNameWithoutExt writer.verified = self.verified for shape in shapes: points = shape['points'] #>>>delete(1) label = shape['label'] bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3]) #>>>delete(1), label # print '???',filename if filename.split('.')[1] == 'jpg': filename = filename.split('.')[0] + '.xml' # print '!!!???',filename writer.save(targetFile=filename) return
def convert_coco2_voc(json_file, save_dir): coco = COCO(json_file) classes_dict = catid2name(coco) for cat in classes: catIds = coco.getCatIds(catNms=[cat]) if len(catIds) == 0: continue imgIds = coco.getImgIds(catIds=catIds) for imgId in tqdm(imgIds): img = coco.loadImgs(imgId)[0] filename = img['file_name'] height = img['height'] width = img['width'] annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None) anns = coco.loadAnns(annIds) if len(anns) == 0: continue writer = PascalVocWriter(filename, filename, (height, width, 3), localImgPath=filename) writer.verified = True objs = [] for ann in anns: name = classes_dict[ann['category_id']] if name in classes: if 'bbox' in ann: bbox = ann['bbox'] xmin = (int)(bbox[0]) ymin = (int)(bbox[1]) xmax = (int)(bbox[2] + bbox[0]) ymax = (int)(bbox[3] + bbox[1]) obj = [name, 1.0, xmin, ymin, xmax, ymax] objs.append(obj) writer.addBndBox(xmin, ymin, xmax, ymax, name, 0) writer.save(os.path.join(save_dir, filename.replace("jpg", "xml")))
def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [ image.height(), image.width(), 1 if image.isGrayscale() else 3 ] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, localImgPath=imagePath) writer.verified = self.verified for shape in shapes: points = shape['points'] label = shape['label'] # Add Chris difficult = int(shape['difficult']) checked = int(shape['checked']) bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult, checked) writer.save(targetFile=filename) return
filename = path + file print file dot = file.find('.') purename = file[:dot] reader = PascalVocReader(filename) reader.parseXML() boxes = [] for i, shape in enumerate(reader.shapes): if i < (len(reader.shapes) / 2): boxes.append(shape) boxes = sorted(boxes, key=lambda item: item[1][0][0]) readimgname = 'VOCdevkit/VOC2050/JPEGImages/{}.jpg'.format(purename) img = cv2.imread(readimgname) imgSize = [img.shape[0], img.shape[1], 1] foldername = 'JPEGImages' filename = purename + '.jpg' localImagePath = '/root/darknet/scripts/VOCdevkit/VOC2050back/JPEGImages/' + filename writer = PascalVocWriter(foldername, filename, imgSize, localImgPath=localImagePath) #print type(boxes) for b in boxes: writer.addBndBox(b[1][0][0], b[1][0][1], b[1][2][0], b[1][2][1], b[0], 0) #print b writer.verified = False xmlfname = '/root/darknet/scripts/VOCdevkit/VOC2050back/Annotations/{}'.format( file) writer.save(xmlfname)
if iou < 0.1: continue # print '没有重叠,过滤掉' elif iou > 0.5: # print '两个bounding box重叠度为:', iou # print '将舍弃该张图像' continue imgFileName = os.path.basename(background_imgs[i])[:-4] + "-" + os.path.basename(roi_imgs[j])[:-4] + "-" + str(j_angle) + "-" + os.path.basename(roi_imgs[k][:-4]) + "-" + str(k_angle) + ".png" imagePath = os.path.join(imgFolderName, imgFileName) writer = PascalVocWriter(imgFolderName, imgFileName, (img_h, img_w, 3), localImgPath=imagePath, usrname="auto") writer.verified = True writer.addBndBox(x1, y1, x1 + w1, y1 + h1, label_1, 0) writer.addBndBox(x2, y2, x2 + w2, y2 + h2, label_2, 0) writer.save(targetFile=imagePath[:-4] + XML_EXT) if np.random.randint(1 , 11) == 10: result4 = addPepperNoise(result4) if np.random.randint(1 , 6) == 3: result4 = cv2.GaussianBlur(result4 , (5 , 5) , 0) cv2.imwrite(imagePath, result4) cv2.waitKey(0)