File size: 2,856 Bytes
dba26ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import csv
from Damage_calculation import DamageCalculator

class CSVGenerator:
    def __init__(self, output_folder):
        self.damage_folder = os.path.join(output_folder, 'damage/labels')
        self.parts_folder = os.path.join(output_folder, 'parts/labels')
        self.output_csv = os.path.join(output_folder, 'damage_estimation.csv')
        self.calculator = DamageCalculator()

    def process_files(self):
        damage_files = sorted(os.listdir(self.damage_folder))
        parts_files = sorted(os.listdir(self.parts_folder))

        total_cost_min = 0
        total_cost_max = 0
        damage_data = []

        with open(self.output_csv, 'w', newline='') as csvfile:
            fieldnames = ['Car Parts', 'Damage Type', 'Damage Count', 'Severity', 'Estimated Cost Range']
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            writer.writeheader()

            for damage_file, parts_file in zip(damage_files, parts_files):
                damage_path = os.path.join(self.damage_folder, damage_file)
                parts_path = os.path.join(self.parts_folder, parts_file)

                damage_classes, damage_polygons = self.calculator.parse_coordinates(damage_path)
                part_classes, part_polygons = self.calculator.parse_coordinates(parts_path)

                damage_polygons = list(zip(damage_classes, damage_polygons))
                part_polygons = list(zip(part_classes, part_polygons))

                intersection_info = self.calculator.calculate_intersection_area(damage_polygons, part_polygons)

                for info in intersection_info:
                    writer.writerow({
                        'Car Parts': info['part_class'],
                        'Damage Type': info['damage_class'],
                        'Damage Count': info['count'],
                        'Severity': info['severity'],
                        'Estimated Cost Range': f"Rs. {info['cost_min']}-{info['cost_max']}"
                    })
                    total_cost_min += info['cost_min']
                    total_cost_max += info['cost_max']

                    damage_data.append({
                        'damage_class': info['damage_class'],
                        'part_class': info['part_class'],
                        'coverage_percentage': info['coverage_percentage']
                    })

            writer.writerow({
                'Car Parts': 'Total',
                'Damage Type': '',
                'Damage Count': '',
                'Severity': '',
                'Estimated Cost Range': f"Rs. {total_cost_min}-{total_cost_max}"
            })

        return damage_data

def main(output_folder):
    generator = CSVGenerator(output_folder)
    generator.process_files()

if __name__ == "__main__":
    output_folder = 'Output'
    main(output_folder)