reidddd commited on
Commit
f5c4564
·
1 Parent(s): 96cdb25

updated roboflow model to improve accuracy

Browse files
Files changed (3) hide show
  1. app.py +121 -74
  2. requirements.txt +1 -0
  3. test.ipynb +0 -0
app.py CHANGED
@@ -39,6 +39,8 @@ from PIL import Image
39
  import os
40
  from sklearn.model_selection import train_test_split
41
  from sklearn.preprocessing import MinMaxScaler
 
 
42
  # Initialize Flask app
43
  app = Flask(__name__)
44
 
@@ -138,88 +140,133 @@ def fetchImage():
138
  scene=image, detections=detections_damage
139
  )
140
 
141
- # Create a temporary directory to save outputs
142
- temp_dir = tempfile.mkdtemp()
143
 
144
  # Define a repair cost dictionary (per part)
145
- repair_cost_dict = {
146
- "wheel": 100, # Base cost for wheel
147
- "door": 200, # Base cost for door
148
- "hood": 300, # Base cost for hood
149
- "front_bumper": 250, # Base cost for bumper
150
- "trunk": 200,
151
- "front_glass": 150,
152
- "back_left_door": 200,
153
- "left_mirror": 20,
154
- "back_glass": 150,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  }
156
 
157
- # Initialize total cost
158
  total_cost = 0
159
 
160
- # Ensure coordinate processing is done in chunks of 4
161
- coordinates = list(map(int, detections_damage.xyxy.flatten()))
162
- num_damages = (
163
- len(coordinates) // 4
164
- ) # Each damage has 4 coordinates (x1, y1, x2, y2)
165
 
166
  # Iterate through damages
167
- for i in range(num_damages):
168
- x1, y1, x2, y2 = coordinates[i * 4: (i + 1) * 4]
169
-
170
- # Ensure the coordinates are within image bounds
171
- x1, y1 = max(0, x1), max(0, y1)
172
- x2, y2 = min(image.shape[1], x2), min(image.shape[0], y2)
173
-
174
- # Crop the damaged region
175
- cropped_damage = image[y1:y2, x1:x2]
176
-
177
- # Check if the cropped region is valid
178
- if cropped_damage.size == 0:
179
- print(f"Skipping empty crop for damage region {i + 1}")
180
- continue
181
-
182
- # Save the cropped damaged area
183
- damage_image_path = os.path.join(temp_dir, f"damage_image_{i}.png")
184
- cv2.imwrite(damage_image_path, cropped_damage)
185
-
186
- # Run the parts detection model on the cropped damage
187
- result_parts = model_parts.predict(
188
- damage_image_path, confidence=15).json()
189
- detections_parts = sv.Detections.from_inference(result_parts)
190
-
191
- # Calculate repair cost for each detected part
192
- for part in result_parts["predictions"]:
193
- part_name = part["class"]
194
- damage_area = part["width"] * part["height"]
195
- cropped_area = (x2 - x1) * (y2 - y1)
196
- damage_percentage = (damage_area / cropped_area) * 100
197
-
198
- # Lookup cost and add to total
199
- base_cost = repair_cost_dict.get(
200
- part_name, 0
201
- ) # Default to 0 if part not in dict
202
- repair_cost = (damage_percentage / 100) * 10 * base_cost
203
- total_cost += round(repair_cost, ndigits=1)
204
-
205
- print(
206
- f"Damage {i + 1} - {part_name}: {damage_percentage:.2f}% damaged, Cost: ${repair_cost:.2f}"
207
- )
208
-
209
- # Annotate and save the result
210
- part_annotator = sv.LabelAnnotator()
211
- annotated_parts_image = part_annotator.annotate(
212
- scene=cropped_damage, detections=detections_parts
213
- )
214
- annotated_parts_path = os.path.join(
215
- temp_dir, f"annotated_parts_{i}.png")
216
- cv2.imwrite(annotated_parts_path, annotated_parts_image)
217
-
218
- # Save the overall annotated image
219
- annotated_image_path = os.path.join(temp_dir, "annotated_image_damage.png")
220
- cv2.imwrite(annotated_image_path, annotated_image_damage)
221
-
222
- # Return the total cost in the specified format
 
 
 
 
 
 
 
 
 
 
223
  result = {"total_cost": total_cost}
224
  print(result)
225
 
 
39
  import os
40
  from sklearn.model_selection import train_test_split
41
  from sklearn.preprocessing import MinMaxScaler
42
+ from inference_sdk import InferenceHTTPClient
43
+
44
  # Initialize Flask app
45
  app = Flask(__name__)
46
 
 
140
  scene=image, detections=detections_damage
141
  )
142
 
143
+ # temp_dir = tempfile.mkdtemp()
 
144
 
145
  # Define a repair cost dictionary (per part)
