Spaces:
Runtime error
Runtime error
update model file and improved accuracy
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ import io
|
|
9 |
import os
|
10 |
import requests
|
11 |
import gdown
|
|
|
12 |
|
13 |
# Initialize Flask app
|
14 |
app = Flask(__name__)
|
@@ -16,23 +17,29 @@ cfg = None
|
|
16 |
# Google Drive file URL
|
17 |
GDRIVE_MODEL_URL = "https://drive.google.com/uc?id=18aEDo-kWOBhg8mAhnbpFkuM6bmmrBH4E" # Replace 'your-file-id' with the actual file ID from Google Drive
|
18 |
LOCAL_MODEL_PATH = "model_final.pth"
|
|
|
|
|
19 |
def download_file_from_google_drive(id, destination):
|
20 |
gdown.download(GDRIVE_MODEL_URL, LOCAL_MODEL_PATH, quiet=False)
|
21 |
|
22 |
-
|
23 |
-
file_id =
|
24 |
-
destination =
|
25 |
download_file_from_google_drive(file_id, destination)
|
26 |
|
|
|
27 |
# Download model from Google Drive if not already present locally
|
28 |
def download_model():
|
29 |
if not os.path.exists(LOCAL_MODEL_PATH):
|
30 |
response = requests.get(GDRIVE_MODEL_URL, stream=True)
|
31 |
if response.status_code == 200:
|
32 |
-
with open(LOCAL_MODEL_PATH,
|
33 |
f.write(response.content)
|
34 |
else:
|
35 |
-
raise Exception(
|
|
|
|
|
|
|
36 |
|
37 |
# Configuration and model setup
|
38 |
def setup_model(model_path):
|
@@ -44,36 +51,39 @@ def setup_model(model_path):
|
|
44 |
cfg.MODEL.DEVICE = "cpu" # Use "cuda" for GPU
|
45 |
return DefaultPredictor(cfg)
|
46 |
|
|
|
47 |
# Ensure model is available
|
48 |
predictor = setup_model(LOCAL_MODEL_PATH)
|
49 |
|
50 |
# Define expected parts and costs
|
51 |
-
expected_parts = [
|
52 |
cost_dict = {
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
}
|
60 |
|
61 |
-
|
|
|
62 |
def home():
|
63 |
-
return render_template(
|
64 |
|
65 |
-
|
|
|
66 |
def upload():
|
67 |
-
if
|
68 |
return jsonify({"error": "No file uploaded"}), 400
|
69 |
|
70 |
-
file = request.files[
|
71 |
-
if file.filename ==
|
72 |
return jsonify({"error": "No file selected"}), 400
|
73 |
|
74 |
# Load image
|
75 |
-
image =
|
76 |
-
image_np =
|
77 |
|
78 |
# Run model prediction
|
79 |
outputs = predictor(image_np)
|
@@ -86,24 +96,21 @@ def upload():
|
|
86 |
|
87 |
for j in range(len(instances)):
|
88 |
class_id = instances.pred_classes[j].item()
|
89 |
-
damaged_part =
|
|
|
|
|
90 |
if damaged_part not in expected_parts:
|
91 |
-
damaged_part =
|
92 |
|
93 |
-
repair_cost = cost_dict.get(damaged_part, cost_dict[
|
94 |
total_cost += repair_cost
|
95 |
|
96 |
-
damage_details.append({
|
97 |
-
'part': damaged_part,
|
98 |
-
'cost_usd': repair_cost
|
99 |
-
})
|
100 |
|
101 |
-
response = {
|
102 |
-
"damages": damage_details,
|
103 |
-
"total_cost": total_cost
|
104 |
-
}
|
105 |
|
106 |
return jsonify(response)
|
107 |
|
108 |
-
|
109 |
-
|
|
|
|
9 |
import os
|
10 |
import requests
|
11 |
import gdown
|
12 |
+
from skimage import io
|
13 |
|
14 |
# Initialize Flask app
|
15 |
app = Flask(__name__)
|
|
|
17 |
# Google Drive file URL
|
18 |
GDRIVE_MODEL_URL = "https://drive.google.com/uc?id=18aEDo-kWOBhg8mAhnbpFkuM6bmmrBH4E" # Replace 'your-file-id' with the actual file ID from Google Drive
|
19 |
LOCAL_MODEL_PATH = "model_final.pth"
|
20 |
+
|
21 |
+
|
22 |
def download_file_from_google_drive(id, destination):
|
23 |
gdown.download(GDRIVE_MODEL_URL, LOCAL_MODEL_PATH, quiet=False)
|
24 |
|
25 |
+
|
26 |
+
file_id = "18aEDo-kWOBhg8mAhnbpFkuM6bmmrBH4E"
|
27 |
+
destination = "model_final.pth"
|
28 |
download_file_from_google_drive(file_id, destination)
|
29 |
|
30 |
+
|
31 |
# Download model from Google Drive if not already present locally
|
32 |
def download_model():
|
33 |
if not os.path.exists(LOCAL_MODEL_PATH):
|
34 |
response = requests.get(GDRIVE_MODEL_URL, stream=True)
|
35 |
if response.status_code == 200:
|
36 |
+
with open(LOCAL_MODEL_PATH, "wb") as f:
|
37 |
f.write(response.content)
|
38 |
else:
|
39 |
+
raise Exception(
|
40 |
+
f"Failed to download model from Google Drive: {response.status_code}"
|
41 |
+
)
|
42 |
+
|
43 |
|
44 |
# Configuration and model setup
|
45 |
def setup_model(model_path):
|
|
|
51 |
cfg.MODEL.DEVICE = "cpu" # Use "cuda" for GPU
|
52 |
return DefaultPredictor(cfg)
|
53 |
|
54 |
+
|
55 |
# Ensure model is available
|
56 |
predictor = setup_model(LOCAL_MODEL_PATH)
|
57 |
|
58 |
# Define expected parts and costs
|
59 |
+
expected_parts = ["headlamp", "rear_bumper", "door", "hood", "front_bumper"]
|
60 |
cost_dict = {
|
61 |
+
"headlamp": 300,
|
62 |
+
"rear_bumper": 250,
|
63 |
+
"door": 200,
|
64 |
+
"hood": 220,
|
65 |
+
"front_bumper": 250,
|
66 |
+
"other": 150,
|
67 |
}
|
68 |
|
69 |
+
|
70 |
+
@app.route("/")
|
71 |
def home():
|
72 |
+
return render_template("index.html")
|
73 |
|
74 |
+
|
75 |
+
@app.route("/upload", methods=["POST"])
|
76 |
def upload():
|
77 |
+
if "file" not in request.files:
|
78 |
return jsonify({"error": "No file uploaded"}), 400
|
79 |
|
80 |
+
file = request.files["file"]
|
81 |
+
if file.filename == "":
|
82 |
return jsonify({"error": "No file selected"}), 400
|
83 |
|
84 |
# Load image
|
85 |
+
image = io.imread(file)
|
86 |
+
image_np = image
|
87 |
|
88 |
# Run model prediction
|
89 |
outputs = predictor(image_np)
|
|
|
96 |
|
97 |
for j in range(len(instances)):
|
98 |
class_id = instances.pred_classes[j].item()
|
99 |
+
damaged_part = (
|
100 |
+
class_names[class_id] if class_id < len(class_names) else "unknown"
|
101 |
+
)
|
102 |
if damaged_part not in expected_parts:
|
103 |
+
damaged_part = "other"
|
104 |
|
105 |
+
repair_cost = cost_dict.get(damaged_part, cost_dict["other"])
|
106 |
total_cost += repair_cost
|
107 |
|
108 |
+
damage_details.append({"part": damaged_part, "cost_usd": repair_cost})
|
|
|
|
|
|
|
109 |
|
110 |
+
response = {"damages": damage_details, "total_cost": total_cost}
|
|
|
|
|
|
|
111 |
|
112 |
return jsonify(response)
|
113 |
|
114 |
+
|
115 |
+
if __name__ == "__main__":
|
116 |
+
app.run(host="0.0.0.0", port=7860)
|