def check_labels(labels_dir): if not os.path.isdir(labels_dir): raise Exception("bad dir: " + labels_dir) i = 0 for fn in file_helper.enumerate_files(labels_dir): try: i += 1 print("{} - {}".format(i, fn), end=end_txt) dir_name, name, extension = file_helper.get_file_name_extension(fn) if string_helper.equals_case_insensitive(extension, ".txt"): if name == "classes" and extension == ".txt": continue lines = [] changed = False for line in file_helper.read_lines(fn): lst = line.split(" ") for j in range(1, len(lst)): val = float(lst[j]) if val < 0: changed = True lst[j] = "0" elif val > 1: changed = True lst[j] = "1" new_line = string_helper.join(lst, " ") lines.append(new_line) if changed: file_helper.delete_file(fn) file_helper.write_lines(fn, lines) except Exception as e: print("error - mirror_images: {}".format(fn)) print("change_class_id finished")
def change_class_id_list(labels_dir, class_id_from_list, class_id_to): if not os.path.isdir(labels_dir): raise Exception("bad dir: " + labels_dir) i = 0 for fn in file_helper.enumerate_files(labels_dir): try: i += 1 print("{} - {}".format(i, fn), end=end_txt) dir_name, name, extension = file_helper.get_file_name_extension(fn) if string_helper.equals_case_insensitive(extension, ".txt"): if name == "classes" and extension == ".txt": continue lines = [] changed = False for line in file_helper.read_lines(fn): lst = line.split(" ") if int(lst[0]) in class_id_from_list: changed = True lst[0] = str(class_id_to) new_line = string_helper.join(lst, " ") lines.append(new_line) if changed: file_helper.delete_file(fn) file_helper.write_lines(fn, lines) except Exception as e: print("error - change_class_id: {}".format(fn)) print("change_class_id finished")
def coco_separate_classes(input_dir, output_dir): classes_fn = file_helper.path_join(input_dir, "_classes.txt") classes = [] for line in file_helper.read_lines(classes_fn): classes.append(line) i = 0 ann_fn = file_helper.path_join(input_dir, "_annotations.txt") img_fn = None for line in file_helper.read_lines(ann_fn): try: items = line.split(" ") img_fn = file_helper.path_join(input_dir, items[0]) dir_name, name, extension = file_helper.get_file_name_extension( img_fn) i += 1 print("{} - {}".format(i, img_fn), end=end_txt) mat = cv2.imread(img_fn) h, w = image_helper.image_h_w(mat) for j in range(1, len(items)): item = items[j] values_str = item.split(",") values = [] for v in values_str: values.append(int(v)) class_name = classes[values[4]] write_dir = file_helper.path_join(output_dir, class_name) if not os.path.isdir(write_dir): file_helper.create_dir(write_dir) write_img_fn = file_helper.path_join(write_dir, name + extension) if not os.path.isfile(write_img_fn): file_helper.copy_file(img_fn, write_img_fn) x1, y1, x2, y2 = list(values[:4]) w_ = (x2 - x1) h_ = (y2 - y1) cx = (x1 + w_ * 0.5) / w cy = (y1 + h_ * 0.5) / h line = "0 {} {} {} {}".format(cx, cy, w_ / w, h_ / h) write_lbl_fn = file_helper.path_join(write_dir, name + ".txt") if os.path.isfile(write_lbl_fn): file_helper.append_line(write_lbl_fn, line) else: file_helper.write_lines(write_lbl_fn, [line]) except Exception as e: print('Error - file:{} msg:{}'.format(img_fn, str(e)))
def _oidv6_to_yolo(class_id, class_name, images_dir, labels_dir, yolo_dir): for label_fn in file_helper.enumerate_files(labels_dir, recursive=False): try: print(label_fn, end="\r") name = os.path.basename(label_fn) image_fn = file_helper.path_join(images_dir, name.replace(".txt", ".jpg")) if os.path.isfile(image_fn): write_image_fn = file_helper.path_join( yolo_dir, name.replace(".txt", ".jpg")) write_fn_txt = file_helper.path_join(yolo_dir, name) if os.path.isfile(write_image_fn) and os.path.isfile( write_fn_txt): continue mat = cv2.imread(image_fn) h, w = image_helper.image_h_w(mat) lines = [] for line in file_helper.read_lines(label_fn): line = line.replace("\t", " ").replace(" ", " ").replace( " ", " ").replace(" ", " ").replace(" ", " ") arr0 = line.split(" ") arr = [class_id] x1 = float(arr0[1]) y1 = float(arr0[2]) x2 = float(arr0[3]) y2 = float(arr0[4]) arr.append((x1 + x2) * 0.5 / w) arr.append((y1 + y2) * 0.5 / h) arr.append((x2 - x1) / w) arr.append((y2 - y1) / h) line = ' '.join(str(e) for e in arr) lines.append(line) if len(lines) > 0: # write_image_fn = file_helper.path_join(yolo_dir, name.replace(".txt", ".jpg")) # cv2.imwrite(write_image_fn, mat) file_helper.copy_file(image_fn, write_image_fn) with contextlib.suppress(FileNotFoundError): os.remove(write_fn_txt) file_helper.write_lines(write_fn_txt, lines) else: print("nok: " + label_fn) else: print("no image: " + image_fn) except Exception as e: print('Error - file:{} msg:{}'.format(label_fn, str(e))) print("finished: " + class_name)
def oidv6_to_yolo2(input_images_dir, input_labels_dir, output_labels_and_images_dir, class_id): i = 0 if not os.path.isdir(output_labels_and_images_dir): file_helper.create_dir(output_labels_and_images_dir) for label_fn in file_helper.enumerate_files(input_labels_dir, recursive=False, wildcard_pattern="*.txt"): try: i += 1 print("{} - {}".format(i, label_fn), end=end_txt) dir_name, name, extension = file_helper.get_file_name_extension( label_fn) image_fn = file_helper.path_join(input_images_dir, name + ".jpg") if os.path.isfile(image_fn): mat = cv2.imread(image_fn) h, w = image_helper.image_h_w(mat) lines = [] for line in file_helper.read_lines(label_fn): line = line.replace("\t", " ").replace(" ", " ").replace( " ", " ").replace(" ", " ").replace(" ", " ") arr0 = line.split(" ") arr = [class_id] x1 = float(arr0[1]) y1 = float(arr0[2]) x2 = float(arr0[3]) y2 = float(arr0[4]) arr.append((x1 + x2) * 0.5 / w) arr.append((y1 + y2) * 0.5 / h) arr.append((x2 - x1) / w) arr.append((y2 - y1) / h) line = ' '.join(str(e) for e in arr) lines.append(line) out_img_fn = file_helper.path_join( output_labels_and_images_dir, name + ".jpg") out_lbl_fn = file_helper.path_join( output_labels_and_images_dir, name + ".txt") if os.path.isfile(out_img_fn): os.remove(out_img_fn) if os.path.isfile(out_lbl_fn): os.remove(out_lbl_fn) file_helper.write_lines(out_lbl_fn, lines) file_helper.copy_file(image_fn, out_img_fn) else: print("no image: " + image_fn) except Exception as e: print('Error - file:{} msg:{}'.format(label_fn, str(e)))
def delete_classes(images_dir, classes_old, classes_new): class_id_map = {} for old_name in classes_old: old_id = str(classes_old.index(old_name)) if old_name in classes_new: class_id_map[old_id] = str(classes_new.index(old_name)) else: class_id_map[old_id] = None i = 0 for fn in file_helper.enumerate_files(images_dir, wildcard_pattern="*.txt"): try: i += 1 lines = [] changed = False for line in file_helper.read_lines(fn): items = line.split(" ") old_id = items[0] new_id = class_id_map[old_id] if new_id is not None: if new_id != old_id: changed = True items[0] = new_id lines.append(string_helper.join(items, " ")) else: changed = True if changed: file_helper.delete_file(fn) if len(lines) > 0: # print("changed - {}".format(fn)) file_helper.write_lines(fn, lines) print("{} - {} EDITED".format(i, fn), end=end_txt) else: print("{} - {} DELETED".format(i, fn), end=end_txt) else: print("{} - {}".format(i, fn), end=end_txt) except Exception as e: print('Error - merge_single_classes - file: {} msg: {}'.format( fn, str(e))) check_single_files(images_dir)
def oidv6_to_yolo(images_and_labels_dir, class_id): for label_fn in file_helper.enumerate_files(images_and_labels_dir, recursive=False, wildcard_pattern="*.txt"): try: print(label_fn, end=end_txt) name = os.path.basename(label_fn) image_fn = file_helper.path_join(images_and_labels_dir, name.replace(".txt", ".jpg")) if os.path.isfile(image_fn): mat = cv2.imread(image_fn) h, w = image_helper.image_h_w(mat) lines = [] for line in file_helper.read_lines(label_fn): line = line.replace("\t", " ").replace(" ", " ").replace( " ", " ").replace(" ", " ").replace(" ", " ") arr0 = line.split(" ") arr = [class_id] x1 = float(arr0[1]) y1 = float(arr0[2]) x2 = float(arr0[3]) y2 = float(arr0[4]) arr.append((x1 + x2) * 0.5 / w) arr.append((y1 + y2) * 0.5 / h) arr.append((x2 - x1) / w) arr.append((y2 - y1) / h) line = ' '.join(str(e) for e in arr) lines.append(line) with contextlib.suppress(FileNotFoundError): os.remove(label_fn) file_helper.write_lines(label_fn, lines) else: print("no image: " + image_fn) except Exception as e: print('Error - file:{} msg:{}'.format(label_fn, str(e)))
def combine_classes(class_items, out_images_dir, out_labels_dir, out_classes_txt_fn, out_style="yolo"): # a typical class_item # { # "class_name": "vehicle license plate", # "dirs": # [ # { # "images": "C:/_koray/train_datasets/yolo_oidv6_class0/vehicle_registration_plate", # "labels": "C:/_koray/train_datasets/yolo_oidv6_class0/vehicle_registration_plate" # }, # { # "images": "C:/_koray/train_datasets/yolo_misc/vehicle_registration_plate/class0", # "labels": "C:/_koray/train_datasets/yolo_misc/vehicle_registration_plate/class0" # } # ], # "resize_style": None, "if_larger" or "crop" - if "crop" -> make the image unique, don't combine, other combine with other classes # "image_size": 640, - if None no resize, combine all # "style": "yolo" # } class_names = [] if out_style == "yolo": if not os.path.isdir(out_images_dir): file_helper.create_dir(out_images_dir) if not os.path.isdir(out_labels_dir): file_helper.create_dir(out_labels_dir) if os.path.isfile(out_classes_txt_fn): for class_name in file_helper.read_lines(out_classes_txt_fn): class_names.append(class_name) else: raise Exception("Bad out_style: " + out_style) for class_item in class_items: class_name = class_item["class_name"] resize_style = class_item["resize_style"] image_size = class_item["image_size"] if image_size is not None: image_size_txt = "{}_{}".format(image_size[0], image_size[1]) else: image_size_txt = None input_style = class_item["input_style"] if class_name in class_names: class_index = class_names.index(class_name) else: class_index = len(class_names) class_names.append(class_name) file_helper.append_line(out_classes_txt_fn, class_name) i = 0 for dir_item in class_item["dirs"]: images_dir = dir_item["images"] labels_dir = dir_item["labels"] if input_style == "yolo": for label_fn in file_helper.enumerate_files( labels_dir, wildcard_pattern="*.txt"): try: _dir_name, name, _extension = file_helper.get_file_name_extension( label_fn) i += 1 print("{} - {}".format(i, label_fn), end=end_txt) for line in file_helper.read_lines(label_fn): line_items = line.split(" ") cx_norm = float(line_items[1]) cy_norm = float(line_items[2]) w_norm = float(line_items[3]) h_norm = float(line_items[4]) line_items[0] = str(class_index) out_lbl_fn = None out_img_fn = None mat = None # for image_fn in file_helper.enumerate_files(images_dir, wildcard_pattern=name + ".*"): image_fn = find_image_file(images_dir, name) if image_fn is not None: _dir_name, _name, extension = file_helper.get_file_name_extension( image_fn) if extension != ".txt": try: mat = cv2.imread(image_fn) except Exception as e: print( 'Error reading image file: {} msg:{}' .format(image_fn, str(e))) if mat is not None: if image_size is None or resize_style is None: out_img_fn = file_helper.path_join( out_images_dir, name + ".jpg") out_lbl_fn = file_helper.path_join( out_labels_dir, name + ".txt") elif resize_style == "if_larger": out_img_fn = file_helper.path_join( out_images_dir, name + "_" + image_size_txt + ".jpg") out_lbl_fn = file_helper.path_join( out_labels_dir, name + "_" + image_size_txt + ".txt") mat = image_helper.resize_if_larger( mat, max(image_size[0], image_size[1])) elif resize_style == "crop": new_name = file_helper.get_unique_file_name( ) out_img_fn = file_helper.path_join( out_images_dir, name + "_crop_" + new_name + ".jpg") out_lbl_fn = file_helper.path_join( out_labels_dir, name + "_crop_" + new_name + ".txt") mat, cx, cy, w, h = crop_rect( mat, cx_norm, cy_norm, w_norm, h_norm, image_size) line_items[1] = str(cx) line_items[2] = str(cy) line_items[3] = str(w) line_items[4] = str(h) else: raise Exception( "Bad resize_style: " + resize_style) else: raise Exception("Cannot find image file") if out_lbl_fn is not None: line = string_helper.join(line_items, " ") if os.path.isfile(out_lbl_fn): file_helper.append_line(out_lbl_fn, line) else: file_helper.write_lines(out_lbl_fn, [line]) if out_img_fn is not None and mat is not None: if not os.path.isfile(out_img_fn): cv2.imwrite(out_img_fn, mat) except Exception as e: print('Error - file:{} msg:{}'.format( label_fn, str(e))) else: raise Exception("Bad input_style: " + out_style)
def oidv6_to_yolo_multi(input_multi_oidv6_dir, output_yolo_dir): if not os.path.isdir(output_yolo_dir): file_helper.create_dir(output_yolo_dir) output_images_dir = file_helper.path_join(output_yolo_dir, "images") if not os.path.isdir(output_images_dir): file_helper.create_dir(output_images_dir) output_labels_dir = file_helper.path_join(output_yolo_dir, "labels") if not os.path.isdir(output_labels_dir): file_helper.create_dir(output_labels_dir) classes = [] print("started.... oidv6_to_yolo_multi - {}".format(input_multi_oidv6_dir)) i = 0 for sub_dir in ["test", "train", "validation"]: images_dir = file_helper.path_join(input_multi_oidv6_dir, sub_dir) labels_dir = file_helper.path_join(images_dir, "labels") for image_fn in file_helper.enumerate_files(images_dir, recursive=False): try: dir_name, name, extension = file_helper.get_file_name_extension( image_fn) label_fn = file_helper.path_join(labels_dir, name + ".txt") if not os.path.isfile(label_fn): print("!!! File has no label: {}".format(image_fn)) else: i += 1 print("processing {} - {}".format(str(i), image_fn), end=" \r") key_name = name[name.rfind("_") + 1:] out_image_fn = file_helper.path_join( output_yolo_dir, "images", key_name + extension) out_label_fn_1 = file_helper.path_join( output_yolo_dir, "images", key_name + ".txt") out_label_fn_2 = file_helper.path_join( output_yolo_dir, "labels", key_name + ".txt") class_name = name[:name.rfind("_")] if class_name not in classes: classes.append(class_name) print(class_name) class_id = classes.index(class_name) if os.path.isfile(out_image_fn) and os.path.isfile( out_label_fn_2): exists = False for line in file_helper.read_lines(out_label_fn_2): line = line.replace("\t", " ").replace( " ", " ").replace(" ", " ").replace( " ", " ").replace(" ", " ") if class_id == int(line.split(" ")[0]): exists = True break if exists: continue mat = cv2.imread(image_fn) h, w = image_helper.image_h_w(mat) lines = [] for line in file_helper.read_lines(label_fn): line = line.replace("\t", " ").replace( " ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ") arr0 = line.split(" ") arr = [class_id] x1 = float(arr0[1]) y1 = float(arr0[2]) x2 = float(arr0[3]) y2 = float(arr0[4]) arr.append((x1 + x2) * 0.5 / w) arr.append((y1 + y2) * 0.5 / h) arr.append((x2 - x1) / w) arr.append((y2 - y1) / h) line = ' '.join(str(e) for e in arr) lines.append(line) if len(lines) > 0: cv2.imwrite(out_image_fn, mat) # file_helper.copy_file(image_fn, out_image_fn) # if os.path.isfile(out_label_fn_2): # print("merged: " + out_label_fn_2) for line in lines: file_helper.append_line(out_label_fn_1, line) file_helper.append_line(out_label_fn_2, line) else: print("nok: " + label_fn) except Exception as e: print('Error - file:{} msg:{}'.format(image_fn, str(e))) classes_txt_fn = file_helper.path_join(output_yolo_dir, "classes.txt") file_helper.write_lines(classes_txt_fn, classes) print("finished: oidv6_to_yolo_multi")
H = w * s + h * c W = w * c + h * s div = (c * c - s * s) if abs(div) < 0.001: div = 0.001 w1 = w / 2 + (H * c - W * s) / div h1 = h / 2 - (H * s - W * c) / div arr.append(cx / w0) arr.append(cy / h0) arr.append(w1 / w0) arr.append(h1 / h0) line = ' '.join(str(e) for e in arr) lines.append(line) write_fn_txt = file_helper.path_join(write_dir, os.path.basename(fn_jpg)).replace( ".JPG", ".txt") with contextlib.suppress(FileNotFoundError): os.remove(write_fn_txt) file_helper.write_lines(write_fn_txt, lines) write_fn_jpg = file_helper.path_join(write_dir, os.path.basename(fn_jpg)) with contextlib.suppress(FileNotFoundError): os.remove(write_fn_jpg) copyfile(fn_jpg, write_fn_jpg) print("finished")