Spaces:
Runtime error
Runtime error
Update app.py
Browse files23 unmodified lines2425262728293068 unmodified lines9910010110210310410510610710810911011111211399 unmodified lines21321421521621721821922022122222322422522622722822923023123223323423523623723823924023 unmodified linesdef setup_model():cfg = get_cfg()cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"#cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml")cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # Set a threshold for this model68 unmodified linesoverlayed_img = cv2.addWeighted(image, alpha, heatmap, 1 - alpha, 0)return overlayed_img# # Set up the model# def setup_model():# cfg = get_cfg()# cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))# cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model# cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")# cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # Use GPU if available# predictor = DefaultPredictor(cfg)# return predictor# Function to segment imagedef segment_image(image):99 unmodified linesmodernity_scores = calculate_modernity_scores(modernity_output, year_categories).item()target_layer = modernity_model.layer4[-1]modernity_cam = GradCAM(modernity_model, target_layer)modernity_heatmap = modernity_cam(input_tensor, class_idx=torch.argmax(modernity_output).item())target_layer = typicality_model.layer4[-1]typicality_cam = GradCAM(typicality_model, target_layer)typicality_heatmap = typicality_cam(input_tensor, class_idx=torch.argmax(typicality_output).item())# Convert the input image to a format suitable for overlayingimg_np = np.array(cropped_car_pil)img_np = cv2.resize(img_np, (224, 224))# Overlay the heatmap on the imageoverlayed_img_modernity = overlay_heatmap_on_image(modernity_heatmap, img_np)overlayed_img_typicality = overlay_heatmap_on_image(typicality_heatmap, img_np)# Convert overlayed images back to PIL for savingoverlayed_img_modernity_pil = Image.fromarray(cv2.cvtColor(overlayed_img_modernity, cv2.COLOR_BGR2RGB))overlayed_img_typicality_pil = Image.fromarray(cv2.cvtColor(overlayed_img_typicality, cv2.COLOR_BGR2RGB))return "Automobiles detected in the image", cropped_car_pil, modernity_scores, typicality_scores , most_similar_group, overlayed_img_modernity_pil, overlayed_img_typicality_pil# Create Gradio interface23 unmodified lines2425262728293068 unmodified lines9910010110210310499 unmodified lines20420520620720820921021121221321421521621721821922022122222322422522622722822923023123 unmodified linesdef setup_model():cfg = get_cfg()cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"cfg.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml")cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml")cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # Set a threshold for this model68 unmodified linesoverlayed_img = cv2.addWeighted(image, alpha, heatmap, 1 - alpha, 0)return overlayed_img# Function to segment imagedef segment_image(image):99 unmodified linesmodernity_scores = calculate_modernity_scores(modernity_output, year_categories).item()# target_layer = modernity_model.layer4[-1]# modernity_cam = GradCAM(modernity_model, target_layer)# modernity_heatmap = modernity_cam(input_tensor, class_idx=torch.argmax(modernity_output).item())# target_layer = typicality_model.layer4[-1]# typicality_cam = GradCAM(typicality_model, target_layer)# typicality_heatmap = typicality_cam(input_tensor, class_idx=torch.argmax(typicality_output).item())# # Convert the input image to a format suitable for overlaying# img_np = np.array(cropped_car_pil)# img_np = cv2.resize(img_np, (224, 224))# # Overlay the heatmap on the image# overlayed_img_modernity = overlay_heatmap_on_image(modernity_heatmap, img_np)# overlayed_img_typicality = overlay_heatmap_on_image(typicality_heatmap, img_np)# # Convert overlayed images back to PIL for saving# overlayed_img_modernity_pil = Image.fromarray(cv2.cvtColor(overlayed_img_modernity, cv2.COLOR_BGR2RGB))# overlayed_img_typicality_pil = Image.fromarray(cv2.cvtColor(overlayed_img_typicality, cv2.COLOR_BGR2RGB))return "Automobiles detected in the image", cropped_car_pil, modernity_scores, typicality_scores , most_similar_group#, overlayed_img_modernity_pil, overlayed_img_typicality_pil# Create Gradio interface
app.py+17-26