Spaces:
Runtime error
Runtime error
File size: 12,350 Bytes
7b813cc 14d234f bcc0393 14d234f 75c33bc a39b793 75c33bc a64384c f5fe239 76ba6f0 848becd f5c4564 7b813cc df285c2 bcc0393 f5fe239 0a796a0 bcc0393 f5fe239 bcc0393 f5fe239 bcc0393 e67e1b9 7b813cc e67e1b9 7b813cc e67e1b9 93224ed ad9f29a 93224ed 281f6eb 14d234f 93224ed 14d234f 75c33bc 14d234f f5c4564 14d234f f5c4564 14d234f 93224ed f5c4564 93224ed 14d234f f5c4564 1a0c2a2 c479e13 848becd 1a0c2a2 f5c4564 14d234f 7b813cc 14d234f 7b813cc e67e1b9 75c33bc 2b64a89 75c33bc 1712d72 75c33bc 1712d72 75c33bc bcc0393 357c490 836fb68 bcc0393 f5fe239 bcc0393 0a796a0 bcc0393 3f0c1ee 95e893d 3f0c1ee f47808e dddca60 76ba6f0 4676214 466ca4d b98361b 4676214 466ca4d 4676214 e88283e f5fe239 dddca60 f5fe239 466ca4d f5fe239 466ca4d f5fe239 bcc0393 e67e1b9 |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
from flask import Flask, request, jsonify, render_template
from PIL import Image
import io
import os
import requests
from roboflow import Roboflow
import supervision as sv
import cv2
import tempfile
import gdown
import os
import requests
import requests
import cloudinary
import model
import cloudinary.uploader
from a import main
import numpy as np
import torchvision.transforms as transforms
import pandas as pd
import nibabel as nib
import numpy as np
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import cv2
from PIL import Image
from sklearn.model_selection import train_test_split
import os
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.models as models
from torch.utils.data import Dataset, DataLoader
import pandas as pd
from PIL import Image
import os
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from inference_sdk import InferenceHTTPClient, InferenceConfiguration
# Initialize Flask app
app = Flask(__name__)
GDRIVE_MODEL_URL = "https://drive.google.com/uc?id=1fzKneepaRt_--dzamTcDBM-9d3_dLX7z"
LOCAL_MODEL_PATH = "checkpoint32.pth"
d = "https://drive.google.com/uc?id=1GfrlFNoa7E4liMHyMuF73nA21yT9SNSb"
def download_file_from_google_drive():
gdown.download(GDRIVE_MODEL_URL, LOCAL_MODEL_PATH, quiet=False)
da = "a.pth"
def download_file_from_google_drived():
gdown.download(d, da, quiet=False)
def download_model():
if not os.path.exists(LOCAL_MODEL_PATH):
response = requests.get(GDRIVE_MODEL_URL, stream=True)
if response.status_code == 200:
with open(LOCAL_MODEL_PATH, "wb") as f:
f.write(response.content)
else:
raise Exception(
f"Failed to download model from Google Drive: {response.status_code}"
)
download_file_from_google_drive()
download_file_from_google_drived()
@app.route("/")
def home():
return render_template("index.html")
@app.route("/fetch-image", methods=["POST"])
def fetchImage():
file = None
url = ""
if "url" in request.form:
url = request.form["url"]
response = requests.get(url)
file = io.BytesIO(response.content)
elif "file" in request.files:
file = request.files["file"]
# 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"
# File name for saving
file_name = "downloaded_image.jpg"
# Download the image
response = requests.get(url)
# Save the image to the current directory
if response.status_code == 200:
file_name = "downloaded_image.jpg"
image = Image.open(io.BytesIO(response.content))
if image.mode == "RGBA":
image = image.convert("RGB")
image.save(file_name, "JPEG", quality=100)
print(f"Image downloaded and saved as {file_name}")
else:
print(f"Failed to download image. Status code: {response.status_code}")
image = cv2.imread(file_name)
rf = Roboflow(api_key="LqD8Cs4OsoK8seO3CPkf")
project_parts = rf.workspace().project("car-parts-segmentation")
model_parts = project_parts.version(2).model
project_damage = rf.workspace().project("car-damage-detection-ha5mm")
model_damage = project_damage.version(1).model
# Run the damage detection model
result_damage = model_damage.predict(
file_name,
confidence=40,
).json()
# Extract detections from the result
detections_damage = sv.Detections.from_inference(result_damage)
# Read the input image
# Annotate damaged areas of the car
mask_annotator = sv.MaskAnnotator()
annotated_image_damage = mask_annotator.annotate(
scene=image, detections=detections_damage
)
# temp_dir = tempfile.mkdtemp()
# Define a repair cost dictionary (per part)
repair_costs = {
"Car-Damage-Detection-1KxY": 1000, # General damage assessment cost
"Bodypanel-Dent": 200,
"Front-Windscreen-Damage": 400,
"Headlight-Damage": 250,
"Rear-windscreen-Damage": 350,
"RunningBoard-Dent": 150,
"Sidemirror-Damage": 180,
"Signlight-Damage": 120,
"Taillight-Damage": 220,
"back-bumper": 500,
"back-glass": 400,
"bonnet-dent": 300,
"boot-dent": 350,
"broken_lamp": 100,
"crack": 250,
"damaged-door": 600,
"damaged-front-bumper": 550,
"damaged-head-light": 270,
"damaged-hood": 500,
"damaged-rear-bumper": 520,
"damaged-rear-window": 380,
"damaged-tail-light": 230,
"damaged-trunk": 600,
"damaged-window": 280,
"damaged-windscreen": 450,
"dent": 200,
"dent-or-scratch": 180,
"door": 700,
"doorouter-dent": 250,
"fender-dent": 220,
"flat_tire": 100,
"front-bumper": 500,
"front-bumper-dent": 450,
"front-glass": 400,
"headlight": 250,
"hood": 500,
"mirror": 180,
"pillar-dent": 220,
"quaterpanel-dent": 270,
"rear-bumper-dent": 480,
"roof-dent": 400,
"scratch": 150,
"shattered_glass": 500,
"taillight": 220,
"trunk": 600,
"wheel": 250,
"window": 300,
}
total_cost = 0
# coordinates = list(map(int, detections_damage.xyxy.flatten()))
# num_damages = (
# len(coordinates) // 4
# ) # Each damage has 4 coordinates (x1, y1, x2, y2)
# Iterate through damages
# for i in range(num_damages):
# x1, y1, x2, y2 = coordinates[i * 4: (i + 1) * 4]
# # Ensure the coordinates are within image bounds
# x1, y1 = max(0, x1), max(0, y1)
# x2, y2 = min(image.shape[1], x2), min(image.shape[0], y2)
# # Crop the damaged region
# cropped_damage = image[y1:y2, x1:x2]
# # Check if the cropped region is valid
# if cropped_damage.size == 0:
# print(f"Skipping empty crop for damage region {i + 1}")
# continue
# # Save the cropped damaged area
# damage_image_path = os.path.join(temp_dir, f"damage_image_{i}.png")
# cv2.imwrite(damage_image_path, cropped_damage)
# # Run the parts detection model on the cropped damage
# result_parts = model_parts.predict(
# damage_image_path, confidence=15).json()
# detections_parts = sv.Detections.from_inference(result_parts)
# # Calculate repair cost for each detected part
# for part in result_parts["predictions"]:
# part_name = part["class"]
# damage_area = part["width"] * part["height"]
# cropped_area = (x2 - x1) * (y2 - y1)
# damage_percentage = (damage_area / cropped_area) * 100
# # Lookup cost and add to total
# base_cost = repair_cost_dict.get(
# part_name, 0
# ) # Default to 0 if part not in dict
# repair_cost = (damage_percentage / 100) * 10 * base_cost
# total_cost += round(repair_cost, ndigits=1)
# print(
# f"Damage {i + 1} - {part_name}: {damage_percentage:.2f}% damaged, Cost: ${repair_cost:.2f}"
# )
# # Annotate and save the result
# part_annotator = sv.LabelAnnotator()
# annotated_parts_image = part_annotator.annotate(
# scene=cropped_damage, detections=detections_parts
# )
# annotated_parts_path = os.path.join(
# temp_dir, f"annotated_parts_{i}.png")
# cv2.imwrite(annotated_parts_path, annotated_parts_image)
# # Save the overall annotated image
# annotated_image_path = os.path.join(temp_dir, "annotated_image_damage.png")
# cv2.imwrite(annotated_image_path, annotated_image_damage)
# # Return the total cost in the specified format
CLIENT = InferenceHTTPClient(
api_url="https://detect.roboflow.com",
api_key="LqD8Cs4OsoK8seO3CPkf"
)
print(file_name)
# Set confidence threshold to 50%
custom_configuration = InferenceConfiguration(confidence_threshold=0.8)
with CLIENT.use_configuration(custom_configuration):
result = CLIENT.infer(
file_name, model_id="car-damage-detection-krsix/1")
print(result)
threshold = 0.3 # Adjust this as needed
filtered_labels = [item["class"]
for item in result["predictions"] if item["confidence"] >= threshold]
print(filtered_labels)
labels = filtered_labels
print(labels)
for class_ in labels:
total_cost += repair_costs.get(class_, 0)
result = {"total_cost": total_cost}
print(result)
return jsonify(result)
@app.route("/generate-report", methods=["POST"])
def generate_report():
file = None
if "report_url" in request.form:
report_url = request.form["report_url"]
insurance_url = request.form["insurance_url"]
url = main(report_url, insurance_url, "output.pdf")
result = {"url": url}
return jsonify(result), 200
elif "file" in request.files:
file = request.files["file"]
with open("uploaded_report.pdf", "wb") as f:
f.write(file.read())
return jsonify({"message": "Something happened!."}), 404
@app.route("/ms-detection", methods=["POST"])
def predict():
file = request.files["file"]
if not file:
return jsonify({"error": "file not uploaded"}), 400
# Save file temporarily
temp_path = os.path.join(tempfile.gettempdir(), file.filename)
file.save(temp_path)
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
])
if file.filename.lower().endswith((".png", ".jpg", ".jpeg")):
image = Image.open(temp_path)
image_save_path = os.path.join(
tempfile.gettempdir(), file.filename.lower())
image.save(image_save_path)
def is_mri_image(image_path):
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
if img is None:
return False # Invalid image
# Apply Canny edge detection
edges = cv2.Canny(img, 50, 150)
# Calculate edge density (MRI images have high edge presence)
edge_density = np.sum(edges > 0) / edges.size
print(edge_density)
return edge_density > 0.05
if (is_mri_image(temp_path)):
return jsonify({"message": "Not an mri image", "confidence": 0.95, "saved_path": image_save_path})
a, b = model.check_file(temp_path)
class ResNetRegression(nn.Module):
def __init__(self):
super(ResNetRegression, self).__init__()
self.model = models.resnet34(pretrained=True)
in_features = self.model.fc.in_features
# Change output layer for regression
self.model.fc = nn.Linear(in_features, 1)
def forward(self, x):
return self.model(x)
# Initialize Model, Loss, and Optimizer
model_new = ResNetRegression()
checkpoint = torch.load(
"/home/user/app/a.pth", weights_only=False, map_location=torch.device('cpu'))
def remove_module_from_checkpoint(checkpoint):
new_state_dict = {}
print(checkpoint.keys())
for key, value in checkpoint.items():
new_key = key.replace("module.", "")
new_state_dict[new_key] = value
checkpoint = new_state_dict
return checkpoint
checkpoint = remove_module_from_checkpoint(checkpoint)
model_new.load_state_dict(checkpoint)
image = Image.open(temp_path).convert("RGB")
output = model_new(transform(image).unsqueeze(0))
stage = output.item()
if not a == "No ms detected":
if stage <= 2.0:
stage = "Mild"
elif stage >= 2.0 and stage <= 3.2:
stage = "Moderate"
else:
stage = "Severe"
else:
stage = "No ms detected"
return jsonify({"message": a, "confidence": b, "stage": stage, "saved_path": image_save_path})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)
|