Switch to unified view

a b/Classification/crop_roi_sag.py
1
import os
2
import xml.etree.ElementTree as ET
3
import cv2
4
# import numpy as np
5
6
7
data_dir = "Sag_1-491_5380x2jpgXmls_ResnetRecrop_Jun223020/"
8
sag_dir = data_dir[:-1] + "-scaledby150-cropped-version3/"
9
10
scale_factor = 0.5
11
12
grading = ['1', '2', '3', '4']
13
14
grading_LUT = {
15
            'normal': '1',
16
            'mild': '2',
17
            'moderate': '3',
18
            'severe': '4',
19
        }
20
21
sag_label_file = data_dir[:-1] + "-Jun242020-sagittal-label.txt"
22
23
sag_label = {}
24
25
with open(sag_label_file, 'r') as f:
26
    lines = f.readlines()
27
    for e in lines:
28
        tmp = e.strip().split(' ')
29
        tmp2 = []
30
        for i in range(1, len(tmp)):
31
            tmp2.append(tmp[i])
32
33
        sag_label[tmp[0]] = tmp2
34
35
print("sag_label: ", sag_label)
36
37
if not os.path.exists(sag_dir):
38
    os.makedirs(sag_dir)
39
40
for e in grading:
41
    if not os.path.exists(os.path.join(sag_dir, e)):
42
        os.makedirs(os.path.join(sag_dir, e))
43
44
def scale_crop(xmin, xmax, ymin, ymax, factor, img_shape):
45
    """
46
    the imgs top left corner is (0,0),
47
    x min-max is from point A,
48
    y min-max is from point B
49
    """
50
    cropped_w = xmax - xmin
51
    cropped_h = ymax - ymin
52
    xmin -= ((cropped_w * factor) // 2)
53
    ymin -= ((cropped_h * factor) // 2)
54
55
    xmax += ((cropped_w * factor) // 2)
56
    ymax += ((cropped_h * factor) // 2)
57
58
    # cv2 img shape information
59
    height = img_shape[0]
60
    width = img_shape[1]
61
62
    return (
63
            int(max(xmin, 0)),
64
            int(min(xmax, width)),
65
            int(max(ymin, 0)),
66
            int(min(ymax, height))
67
            )
68
69
for file in os.listdir(data_dir):
70
    if '.xml' in file:
71
        print("\nXML file: ", file)
72
        # Load an color image in grayscale, flag=0
73
        img = cv2.imread(
74
            os.path.join(data_dir, file.replace('xml', 'jpg')),
75
            0)
76
        # NOTE: some imgs are with ext of JPG uppercase
77
        # JPG == jpg == jpeg == JPEG in storage
78
        if img is None:
79
            img = cv2.imread(
80
                os.path.join(
81
                    data_dir,
82
                    file.replace('xml', 'JPG')
83
                ), 0)
84
        print("img.shape: ", img.shape)
85
86
        data = ET.parse(os.path.join(data_dir, file))
87
        root = data.getroot()
88
89
        crops = []
90
        for o in root.findall('object'):
91
            xmin = int(o.find('bndbox').find('xmin').text)
92
            xmax = int(o.find('bndbox').find('xmax').text)
93
            ymin = int(o.find('bndbox').find('ymin').text)
94
            ymax = int(o.find('bndbox').find('ymax').text)
95
96
            print(">>>>before scaling, xmin, xmax, ymin, ymax: ", xmin, xmax, ymin, ymax)
97
98
            (xmin, xmax, ymin, ymax) = scale_crop(xmin, xmax, ymin, ymax, scale_factor, img.shape)
99
100
            print("<<<<after scaling, xmin, xmax, ymin, ymax: ", xmin, xmax, ymin, ymax)
101
102
            cropped_img = img[ymin:ymax, xmin:xmax]
103
            crops.append((ymin, cropped_img))
104
105
        crops = sorted(crops, key=lambda x: x[0])
106
107
        for e in crops:
108
            print("=======crops item:=======")
109
            print(e[0])
110
            #  print("cropped img: ", e[1])
111
            print("img.shape: ", e[1].shape)
112
113
        original_labels = sag_label.get(file, '')
114
        print("original_labels: ", original_labels)
115
        labels = [grading_LUT.get(i, i) for i in original_labels]
116
        print("labels: ", labels)
117
118
        file_index = 0
119
        for i in range(0, len(labels)):
120
            print('labels[i]: ', labels[i])
121
            #  print('crops[i][1]: ', crops[i][1])
122
            file_name = os.path.join(
123
                    sag_dir,
124
                    labels[i],
125
                    file[:-3])
126
            unique_file_name = file_name + \
127
                    'file-' + str(file_index) + \
128
                    '-label-' + str(labels[i]) + \
129
                    ".png"
130
            print("file_name: ", file_name)
131
            print("unique_file_name: ", unique_file_name)
132
            # write to destination folder with the filename:
133
            # <original_filename>.file-<index>-label-<label>.png
134
            if not cv2.imwrite(unique_file_name, crops[i][1]):
135
                raise Exception("Could not write image")
136
            file_index += 1