File size: 5,334 Bytes
be2715b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import numpy as np
import torch
import shutil
import os
import matplotlib.pyplot as plt
import cv2
import json
from PIL import Image
import pickle
from skimage.transform import resize

def split_data(datasetname):
    dir_image = "./dataset_demo/datasetname/images"
    dir_label = "./dataset_demo/datasetname/labels"

    dir_image = dir_image.replace("datasetname", datasetname)
    dir_label = dir_label.replace("datasetname", datasetname)

    path = "./dataset_demo"
    filesplit = "./files_split"

    file_data = os.path.join(filesplit, datasetname+".json")
    path_data = os.path.join(path, datasetname)


    with open(file_data, 'r') as openfile:
        json_object = json.load(openfile)

    for typ in json_object.keys():
        print(typ)
        file_type = json_object[typ]
        path_type = os.path.join(path_data, typ)
        if os.path.exists(path_type):
            shutil.rmtree(path_type)
        os.mkdir(path_type)

        for img in file_type:
            if not "labels" in typ:
                shutil.copy(os.path.join(dir_image, img), os.path.join(path_type, img.replace(" ", "")))
            else:
                shutil.copy(os.path.join(dir_label, img), os.path.join(path_type, img.replace(" ", "")))
                
def save_fileLabel(datasetname):
    file_image = "./dataset_demo/datasetname/type"
    file_label = "./dataset_demo/datasetname/type_labels"
    path = "./dataset_demo/datasetname"
#     if datasetname == "BUID":
#         B = ['train', 'valid']
#     else:
    B = ['train']
    file_image = file_image.replace("datasetname", datasetname)
    file_label = file_label.replace("datasetname", datasetname)
    path = path.replace("datasetname", datasetname)
    with open(os.path.join(path, 'have_label.txt'), 'w') as F:
        for j in B:
            dir_label = file_label.replace("type", j)
            dir_train = file_image.replace("type", j)
            for i in os.listdir(dir_label):
                file = os.path.join(dir_label, i)
                img = np.array(Image.open(file))
                img[img < 50] = 0
                img[img > 200] = 1
                values, counts = np.unique(img, return_counts = True)
                if (len(values) == 2):
                        a = i.replace("_segmentation.png", ".jpg")
                        F.write(os.path.join(dir_train, a))
                        F.write(" ")
                        F.write(file)
                        F.write(" ")
                        F.write("[0, 1]")
                        F.write("\n")
    
    with open(os.path.join(path, 'non_label.txt'), 'w') as F:
        for j in B:
            dir_label = file_label.replace("type", j)
            dir_train = file_image.replace("type", j)
            for i in os.listdir(dir_label):
                file = os.path.join(dir_label, i)
                img = np.array(Image.open(file))
                img[img < 50] = 0
                img[img > 200] = 1
                values, counts = np.unique(img, return_counts = True)

                if (len(values) == 1):
                        a = i.replace("_segmentation.png", ".jpg")
                        F.write(os.path.join(dir_train, a))
                        F.write(" ")
                        F.write(file)
                        F.write(" ")
                        F.write("[0]")
                        F.write("\n")

def save_fileLabel_3D(datasetname):
    file_image = "./dataset_demo/datasetname/type"
    file_label = "./dataset_demo/datasetname/type_labels"
    path = "./dataset_demo/datasetname"
#     if datasetname == "BUID":
#         B = ['train', 'valid']
#     else:
    B = ['train']
    file_image = file_image.replace("datasetname", datasetname)
    file_label = file_label.replace("datasetname", datasetname)
    path = path.replace("datasetname", datasetname)
    with open(os.path.join(path, 'have_label.txt'), 'w') as F:
        for j in B:
            dir_label = file_label.replace("type", j)
            dir_train = file_image.replace("type", j)
            for i in os.listdir(dir_label):
                file = os.path.join(dir_label, i)
                img = np.array(Image.open(file))
                values, counts = np.unique(img, return_counts = True)
                if 60 in values:
                        a = i.replace("label", "image")
                        F.write(os.path.join(dir_train, a))
                        F.write(" ")
                        F.write(file)
                        F.write(" ")
                        F.write("[0, 2]")
                        F.write("\n")
    
    with open(os.path.join(path, 'non_label.txt'), 'w') as F:
        for j in B:
            dir_label = file_label.replace("type", j)
            dir_train = file_image.replace("type", j)
            for i in os.listdir(dir_label):
                file = os.path.join(dir_label, i)
                img = np.array(Image.open(file))
                values, counts = np.unique(img, return_counts = True)

                if not 60 in values:
                        a = i.replace("label", "image")
                        F.write(os.path.join(dir_train, a))
                        F.write(" ")
                        F.write(file)
                        F.write(" ")
                        F.write("[0]")
                        F.write("\n")