Spaces:
Runtime error
Runtime error
updated roboflow model to improve accuracy
Browse files
app.py
CHANGED
@@ -42,7 +42,6 @@ from sklearn.model_selection import train_test_split
|
|
42 |
from sklearn.preprocessing import MinMaxScaler
|
43 |
from inference_sdk import InferenceHTTPClient, InferenceConfiguration
|
44 |
|
45 |
-
# Initialize Flask app
|
46 |
app = Flask(__name__)
|
47 |
|
48 |
GDRIVE_MODEL_URL = "https://drive.google.com/uc?id=1fzKneepaRt_--dzamTcDBM-9d3_dLX7z"
|
@@ -92,15 +91,11 @@ def fetchImage():
|
|
92 |
file = io.BytesIO(response.content)
|
93 |
elif "file" in request.files:
|
94 |
file = request.files["file"]
|
95 |
-
# url = "https://firebasestorage.googleapis.com/v0/b/car-damage-detector-s34rrz.firebasestorage.app/o/users%2FYMd99dt33HaktTWpYp5MM5oYeBE3%2Fuploads%2F1737454072124000.jpg?alt=media&token=9eae79fa-4c06-41a5-9f58-236c39efaac0"
|
96 |
|
97 |
-
# File name for saving
|
98 |
file_name = f"downloaded_image_{random.randint(1,50)}.jpg"
|
99 |
|
100 |
-
# Download the image
|
101 |
response = requests.get(url)
|
102 |
|
103 |
-
# Save the image to the current directory
|
104 |
if response.status_code == 200:
|
105 |
image = Image.open(io.BytesIO(response.content))
|
106 |
|
@@ -113,120 +108,28 @@ def fetchImage():
|
|
113 |
print(f"Failed to download image. Status code: {response.status_code}")
|
114 |
image = cv2.imread(file_name)
|
115 |
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
).json()
|
129 |
-
|
130 |
-
# Extract detections from the result
|
131 |
-
detections_damage = sv.Detections.from_inference(result_damage)
|
132 |
-
|
133 |
-
# Read the input image
|
134 |
-
|
135 |
-
# Annotate damaged areas of the car
|
136 |
-
mask_annotator = sv.MaskAnnotator()
|
137 |
-
annotated_image_damage = mask_annotator.annotate(
|
138 |
-
scene=image, detections=detections_damage
|
139 |
-
)
|
140 |
-
|
141 |
-
# temp_dir = tempfile.mkdtemp()
|
142 |
-
|
143 |
-
# Define a repair cost dictionary (per part)
|
144 |
-
repair_costs = {
|
145 |
-
"damage-front-windscreen": 500,
|
146 |
-
"damaged-door": 550,
|
147 |
-
"damaged-fender": 450,
|
148 |
-
"damaged-front-bumper": 440,
|
149 |
-
"damaged-head-light": 480,
|
150 |
-
"damaged-hood": 490,
|
151 |
-
"damaged-rear-bumper": 510,
|
152 |
-
"damaged-rear-window": 505,
|
153 |
-
"damaged-side-window": 501,
|
154 |
-
"damaged-tail-light": 478,
|
155 |
-
"quaterpanel-dent": 520
|
156 |
}
|
157 |
|
158 |
total_cost = 0
|
159 |
|
160 |
-
# coordinates = list(map(int, detections_damage.xyxy.flatten()))
|
161 |
-
# num_damages = (
|
162 |
-
# len(coordinates) // 4
|
163 |
-
# ) # Each damage has 4 coordinates (x1, y1, x2, y2)
|
164 |
-
|
165 |
-
# Iterate through damages
|
166 |
-
# for i in range(num_damages):
|
167 |
-
# x1, y1, x2, y2 = coordinates[i * 4: (i + 1) * 4]
|
168 |
-
|
169 |
-
# # Ensure the coordinates are within image bounds
|
170 |
-
# x1, y1 = max(0, x1), max(0, y1)
|
171 |
-
# x2, y2 = min(image.shape[1], x2), min(image.shape[0], y2)
|
172 |
-
|
173 |
-
# # Crop the damaged region
|
174 |
-
# cropped_damage = image[y1:y2, x1:x2]
|
175 |
-
|
176 |
-
# # Check if the cropped region is valid
|
177 |
-
# if cropped_damage.size == 0:
|
178 |
-
# print(f"Skipping empty crop for damage region {i + 1}")
|
179 |
-
# continue
|
180 |
-
|
181 |
-
# # Save the cropped damaged area
|
182 |
-
# damage_image_path = os.path.join(temp_dir, f"damage_image_{i}.png")
|
183 |
-
# cv2.imwrite(damage_image_path, cropped_damage)
|
184 |
-
|
185 |
-
# # Run the parts detection model on the cropped damage
|
186 |
-
# result_parts = model_parts.predict(
|
187 |
-
# damage_image_path, confidence=15).json()
|
188 |
-
# detections_parts = sv.Detections.from_inference(result_parts)
|
189 |
-
|
190 |
-
# # Calculate repair cost for each detected part
|
191 |
-
# for part in result_parts["predictions"]:
|
192 |
-
# part_name = part["class"]
|
193 |
-
# damage_area = part["width"] * part["height"]
|
194 |
-
# cropped_area = (x2 - x1) * (y2 - y1)
|
195 |
-
# damage_percentage = (damage_area / cropped_area) * 100
|
196 |
-
|
197 |
-
# # Lookup cost and add to total
|
198 |
-
# base_cost = repair_cost_dict.get(
|
199 |
-
# part_name, 0
|
200 |
-
# ) # Default to 0 if part not in dict
|
201 |
-
# repair_cost = (damage_percentage / 100) * 10 * base_cost
|
202 |
-
# total_cost += round(repair_cost, ndigits=1)
|
203 |
-
|
204 |
-
# print(
|
205 |
-
# f"Damage {i + 1} - {part_name}: {damage_percentage:.2f}% damaged, Cost: ${repair_cost:.2f}"
|
206 |
-
# )
|
207 |
-
|
208 |
-
# # Annotate and save the result
|
209 |
-
# part_annotator = sv.LabelAnnotator()
|
210 |
-
# annotated_parts_image = part_annotator.annotate(
|
211 |
-
# scene=cropped_damage, detections=detections_parts
|
212 |
-
# )
|
213 |
-
# annotated_parts_path = os.path.join(
|
214 |
-
# temp_dir, f"annotated_parts_{i}.png")
|
215 |
-
# cv2.imwrite(annotated_parts_path, annotated_parts_image)
|
216 |
-
|
217 |
-
# # Save the overall annotated image
|
218 |
-
# annotated_image_path = os.path.join(temp_dir, "annotated_image_damage.png")
|
219 |
-
# cv2.imwrite(annotated_image_path, annotated_image_damage)
|
220 |
-
|
221 |
-
# # Return the total cost in the specified format
|
222 |
-
|
223 |
CLIENT = InferenceHTTPClient(
|
224 |
api_url="https://detect.roboflow.com",
|
225 |
api_key="LqD8Cs4OsoK8seO3CPkf"
|
226 |
)
|
227 |
print(file_name)
|
228 |
|
229 |
-
# Set confidence threshold to 50%
|
230 |
custom_configuration = InferenceConfiguration(confidence_threshold=0.7)
|
231 |
with CLIENT.use_configuration(custom_configuration):
|
232 |
result = CLIENT.infer(
|
|
|
42 |
from sklearn.preprocessing import MinMaxScaler
|
43 |
from inference_sdk import InferenceHTTPClient, InferenceConfiguration
|
44 |
|
|
|
45 |
app = Flask(__name__)
|
46 |
|
47 |
GDRIVE_MODEL_URL = "https://drive.google.com/uc?id=1fzKneepaRt_--dzamTcDBM-9d3_dLX7z"
|
|
|
91 |
file = io.BytesIO(response.content)
|
92 |
elif "file" in request.files:
|
93 |
file = request.files["file"]
|
|
|
94 |
|
|
|
95 |
file_name = f"downloaded_image_{random.randint(1,50)}.jpg"
|
96 |
|
|
|
97 |
response = requests.get(url)
|
98 |
|
|
|
99 |
if response.status_code == 200:
|
100 |
image = Image.open(io.BytesIO(response.content))
|
101 |
|
|
|
108 |
print(f"Failed to download image. Status code: {response.status_code}")
|
109 |
image = cv2.imread(file_name)
|
110 |
|
111 |
+
repair_costs_inr = {
|
112 |
+
"damage-front-windscreen": 1200, # Includes glass replacement and labor
|
113 |
+
"damaged-door": 1500, # Includes denting, painting, and panel replacement if needed
|
114 |
+
"damaged-fender": 900, # Includes denting, painting, and possible part replacement
|
115 |
+
"damaged-front-bumper": 1100, # Includes bumper replacement and painting
|
116 |
+
"damaged-head-light": 700, # Headlight unit replacement
|
117 |
+
"damaged-hood": 1400, # Hood denting, painting, or full replacement
|
118 |
+
"damaged-rear-bumper": 1000, # Bumper replacement and painting
|
119 |
+
"damaged-rear-window": 1100, # Rear windshield glass replacement
|
120 |
+
"damaged-side-window": 800, # Side window glass replacement
|
121 |
+
"damaged-tail-light": 600, # Tail light unit replacement
|
122 |
+
"quarterpanel-dent": 1200 # Dent removal and repainting
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
}
|
124 |
|
125 |
total_cost = 0
|
126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
CLIENT = InferenceHTTPClient(
|
128 |
api_url="https://detect.roboflow.com",
|
129 |
api_key="LqD8Cs4OsoK8seO3CPkf"
|
130 |
)
|
131 |
print(file_name)
|
132 |
|
|
|
133 |
custom_configuration = InferenceConfiguration(confidence_threshold=0.7)
|
134 |
with CLIENT.use_configuration(custom_configuration):
|
135 |
result = CLIENT.infer(
|