reidddd commited on
Commit
93224ed
·
1 Parent(s): e3eebda

add fetch image api

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +64 -1
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ model_final.pth
app.py CHANGED
@@ -97,7 +97,70 @@ def upload():
97
  class_ids = instances.pred_classes.numpy()
98
 
99
  # Filter overlapping boxes using IoU
100
- iou_threshold = 0.5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  keep_indices = []
102
  merged_boxes = set()
103
 
 
97
  class_ids = instances.pred_classes.numpy()
98
 
99
  # Filter overlapping boxes using IoU
100
+ iou_threshold = 0.8
101
+ keep_indices = []
102
+ merged_boxes = set()
103
+
104
+ for i in range(len(boxes)):
105
+ if i in merged_boxes:
106
+ continue
107
+ keep_indices.append(i)
108
+ for j in range(i + 1, len(boxes)):
109
+ if j in merged_boxes:
110
+ continue
111
+ iou = box_iou(
112
+ torch.tensor(boxes[i]).unsqueeze(0), torch.tensor(boxes[j]).unsqueeze(0)
113
+ ).item()
114
+ if iou > iou_threshold:
115
+ merged_boxes.add(j)
116
+
117
+ # Calculate total cost based on non-overlapping boxes
118
+ total_cost = 0
119
+ damage_details = []
120
+
121
+ for idx in keep_indices:
122
+ class_id = class_ids[idx]
123
+ damaged_part = (
124
+ class_names[class_id] if class_id < len(class_names) else "unknown"
125
+ )
126
+ if damaged_part not in expected_parts:
127
+ damaged_part = "other"
128
+
129
+ repair_cost = cost_dict.get(damaged_part, cost_dict["other"])
130
+ total_cost += repair_cost
131
+
132
+ damage_details.append({"part": damaged_part, "cost_usd": repair_cost})
133
+
134
+ response = {"damages": damage_details, "total_cost": total_cost}
135
+
136
+ return jsonify(response)
137
+
138
+
139
+ @app.route("/fetch-image", methods=["POST"])
140
+ def fetchImage():
141
+ file = None
142
+ if "url" in request.form:
143
+ url = request.form["url"]
144
+ response = requests.get(url)
145
+ file = io.BytesIO(response.content)
146
+ elif "file" in request.files:
147
+ file = request.files["file"]
148
+
149
+ # Load image
150
+ image = io.imread(file)
151
+ image_np = image
152
+
153
+ # Run model prediction
154
+ outputs = predictor(image_np)
155
+ instances = outputs["instances"].to("cpu")
156
+ class_names = MetadataCatalog.get(cfg.DATASETS.TEST[0]).thing_classes
157
+
158
+ # Extract bounding boxes and class IDs
159
+ boxes = instances.pred_boxes.tensor.numpy()
160
+ class_ids = instances.pred_classes.numpy()
161
+
162
+ # Filter overlapping boxes using IoU
163
+ iou_threshold = 0.8
164
  keep_indices = []
165
  merged_boxes = set()
166