import os import socket import gradio as gr import numpy as np from PIL import Image, ImageDraw from pathlib import Path from loguru import logger import cv2 import torch import time import base64 import requests import json # API for inferences DL4EO_API_URL = "https://dl4eo--groundingdino-predict.modal.run" # Auth Token to access API DL4EO_API_KEY = os.environ['DL4EO_API_KEY'] # width of the boxes on image LINE_WIDTH = 2 # Check Gradio modules version logger.info(f"Gradio version: {gr.__version__}") # Define the inference function def predict_image(image, text_prompt, box_threshold, text_threshold): # Resize the image to the new size #image = image.resize((image.size[0] * 2, image.size[1] * 2)) if isinstance(image, Image.Image): img = np.array(image) if not isinstance(img, np.ndarray) or len(img.shape) != 3 or img.shape[2] != 3: raise BaseException("predit_image(): input 'img' shoud be single RGB image in PIL or Numpy array format.") #width, height = img.shape[0], img.shape[1] # Encode the image data as base64 image_base64 = base64.b64encode(np.ascontiguousarray(img)).decode() # Create a dictionary representing the JSON payload payload = { 'image': image_base64, 'shape': img.shape, 'text_prompt': text_prompt, 'box_threshold': box_threshold, 'text_threshold': text_threshold, } headers = { 'Authorization': 'Bearer ' + DL4EO_API_KEY, 'Content-Type': 'application/json' # Adjust the content type as needed } # Send the POST request to the API endpoint with the image file as binary payload response = requests.post(DL4EO_API_URL, json=payload, headers=headers) # Check the response status if response.status_code != 200: raise Exception( f"Received status code={response.status_code} in inference API: {response.text}" ) json_data = json.loads(response.content) duration = json_data['duration'] boxes = json_data['boxes'] # drow boxes on image draw = ImageDraw.Draw(image) for box in boxes: left, top, right, bottom = box if left <= 0: left = -LINE_WIDTH if top <= 0: top = top - LINE_WIDTH if right >= img.shape[0] - 1: right = img.shape[0] - 1 + LINE_WIDTH if bottom >= img.shape[1] - 1: bottom = img.shape[1] - 1 + LINE_WIDTH draw.rectangle([left, top, right, bottom], outline="red", width=LINE_WIDTH) return image, str(image.size), len(boxes), duration # Define example images and their true labels for users to choose from example_data = [ ["./demo/Pleiades_Neo_Tucson_USA.jpg", 'plane', 0.24, 0.24], ["./demo/Pleiades_Neo_Tucson_USA.jpg", 'building', 0.24, 0.24], #["./demo/Pleiades_Neo_Tucson_USA.jpg", 'tree', 0.24, 0.24], #["./demo/two-dogs-with-a-stick.jpg", "dog", 0.25, 0.25], #["./demo/airport01.jpg", "aircraft", 0.25, 0.25], #["./demo/SPOT_Storage.jpg", "storage", 0.25, 0.25], #["./demo/Satellite_Image_Marina_New_Zealand.jpg", "ship", 0.25, 0.25], ["./demo/Pleiades_HD15_Miami_Marina.jpg", "motorboat", 0.3, 0.0], ["./demo/Pleiades_HD15_Miami_Marina.jpg", "palm tree", 0.15, 0.3], ["./demo/Pleiades_HD15_Miami_Marina.jpg", "building", 0.3, 0.0], ] # Define CSS for some elements css = """ .image-preview { height: 820px !important; width: 800px !important; } """ TITLE = "Open detection on optical satellite images" # Define the Gradio Interface demo = gr.Blocks(title=TITLE, css=css).queue() with demo: gr.Markdown(f"
This demo is provided by Jeff Faudi \ and DL4EO. The demonstration images are Pléiades \ images provided by CNES with distribution by Airbus DS. The model architecture and weights \ are provided Grounding DINO. \ The model has not been trained specifically on satellite imagery and should be finetuned for this task. \ This is for demonstration only. Please contact me \ for more information on how you could get access to a commercial model or API.
") demo.launch( inline=False, show_api=False, debug=False )