Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,13 +40,8 @@ np.random.seed(SEED)
|
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
-
# Get the current working directory
|
| 44 |
-
current_dir = os.path.dirname(__file__)
|
| 45 |
-
# Construct the path to the CSV file
|
| 46 |
-
file_path = os.path.join(current_dir, 'morphs.csv')
|
| 47 |
|
| 48 |
-
|
| 49 |
-
df = pd.read_csv(file_path)
|
| 50 |
|
| 51 |
morphs = {}
|
| 52 |
for _, row in df.iterrows():
|
|
@@ -100,6 +95,15 @@ def overlay_heatmap_on_image(heatmap, image, alpha=0.4, colormap=cv2.COLORMAP_JE
|
|
| 100 |
overlayed_img = cv2.addWeighted(image, alpha, heatmap, 1 - alpha, 0)
|
| 101 |
return overlayed_img
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# Function to segment image
|
| 105 |
def segment_image(image):
|
|
@@ -141,7 +145,6 @@ def segment_image(image):
|
|
| 141 |
|
| 142 |
# Preprocess the cropped car image for the model
|
| 143 |
transform = transforms.Compose([
|
| 144 |
-
transforms.Resize(256),
|
| 145 |
transforms.Resize((224, 224)),
|
| 146 |
transforms.ToTensor(),
|
| 147 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
|
@@ -205,26 +208,26 @@ def segment_image(image):
|
|
| 205 |
modernity_scores = calculate_modernity_scores(modernity_output, year_categories).item()
|
| 206 |
|
| 207 |
|
| 208 |
-
|
| 209 |
|
| 210 |
-
|
| 211 |
-
|
| 212 |
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
|
| 217 |
-
#
|
| 218 |
-
|
| 219 |
-
|
| 220 |
|
| 221 |
-
#
|
| 222 |
-
|
| 223 |
-
|
| 224 |
|
| 225 |
-
#
|
| 226 |
-
|
| 227 |
-
|
| 228 |
|
| 229 |
return "Automobiles detected in the image", cropped_car_pil, modernity_scores, typicality_scores , most_similar_group, overlayed_img_modernity_pil, overlayed_img_typicality_pil
|
| 230 |
|
|
@@ -239,8 +242,8 @@ iface = gr.Interface(
|
|
| 239 |
gr.Textbox(label="Modernity Score"),
|
| 240 |
gr.Textbox(label="Typicality Score"),
|
| 241 |
gr.Textbox(label="most_similar_group"),
|
| 242 |
-
gr.Image(label="Grad-CAM for
|
| 243 |
-
gr.Image(label="Grad-CAM for
|
| 244 |
],
|
| 245 |
title="Automobile Detection and Scoring using Mask R-CNN",
|
| 246 |
description="Upload an image, and the system will detect and segment automobiles, crop the largest car, and predict its modernity and typicality scores. Grad-CAM heatmaps will also be generated."
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
df = pd.read_csv('morphs.csv')
|
|
|
|
| 45 |
|
| 46 |
morphs = {}
|
| 47 |
for _, row in df.iterrows():
|
|
|
|
| 95 |
overlayed_img = cv2.addWeighted(image, alpha, heatmap, 1 - alpha, 0)
|
| 96 |
return overlayed_img
|
| 97 |
|
| 98 |
+
# Set up the model
|
| 99 |
+
def setup_model():
|
| 100 |
+
cfg = get_cfg()
|
| 101 |
+
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
|
| 102 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model
|
| 103 |
+
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
|
| 104 |
+
cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # Use GPU if available
|
| 105 |
+
predictor = DefaultPredictor(cfg)
|
| 106 |
+
return predictor
|
| 107 |
|
| 108 |
# Function to segment image
|
| 109 |
def segment_image(image):
|
|
|
|
| 145 |
|
| 146 |
# Preprocess the cropped car image for the model
|
| 147 |
transform = transforms.Compose([
|
|
|
|
| 148 |
transforms.Resize((224, 224)),
|
| 149 |
transforms.ToTensor(),
|
| 150 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
|
|
|
| 208 |
modernity_scores = calculate_modernity_scores(modernity_output, year_categories).item()
|
| 209 |
|
| 210 |
|
| 211 |
+
target_layer = modernity_model.layer4[-1]
|
| 212 |
|
| 213 |
+
modernity_cam = GradCAM(modernity_model, target_layer)
|
| 214 |
+
modernity_heatmap = modernity_cam(input_tensor, class_idx=torch.argmax(modernity_output).item())
|
| 215 |
|
| 216 |
+
target_layer = typicality_model.layer4[-1]
|
| 217 |
+
typicality_cam = GradCAM(typicality_model, target_layer)
|
| 218 |
+
typicality_heatmap = typicality_cam(input_tensor, class_idx=torch.argmax(typicality_output).item())
|
| 219 |
|
| 220 |
+
# Convert the input image to a format suitable for overlaying
|
| 221 |
+
img_np = np.array(cropped_car_pil)
|
| 222 |
+
img_np = cv2.resize(img_np, (224, 224))
|
| 223 |
|
| 224 |
+
# Overlay the heatmap on the image
|
| 225 |
+
overlayed_img_modernity = overlay_heatmap_on_image(modernity_heatmap, img_np)
|
| 226 |
+
overlayed_img_typicality = overlay_heatmap_on_image(typicality_heatmap, img_np)
|
| 227 |
|
| 228 |
+
# Convert overlayed images back to PIL for saving
|
| 229 |
+
overlayed_img_modernity_pil = Image.fromarray(cv2.cvtColor(overlayed_img_modernity, cv2.COLOR_BGR2RGB))
|
| 230 |
+
overlayed_img_typicality_pil = Image.fromarray(cv2.cvtColor(overlayed_img_typicality, cv2.COLOR_BGR2RGB))
|
| 231 |
|
| 232 |
return "Automobiles detected in the image", cropped_car_pil, modernity_scores, typicality_scores , most_similar_group, overlayed_img_modernity_pil, overlayed_img_typicality_pil
|
| 233 |
|
|
|
|
| 242 |
gr.Textbox(label="Modernity Score"),
|
| 243 |
gr.Textbox(label="Typicality Score"),
|
| 244 |
gr.Textbox(label="most_similar_group"),
|
| 245 |
+
gr.Image(label="Grad-CAM for Type"),
|
| 246 |
+
gr.Image(label="Grad-CAM for Year")
|
| 247 |
],
|
| 248 |
title="Automobile Detection and Scoring using Mask R-CNN",
|
| 249 |
description="Upload an image, and the system will detect and segment automobiles, crop the largest car, and predict its modernity and typicality scores. Grad-CAM heatmaps will also be generated."
|