146
+ repair_costs = {
147
+ "Car-Damage-Detection-1KxY": 1000, # General damage assessment cost
148
+ "Bodypanel-Dent": 200,
149
+ "Front-Windscreen-Damage": 400,
150
+ "Headlight-Damage": 250,
151
+ "Rear-windscreen-Damage": 350,
152
+ "RunningBoard-Dent": 150,
153
+ "Sidemirror-Damage": 180,
154
+ "Signlight-Damage": 120,
155
+ "Taillight-Damage": 220,
156
+ "back-bumper": 500,
157
+ "back-glass": 400,
158
+ "bonnet-dent": 300,
159
+ "boot-dent": 350,
160
+ "broken_lamp": 100,
161
+ "crack": 250,
162
+ "damaged-door": 600,
163
+ "damaged-front-bumper": 550,
164
+ "damaged-head-light": 270,
165
+ "damaged-hood": 500,
166
+ "damaged-rear-bumper": 520,
167
+ "damaged-rear-window": 380,
168
+ "damaged-tail-light": 230,
169
+ "damaged-trunk": 600,
170
+ "damaged-window": 280,
171
+ "damaged-windscreen": 450,
172
+ "dent": 200,
173
+ "dent-or-scratch": 180,
174
+ "door": 700,
175
+ "doorouter-dent": 250,
176
+ "fender-dent": 220,
177
+ "flat_tire": 100,
178
+ "front-bumper": 500,
179
+ "front-bumper-dent": 450,
180
+ "front-glass": 400,
181
+ "headlight": 250,
182
+ "hood": 500,
183
+ "mirror": 180,
184
+ "pillar-dent": 220,
185
+ "quaterpanel-dent": 270,
186
+ "rear-bumper-dent": 480,
187
+ "roof-dent": 400,
188
+ "scratch": 150,
189
+ "shattered_glass": 500,
190
+ "taillight": 220,
191
+ "trunk": 600,
192
+ "wheel": 250,
193
+ "window": 300,
194
  }
195
 
 
196
  total_cost = 0
197
 
198
+ # coordinates = list(map(int, detections_damage.xyxy.flatten()))
199
+ # num_damages = (
200
+ # len(coordinates) // 4
201
+ # ) # Each damage has 4 coordinates (x1, y1, x2, y2)
 
202
 
203
  # Iterate through damages
204
+ # for i in range(num_damages):
205
+ # x1, y1, x2, y2 = coordinates[i * 4: (i + 1) * 4]
206
+
207
+ # # Ensure the coordinates are within image bounds
208
+ # x1, y1 = max(0, x1), max(0, y1)
209
+ # x2, y2 = min(image.shape[1], x2), min(image.shape[0], y2)
210
+
211
+ # # Crop the damaged region
212
+ # cropped_damage = image[y1:y2, x1:x2]
213
+
214
+ # # Check if the cropped region is valid
215
+ # if cropped_damage.size == 0:
216
+ # print(f"Skipping empty crop for damage region {i + 1}")
217
+ # continue
218
+
219
+ # # Save the cropped damaged area
220
+ # damage_image_path = os.path.join(temp_dir, f"damage_image_{i}.png")
221
+ # cv2.imwrite(damage_image_path, cropped_damage)
222
+
223
+ # # Run the parts detection model on the cropped damage
224
+ # result_parts = model_parts.predict(
225
+ # damage_image_path, confidence=15).json()
226
+ # detections_parts = sv.Detections.from_inference(result_parts)
227
+
228
+ # # Calculate repair cost for each detected part
229
+ # for part in result_parts["predictions"]:
230
+ # part_name = part["class"]
231
+ # damage_area = part["width"] * part["height"]
232
+ # cropped_area = (x2 - x1) * (y2 - y1)
233
+ # damage_percentage = (damage_area / cropped_area) * 100
234
+
235
+ # # Lookup cost and add to total
236
+ # base_cost = repair_cost_dict.get(
237
+ # part_name, 0
238
+ # ) # Default to 0 if part not in dict
239
+ # repair_cost = (damage_percentage / 100) * 10 * base_cost
240
+ # total_cost += round(repair_cost, ndigits=1)
241
+
242
+ # print(
243
+ # f"Damage {i + 1} - {part_name}: {damage_percentage:.2f}% damaged, Cost: ${repair_cost:.2f}"
244
+ # )
245
+
246
+ # # Annotate and save the result
247
+ # part_annotator = sv.LabelAnnotator()
248
+ # annotated_parts_image = part_annotator.annotate(
249
+ # scene=cropped_damage, detections=detections_parts
250
+ # )
251
+ # annotated_parts_path = os.path.join(
252
+ # temp_dir, f"annotated_parts_{i}.png")
253
+ # cv2.imwrite(annotated_parts_path, annotated_parts_image)
254
+
255
+ # # Save the overall annotated image
256
+ # annotated_image_path = os.path.join(temp_dir, "annotated_image_damage.png")
257
+ # cv2.imwrite(annotated_image_path, annotated_image_damage)
258
+
259
+ # # Return the total cost in the specified format
260
+
261
+ CLIENT = InferenceHTTPClient(
262
+ api_url="https://detect.roboflow.com",
263
+ api_key="LqD8Cs4OsoK8seO3CPkf"
264
+ )
265
+ result = CLIENT.infer(file_name, model_id="car-damage-detection-krsix/1")
266
+ labels = [item["class"] for item in result["predictions"]]
267
+ print(labels)
268
+ for class_ in labels:
269
+ total_cost += repair_costs.get(class_, 0)
270
  result = {"total_cost": total_cost}
271
  print(result)
272
 
requirements.txt CHANGED
@@ -19,6 +19,7 @@ opencv-python
19
  requests
20
  PyMuPDF
21
  openai
 
22
  fpdf
23
  cloudinary
24
  PyPDF2
 
19
  requests
20
  PyMuPDF
21
  openai
22
+ inference-sdk
23
  fpdf
24
  cloudinary
25
  PyPDF2
test.ipynb ADDED
The diff for this file is too large to render. See raw diff