<jupyter_start><jupyter_text>Input format: File1: dataset/QUT_fish_data/annotations/species_name.pts  PTS file (1 line) containing bounding box:if fish facing left:```bounding_box:   ;   ;  ;    ```if fish facing right:```bounding_box:    ;  ;   ;   ; ``` File2: dataset/QUT_fish_data/final_all_index.txt  txt file where each line is:```===``` Output format : (1 file) where each line is: ```image_file_path x_min,y_min,x_max,y_max,class_id```<jupyter_code># returns the coordinates of the corners of an annotation
def get_coord(annotation_path):
    f = open(annotation_path, "r")
    line = f.read()
    
    ### parse lines
    
    # assume fish is facing left
    top_left, top_right, bottom_right, bottom_left = line.split(";")
    temp_top_left_x, temp_top_left_y = top_left.split(':')[1].strip().split("  ")
    temp_top_right_x, temp_top_right_y = top_right.strip().split("  ")
    temp_bottom_right_x, temp_bottom_right_y = bottom_right.strip().split("  ")
    temp_bottom_left_x, temp_bottom_left_y = bottom_left.strip().split(" ")
    
    # prepare return values
    top_left_x = temp_top_left_x;
    top_left_y = temp_top_left_y;
    top_right_x = temp_top_right_x;
    top_right_y = temp_top_right_y;
    bottom_right_x = temp_bottom_right_x;
    bottom_right_y = temp_bottom_right_y;
    bottom_left_x = temp_bottom_left_x;
    bottom_left_y = temp_bottom_left_y;

    
    # if fish is facing right, swap coordinate values
    if float(temp_top_left_x) > float(temp_bottom_right_x): # will only be true if facing right
        top_right_x = temp_top_left_x
        top_right_y = temp_top_left_y
        bottom_right_x = temp_top_right_x
        bottom_right_y = temp_top_right_y
        bottom_left_x = temp_bottom_right_x
        bottom_left_y = temp_bottom_right_y
        top_left_x = temp_bottom_left_x
        top_left_y = temp_bottom_left_y
    
    f.close()
    return (float(top_left_x), float(top_left_y)), (float(top_right_x), float(top_right_y)),(float(bottom_right_x), float(bottom_right_y)), (float(bottom_left_x), float(bottom_left_y))
coords = get_coord(r"keras-yolo3\dataset\QUT_fish_data\annotations\acanthaluteres_vittiger_5.pts")
coords
# change corner coordinate format to box format
def get_box(coords):
    top_left, top_right, bottom_right, bottom_left = coords
    min_x = min(top_left[0], bottom_left[0])
    max_x = max(top_right[0], bottom_right[0])
    min_y = min(top_left[1], bottom_left[1])
    max_y = max(top_right[1], bottom_right[1])
    return int(min_x), int(max_x), int(min_y), int(max_y)
get_box(coords)
def parse_species(line):
    class_no, species_name, img_type, img_name, idx = line.split("=")
    return species_name, img_type, img_name
def generate_yolo_annotations():
    species = open(r"keras-yolo3/dataset/QUT_fish_data/final_all_index.txt ","r")
    row_box_annotations = open(r"keras-yolo3/train.txt","w")
    classes = open(r"keras-yolo3/dataset/fish_classes.txt", "w")
    classes_map = {};
    class_no = -1
    for line in species.readlines():
        species_name, img_type, img_name = parse_species(line)
        if(img_type=='insitu'):
            if species_name not in classes_map:
                classes.write(f"{species_name}\n")
                classes_map[species_name] = True
                class_no+=1
            img_path = f"dataset/QUT_fish_data/images/raw_images/{img_name}.jpg"
            annotation_path = f"keras-yolo3/dataset/QUT_fish_data/annotations/{img_name}.pts"
            coords = get_coord(annotation_path)
            min_x,max_x,min_y,max_y = get_box(coords)
            row_box_annotations.write(f"{img_path} {min_x},{min_y},{max_x},{max_y},{class_no}\n")
            
    species.close()
    row_box_annotations.close()
    classes.close()
generate_yolo_annotations()<jupyter_output><empty_output><jupyter_text>Input format: File1: dataset/QUT_fish_data/annotations/species_name.pts  CSV file containing bounding box:```,,,,,   ``` File2: dataset/QUT_fish_data/final_all_index.txt  txt file where each line is:```===``` Output format : (1 file) where each line is: ```image_file_path x_min,y_min,x_max,y_max,class_id```<jupyter_code>def parse_csv_line(line):
    img_name, xmin, ymin, xmax, ymax, label = line.split(',')
    return img_name[1:-1], int(float(xmin)), int(float(ymin)), int(float(xmax)), int(float(ymax)), label.strip()[1:-1]

def parse_species(line):
    class_no, species_name, img_type, img_name, idx = line.split("=")
    return species_name, img_type, img_name

def csv_to_yolo_annot():
    species = open(r"keras-yolo3/dataset/QUT_fish_data/final_all_index.txt ","r")
    csv_path = r'keras-yolo3/dataset/QUT_fish_data/images/test/res/vott-csv-export/htfdx-b-fdgcfbdhx-fdg-export.csv'
    imgs_path =  r'keras-yolo3/dataset/QUT_fish_data/images/test/res'
    row_box_annotations = open(r"keras-yolo3/train2.txt","w")
    csv = open(csv_path,'r')
    csv.readline() # first line contains meta data
    for line in csv.readlines():
        img_name, xmin, ymin, xmax, ymax, label = parse_csv_line(line)
        img_path = imgs_path+'/'+img_name
        row_box_annotations.write(f"{img_path} {xmin},{ymin},{xmax},{ymax},{class_no}\n")
    csv.close()

csv_to_yolo_annot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12586074980872228, 'fraction_numerical': 0.006312165263963275, 'mean_word_length': 4.26056338028169, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '411098', 'license': 'MIT', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1929, 'n_words': 319, 'path': 'format_annotations.ipynb', 'repo_name': 'Ali-Amin/marine-life-identification', 'size': '5228'}