text_plain_output_1.png
from PIL import Image, ImageDraw

from xml.dom import minidom

import os

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import xml.etree.ElementTree as ET

import numpy as np
import pandas as pd
import xml.etree.ElementTree as ET
from xml.dom import minidom
import os
paths = []
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        paths.append(os.path.join(dirname, filename))
xmls = []
jpgs = []
for path in paths:
    if path.split('.')[-1] == 'xml':
        xmls.append(path)
    if path.split('.')[-1] == 'jpg':
        jpgs.append(path)

tree = ET.parse(xmls[4])
rough_string = ET.tostring(tree.getroot(), 'utf')
reparsed = minidom.parseString(rough_string)

data = {'Number': [], 'Age': [], 'Sex': [], 'Composition': [], 'Echogenicity': [], 'Margins': [], 'Calcifications': [], 'Tirads': [], 'Reportbacaf': [], 'Reporteco': []}
svg_strings = {}
for xml in xmls:
    tree = ET.parse(xml)
    root = tree.getroot()
    case_number = int(root.find('number').text)
    data['Number'].append(case_number)
    if root.find('age').text:
        data['Age'].append(int(root.find('age').text))
    else:
        data['Age'].append(root.find('age').text)
    data['Sex'].append(root.find('sex').text)
    data['Composition'].append(root.find('composition').text)
    data['Echogenicity'].append(root.find('echogenicity').text)
    data['Margins'].append(root.find('margins').text)
    data['Calcifications'].append(root.find('calcifications').text)
    data['Tirads'].append(root.find('tirads').text)
    data['Reportbacaf'].append(root.find('reportbacaf').text)
    data['Reporteco'].append(root.find('reporteco').text)
    for mark in root.findall('mark'):
        image_idx = mark.find('image').text
        svg_strings[f'{case_number}_{image_idx}'] = mark.find('svg').text
df = pd.DataFrame(data)
df.sort_values(by='Number', inplace=True)
df.set_index('Number', inplace=True)

Image.open(jpgs[0])

unique_dims = []
for jpg in jpgs:
    dims = Image.open(jpg).size
    if dims not in unique_dims:
        unique_dims.append(dims)

image_size = unique_dims[0]
list(svg_strings.items())[0]

count = 0
corrupted_xmls = []
for svg_name, svg_str in svg_strings.items():
    count += 1
    img = Image.new('1', image_size)
    draw = ImageDraw.Draw(img)
    if svg_str is not None:
        try:
            svg_content = eval(svg_str)
        except SyntaxError:
            corrupted_xmls.append(svg_name)
    for area in svg_content:
        points = [(point['x'], point['y']) for point in area['points']]
        draw.polygon(points, fill='white')
    img.save(f'/kaggle/working/mask_{svg_name}.jpg')

corrupted_xmls