File size: 2,882 Bytes
a463462
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import numpy as np

def class_name(classid):
    id_dict = {1:'Scratch', 2:'Dent', 3:'Shatter', 4:'Dislocation'}
    return id_dict[classid]

def damage_cost(classid):
    # cost_dict = {1: [800, 1400], 2:[1200, 3000],3:19000, 4:17000}
    cost_dict = {1: 900, 2:1600, 3:19000, 4:17000}

    return cost_dict[classid]

def area_ratio(image, roi, mask):
    y1, x1, y2, x2 =  tuple(roi)
    crop_mask = mask[y1:y1+(y2-y1),x1:x1+(x2-x1)].copy()
    pixels = cv2.countNonZero(np.float32(crop_mask))
    image_area = image.shape[0] * image.shape[1]
    area_ratio = 1 + (pixels / image_area)
    return area_ratio

def costEstimate(image, rois, masks, classids):
    cost_id_dict = {
    "Shatter": {"Count": 0, "Cost": 0},
    "Scratch": {"Count": 0, "Cost": 0},
    "Dent": {"Count": 0, "Cost": 0},
    "Dislocation": {"Count": 0, "Cost": 0}
    }
    total = 0
    count = int()
    cost_init = int()
    
    for index in range(rois.shape[0]):

        name = class_name(classids[index])
        cost = damage_cost(classids[index])
        ratio = area_ratio(image, rois[index], masks[: ,: ,index])

        total = total + round(cost * ratio,2)

        # unique_id = str()
        
        # for roi in rois[index]:
        #     unique_id = unique_id + str(roi)
            
        
        if name is 'Scratch':
            count = cost_id_dict[name]['Count'] + 1
            cost_init = cost_id_dict[name]['Cost'] + round(cost * ratio,2)
            cost_id_dict[name]['Count'] = count
            cost_id_dict[name]['Cost'] = cost_init
            # cost_id_dict[name] = "Range: Rs." + str(round(cost[0] * ratio,3)) + ' - Rs.' + str(round(cost[1] * ratio, 3))
        elif name is 'Dent':
            count = cost_id_dict[name]['Count'] + 1
            cost_init = cost_id_dict[name]['Cost'] + round(cost * ratio,2)
            cost_id_dict[name]['Count'] = count
            cost_id_dict[name]['Cost'] = cost_init
            # cost_id_dict[name] = "Range: Rs." + str(cost[0] * ratio) + ' - Rs.' + str(cost[1] * ratio)
        elif name is 'Shatter':
            count = cost_id_dict[name]['Count'] + 1
            cost_init = cost_id_dict[name]['Cost'] + round(cost * ratio,2)
            cost_id_dict[name]['Count'] = count
            cost_id_dict[name]['Cost'] = cost_init
            # cost_id_dict[name] = "Cost: Rs." + str(cost)
        else:
            count = cost_id_dict[name]['Count'] + 1
            cost_init = cost_id_dict[name]['Cost'] + round(cost * ratio,2)
            cost_id_dict[name]['Count'] = count
            cost_id_dict[name]['Cost'] = cost_init
            # cost_id_dict[name] = "Cost: Rs." + str(cost)

    for name, values in cost_id_dict.copy().items():
        if values['Count'] == 0:
            cost_id_dict.pop(name)

    return total, cost_id_dict