def main(): pp = pprint.PrettyPrinter(indent=4) img_path = args.img_path print("imageee path",img_path) label_path = args.label_path img_type = args.img_type datasets = args.datasets cls_list = args.cls_list_file result = None data = None if datasets == "COCO": coco = COCO() result, data = coco.parse(label_path) elif datasets == "VOC": voc = VOC() result, data = voc.parse(label_path) elif datasets == "UDACITY": udacity = UDACITY() result, data = udacity.parse(label_path, img_path) elif datasets == "KITTI": kitti = KITTI() result, data = kitti.parse(label_path, img_path, img_type=img_type) elif datasets == "YOLO": yolo =YOLO(os.path.abspath(cls_list)) result, data = yolo.parse(label_path, img_path, img_type=img_type) if result is True: for key in data: filepath = "".join([img_path, key, img_type]) im = Image.open(filepath) draw = ImageDraw.Draw(im) print("data['{}']: ".format(key), end="") pp.pprint(data[key]) print("num_object : {}".format(data[key]["objects"]["num_obj"])) for idx in range(0, int(data[key]["objects"]["num_obj"])): print("idx {}, name : {}, bndbox :{}".format(idx, data[key]["objects"][str(idx)]["name"], data[key]["objects"][str(idx)]["bndbox"])) x0 = data[key]["objects"][str(idx)]["bndbox"]["xmin"] y0 = data[key]["objects"][str(idx)]["bndbox"]["ymin"] x1 = data[key]["objects"][str(idx)]["bndbox"]["xmax"] y1 = data[key]["objects"][str(idx)]["bndbox"]["ymax"] draw.rectangle(((x0,y0), (x1,y1)), outline='#00ff88') draw.text((x0,y0), data[key]["objects"][str(idx)]["name"]) del draw print("===============================================================================================\n\n") plt.imshow(im) plt.show() plt.clf() im.close() else: print("return value : {}, msg : {}, args: {}".format(result, data, args))
def main(config): if config["no_label"]: make_image_path(config) return coco = COCO() print(config["label"]) print(config["img_path"]) flag, data, cls_hierarchy = coco.parse(config["label"], config["img_path"]) yolo = YOLO(os.path.abspath(config["cls_list"]), cls_hierarchy=cls_hierarchy) if flag == True: flag, data = yolo.generate(data) if flag == True: flag, data = yolo.save(data, config["output_path"], config["img_path"], config["img_type"], config["manifest_path"], config["manifest_name"]) if flag == False: print("Saving Result : {}, msg : {}".format(flag, data)) else: print("YOLO Generating Result : {}, msg : {}".format(flag, data)) else: print("COCO Parsing Result : {}, msg : {}".format(flag, data))
def main(config): if config["datasets"] == "VOC": voc = VOC() yolo = YOLO(os.path.abspath(config["cls_list"])) print('parsing...') flag, data = voc.parse(config["label"]) if flag == True: print('parsing succeeded') flag, data = yolo.generate(data) if flag == True: print('saving results') if not os.path.exists(config["output_path"]): os.makedirs(config["output_path"]) flag, data = yolo.save(data, config["output_path"], config["img_path"], config["img_type"], config["manifest_path"]) if flag == False: print("Saving Result : {}, msg : {}".format(flag, data)) else: print("YOLO Generating Result : {}, msg : {}".format( flag, data)) else: print("VOC Parsing Result : {}, msg : {}".format(flag, data))
def main(config): if config["datasets"] == "VOC": voc = VOC() yolo = YOLO(os.path.abspath(config["cls_list"])) flag, data = voc.parse(config["label"]) if flag == True: flag, data = yolo.generate(data) if flag == True: flag, data = yolo.save(data, config["output_path"], config["img_path"], config["img_type"], config["manipast_path"], config["train"]) if flag == False: print("Saving Result : {}, msg : {}".format(flag, data)) else: print("YOLO Generating Result : {}, msg : {}".format( flag, data)) else: print("VOC Parsing Result : {}, msg : {}".format(flag, data)) else: print("Unknown Datasets")
def main(config): if config["datasets"] == "VOC": voc = VOC() yolo = YOLO(os.path.abspath(config["cls_list"])) flag, data = voc.parse(config["label"]) if flag == True: flag, data = yolo.generate(data) if flag == True: flag, data = yolo.save(data, config["output_path"], config["img_path"], config["img_type"], config["manipast_path"]) if flag == False: print("Saving Result : {}, msg : {}".format(flag, data)) else: print("YOLO Generating Result : {}, msg : {}".format( flag, data)) else: print("VOC Parsing Result : {}, msg : {}".format(flag, data)) elif config["datasets"] == "COCO": coco = COCO() yolo = YOLO(os.path.abspath(config["cls_list"])) flag, data = coco.parse(config["label"]) if flag == True: flag, data, data_annotation = yolo.generate(data) if flag == True: flag, data = yolo.save_annotation(data, data_annotation, config["output_path"], config["img_path"], config["img_type"], config["manipast_path"]) if flag == False: print("Saving Result : {}, msg : {}".format(flag, data)) else: print("YOLO Generating Result : {}, msg : {}".format( flag, data)) else: print("COCO Parsing Result : {}, msg : {}".format(flag, data)) elif config["datasets"] == "UDACITY": udacity = UDACITY() yolo = YOLO(os.path.abspath(config["cls_list"])) flag, data = udacity.parse(config["label"]) if flag == True: flag, data = yolo.generate(data) if flag == True: flag, data = yolo.save(data, config["output_path"], config["img_path"], config["img_type"], config["manipast_path"]) if flag == False: print("Saving Result : {}, msg : {}".format(flag, data)) else: print("UDACITY Generating Result : {}, msg : {}".format( flag, data)) else: print("COCO Parsing Result : {}, msg : {}".format(flag, data)) elif config["datasets"] == "KITTI": kitti = KITTI() yolo = YOLO(os.path.abspath(config["cls_list"])) flag, data = kitti.parse(config["label"], config["img_path"], img_type=config["img_type"]) if flag == True: flag, data = yolo.generate(data) if flag == True: flag, data = yolo.save(data, config["output_path"], config["img_path"], config["img_type"], config["manipast_path"]) if flag == False: print("Saving Result : {}, msg : {}".format(flag, data)) else: print("YOLO Generating Result : {}, msg : {}".format( flag, data)) else: print("KITTI Parsing Result : {}, msg : {}".format(flag, data)) else: print("Unkwon Datasets")
def generate_voc_annotations(): image_num = 0 images_train = [] xml_files = get_xml_files() voc_format = VOC() yolo_format = YOLO(os.path.abspath(os.path.join(data_yolo, 'penelope.name').replace("\\", "/"))) with tqdm(bar_format='{l_bar}{bar}{n_fmt}/{total_fmt} [{elapsed}<{remaining}]') as pbar: for xml_file in xml_files: root = ElementTree.parse(xml_file).getroot() title = root.get('title') pages = root.find('pages') pbar.total = len(xml_files) pbar.set_description("Labeling {0}".format(title), refresh=True) # pbar.reset() pbar.update() for page in pages: index_img = page.get('index').zfill(3) width = page.get('width') height = page.get('height') texts = page.findall('text') # Some page doesn't have texts if not texts: continue image_path = os.path.join(images_dir, title, index_img + '.jpg').replace("\\", "/") new_image_path = os.path.join(data_yolo_labels, str(image_num) + '.jpg').replace("\\", "/") copyfile(image_path, new_image_path) images_train.append(new_image_path + '\n') voc_file = os.path.join(images_voc_dir, str(image_num) + '.xml').replace("\\", "/") voc_root = ElementTree.Element("annotations") ElementTree.SubElement(voc_root, "filename").text = "{}.jpg".format(image_num) ElementTree.SubElement(voc_root, "folder").text = new_image_path source = ElementTree.SubElement(voc_root, "source") ElementTree.SubElement(source, "database").text = "Unknown" size = ElementTree.SubElement(voc_root, "size") ElementTree.SubElement(size, "width").text = str(width) ElementTree.SubElement(size, "height").text = str(height) ElementTree.SubElement(size, "depth").text = "3" ElementTree.SubElement(voc_root, "segmented").text = str(0) for each_text in texts: # Draw rectangle around the text # +/- 10 because some letters are output of the rectangle x_min = int(each_text.get('xmin')) - 10 x_max = int(each_text.get('xmax')) + 10 y_min = int(each_text.get('ymin')) - 10 y_max = int(each_text.get('ymax')) + 10 text = each_text.text if text is not None: obj = ElementTree.SubElement(voc_root, "object") ElementTree.SubElement(obj, "name").text = "text" ElementTree.SubElement(obj, "pose").text = "Unspecified" ElementTree.SubElement(obj, "truncated").text = str(0) ElementTree.SubElement(obj, "difficult").text = str(0) bbox = ElementTree.SubElement(obj, "bndbox") ElementTree.SubElement(bbox, "xmin").text = str(x_min) ElementTree.SubElement(bbox, "ymin").text = str(y_min) ElementTree.SubElement(bbox, "xmax").text = str(x_max) ElementTree.SubElement(bbox, "ymax").text = str(y_max) # TEST PYTESSERACT # RESULT : FAILED # image_roi = cv_image[y_min:y_max, x_min:x_max] # configuration = "-l jpn_vert --oem 1 --psm 5" # pytext = pytesseract.image_to_string(image_roi, config=configuration).replace("\n", "").replace(" ", "") # if text == pytext: # print("It's equal {0} and {1}".format(text, pytext)) # else: # print("It's NOT equal {0} and {1}".format(text, pytext)) # assert text == pytext, "Difference between {0} and {1}".format(text, pytext) # For debugging # cv2.rectangle(cv_image, (int(x_min), int(y_min)), (int(x_max), int(y_max)), (0, 255, 0), 2) image_num = image_num + 1 xml_str = minidom.parseString(ElementTree.tostring(voc_root)).toprettyxml(indent=" ") with open(voc_file, "w") as f: f.write(xml_str) with open(os.path.join(data_yolo, 'train.txt').replace("\\", "/"), 'w') as f: f.writelines(images_train) flag, data = voc_format.parse(images_voc_dir) if flag: flag, data = yolo_format.generate(data) if flag: flag, data = yolo_format.save(data, data_yolo_labels, data_yolo_labels, '.jpg', data_yolo_labels) if not flag: print("Saving Result : {}, msg : {}".format(flag, data)) else: print("YOLO Generating Result : {}, msg : {}".format(flag, data))
def main(config): if config["datasets"] == "VOC": voc = VOC() yolo = YOLO(os.path.abspath(config["cls_list"])) flag, data = voc.parse(config["label"]) if flag == True: flag, data = yolo.generate(data) if flag == True: flag, data = yolo.save(data, config["output_path"], config["img_path"], config["img_type"], config["manifest_path"]) if flag == False: print("Saving Result : {}, msg : {}".format(flag, data)) else: print("YOLO Generating Result : {}, msg : {}".format( flag, data)) else: print("VOC Parsing Result : {}, msg : {}".format(flag, data)) elif config["datasets"] == "COCO": coco = COCO() keep = { "person", "bicycle", "car", "motorcycle", "bus", "train", "truck" } flag, data, cls_hierarchy = coco.parse(config["label"], config["img_path"], keep=keep) data = sampleDataset(data, config["num_samples"], keep, config["seed"]) yolo = YOLO(os.path.abspath(config["cls_list"]), cls_hierarchy=cls_hierarchy) if flag == True: flag, data = yolo.generate(data) if flag == True: flag, data = yolo.save(data, config["output_path"], config["img_path"], config["img_type"], config["manifest_path"]) if flag == False: print("Saving Result : {}, msg : {}".format(flag, data)) else: print("YOLO Generating Result : {}, msg : {}".format( flag, data)) else: print("COCO Parsing Result : {}, msg : {}".format(flag, data)) elif config["datasets"] == "UDACITY": udacity = UDACITY() yolo = YOLO(os.path.abspath(config["cls_list"])) flag, data = udacity.parse(config["label"]) if flag == True: flag, data = yolo.generate(data) if flag == True: flag, data = yolo.save(data, config["output_path"], config["img_path"], config["img_type"], config["manifest_path"]) if flag == False: print("Saving Result : {}, msg : {}".format(flag, data)) else: print("UDACITY Generating Result : {}, msg : {}".format( flag, data)) else: print("COCO Parsing Result : {}, msg : {}".format(flag, data)) elif config["datasets"] == "KITTI": kitti = KITTI() yolo = YOLO(os.path.abspath(config["cls_list"])) flag, data = kitti.parse(config["label"], config["img_path"], img_type=config["img_type"]) if flag == True: flag, data = yolo.generate(data) if flag == True: flag, data = yolo.save(data, config["output_path"], config["img_path"], config["img_type"], config["manifest_path"]) if flag == False: print("Saving Result : {}, msg : {}".format(flag, data)) else: print("YOLO Generating Result : {}, msg : {}".format( flag, data)) else: print("KITTI Parsing Result : {}, msg : {}".format(flag, data)) else: print("Unkwon Datasets")