Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,15 +2,16 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
|
|
5 |
from transformers import SegformerForSemanticSegmentation, AutoFeatureExtractor
|
6 |
import cv2
|
7 |
import json
|
8 |
-
import random
|
9 |
|
10 |
# Load models
|
11 |
part_seg_model = SegformerForSemanticSegmentation.from_pretrained("Mohaddz/huggingCars")
|
12 |
damage_seg_model = SegformerForSemanticSegmentation.from_pretrained("Mohaddz/DamageSeg")
|
13 |
feature_extractor = AutoFeatureExtractor.from_pretrained("Mohaddz/huggingCars")
|
|
|
14 |
|
15 |
# Load parts list
|
16 |
with open('cars117.json', 'r', encoding='utf-8') as f:
|
@@ -49,15 +50,18 @@ def process_image(image):
|
|
49 |
part_heatmap = create_heatmap(part_features)
|
50 |
part_heatmap_resized = cv2.resize(part_heatmap, (image.size[0], image.size[1]))
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
55 |
predicted_parts.sort(key=lambda x: x[1], reverse=True)
|
56 |
|
57 |
return (Image.fromarray(annotated_image),
|
58 |
Image.fromarray(damage_heatmap_resized),
|
59 |
Image.fromarray(part_heatmap_resized),
|
60 |
-
"\n".join([f"{part}: {prob:.2f}" for part, prob in predicted_parts]))
|
61 |
|
62 |
def create_heatmap(features):
|
63 |
heatmap = np.sum(features, axis=0)
|
@@ -72,10 +76,10 @@ iface = gr.Interface(
|
|
72 |
gr.Image(type="pil", label="Annotated Damage"),
|
73 |
gr.Image(type="pil", label="Damage Heatmap"),
|
74 |
gr.Image(type="pil", label="Part Segmentation Heatmap"),
|
75 |
-
gr.Textbox(label="Predicted Parts to Replace
|
76 |
],
|
77 |
-
title="Car Damage Assessment
|
78 |
-
description="Upload an image of a damaged car to get
|
79 |
)
|
80 |
|
81 |
iface.launch()
|
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
+
import tensorflow as tf
|
6 |
from transformers import SegformerForSemanticSegmentation, AutoFeatureExtractor
|
7 |
import cv2
|
8 |
import json
|
|
|
9 |
|
10 |
# Load models
|
11 |
part_seg_model = SegformerForSemanticSegmentation.from_pretrained("Mohaddz/huggingCars")
|
12 |
damage_seg_model = SegformerForSemanticSegmentation.from_pretrained("Mohaddz/DamageSeg")
|
13 |
feature_extractor = AutoFeatureExtractor.from_pretrained("Mohaddz/huggingCars")
|
14 |
+
dl_model = tf.keras.models.load_model('improved_car_damage_prediction_model.h5')
|
15 |
|
16 |
# Load parts list
|
17 |
with open('cars117.json', 'r', encoding='utf-8') as f:
|
|
|
50 |
part_heatmap = create_heatmap(part_features)
|
51 |
part_heatmap_resized = cv2.resize(part_heatmap, (image.size[0], image.size[1]))
|
52 |
|
53 |
+
# Prepare input for damage prediction model
|
54 |
+
input_vector = np.concatenate([part_features.mean(axis=(1, 2)), damage_features.mean(axis=(1, 2))])
|
55 |
+
|
56 |
+
# Predict parts to replace using the loaded model
|
57 |
+
prediction = dl_model.predict(np.array([input_vector]))
|
58 |
+
predicted_parts = [(all_parts[i], float(prob)) for i, prob in enumerate(prediction[0]) if prob > 0.1]
|
59 |
predicted_parts.sort(key=lambda x: x[1], reverse=True)
|
60 |
|
61 |
return (Image.fromarray(annotated_image),
|
62 |
Image.fromarray(damage_heatmap_resized),
|
63 |
Image.fromarray(part_heatmap_resized),
|
64 |
+
"\n".join([f"{part}: {prob:.2f}" for part, prob in predicted_parts[:5]]))
|
65 |
|
66 |
def create_heatmap(features):
|
67 |
heatmap = np.sum(features, axis=0)
|
|
|
76 |
gr.Image(type="pil", label="Annotated Damage"),
|
77 |
gr.Image(type="pil", label="Damage Heatmap"),
|
78 |
gr.Image(type="pil", label="Part Segmentation Heatmap"),
|
79 |
+
gr.Textbox(label="Predicted Parts to Replace")
|
80 |
],
|
81 |
+
title="Car Damage Assessment",
|
82 |
+
description="Upload an image of a damaged car to get an assessment."
|
83 |
)
|
84 |
|
85 |
iface.launch()
|