import gradio as gr import requests from PIL import Image from io import BytesIO import numpy as np import re from landingai.common import decode_bitmap_rle import os # For accessing secrets ENDPOINT_ID = "ba678fa4-65d1-4b87-8c85-cebd15224783" API_KEY = os.environ.get("land_sk_ikq7WEKGtaKI7pXIcKt2x7RoyYE6FBReqGOmKtEhjcmFbLbQsK") API_URL = f"https://predict.app.landing.ai/inference/v1/predict?endpoint_id={ENDPOINT_ID}" def predict_from_landinglens(image_path): # Load and keep original image original_img = Image.open(image_path).convert("RGB") img_array = np.array(original_img) # Get image dimensions height, width = img_array.shape[:2] total_pixels = height * width # Prepare for API buffered = BytesIO() original_img.save(buffered, format="JPEG") img_bytes = buffered.getvalue() files = {"file": (image_path, img_bytes, "image/jpeg")} headers = {"apikey": API_KEY} try: response = requests.post(API_URL, files=files, headers=headers) if response.status_code == 503: return "Service temporarily unavailable. Please try again later." response.raise_for_status() prediction = response.json() if "predictions" not in prediction or not prediction.get("predictions"): print("No 'predictions' key found or it's empty.") return "Error: No 'predictions' found." bitmaps = prediction["predictions"]["bitmaps"] masked_images = [] coverage_info = [] for i, (bitmap_id, bitmap_data) in enumerate(bitmaps.items()): try: # Decode mask mask = decode_bitmap_rle(bitmap_data["bitmap"]) if isinstance(mask, list): mask = np.array(mask) # Reshape mask to match image dimensions mask = mask.reshape(prediction["predictions"]["imageHeight"], prediction["predictions"]["imageWidth"]) # Calculate area coverage mask_area = np.sum(mask > 0) coverage_percentage = (mask_area / total_pixels) * 100 label_name = bitmap_data.get("label_name", f"Mask {i}") coverage_info.append(f"{label_name}: {coverage_percentage:.2f}%") # Create colored overlay using Pillow mask_image = Image.fromarray((mask > 0).astype(np.uint8) * 255) overlay = Image.new("RGB", mask_image.size, (255, 0, 0)) # Red overlay mask_image = mask_image.convert("RGB") masked_image = Image.blend(original_img, overlay, alpha=0.5 * mask_image.convert("L").point(lambda x: x > 0 and 1)) masked_images.append(masked_image) except Exception as e: print(f"Error processing mask {i}: {e}") continue return masked_images, "\n".join(coverage_info) except requests.exceptions.RequestException as e: print(f"API Error: {e}") return f"API Error: {e}" iface = gr.Interface( fn=predict_from_landinglens, inputs=gr.Image(type="filepath"), outputs=[ gr.Gallery(format="png"), gr.Textbox(label="Area of each mask in the image") ], title="Crosswalk detection model", ) iface.launch()