File size: 1,390 Bytes
1d64201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import labelme2coco
from PIL import Image
from from_root import from_root
import floorplan_sampler
import json


def main():
    convert_from_labelme()

    handle_jpeg_files("annotations/train.json")
    handle_jpeg_files("annotations/val.json")

    print("NOW FOR SAMPLING")
    floorplan_sampler.main()

def convert_from_labelme():
    os.chdir(str(from_root("dataset")))
    labelme_source_dir = "labelme_data"
    annotation_dest_dir = "annotations"
    training_split_percentage = .8
    labelme2coco.convert(labelme_source_dir, annotation_dest_dir, training_split_percentage, category_id_start=0)


def handle_jpeg_files(coco_path):
    #open file
    file = open(coco_path,"r+")
    coco = json.load(file)

    #find and edit jpeg images
    for image in coco["images"]:
        img_name = image["file_name"]
        if(".jpg" in img_name or ".jpeg" in img_name):
            new_img_name = convert_to_png(img_name)
            image["file_name"]=new_img_name

    #save
    file.seek(0)
    json.dump(coco, file, indent="  ")
    file.close()


def convert_to_png(img_path):
    #load image
    img = Image.open(img_path)

    #remove .jpg or .jpeg from path
    if(".jpeg" in img_path):
        img_path = img_path[0:-5]
    else:
        img_path = img_path[0:-4]
    
    #add .png and save
    img_path += ".png"
    img.save(img_path)
    return img_path



main()