import os import socket import time import gradio as gr import numpy as np from PIL import Image, ImageDraw import base64 import requests import json # API for inferences DL4EO_API_URL = "https://dl4eo--object-predict-raster.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 version print(f"Gradio version: {gr.__version__}") # Define the inference function def predict_image(img, threshold): if isinstance(img, Image.Image): img = np.array(img) 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.") # 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, 'threshold': 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" ) json_data = json.loads(response.content) image_data = json_data['image'] shape = json_data['shape'] infos = json_data['infos'] duration = json_data['duration'] img_data_in = base64.b64decode(json_data['image']) np_img = np.frombuffer(img_data_in, dtype=np.uint8).reshape(shape) pil_img = Image.fromarray(np_img) return pil_img, shape, infos, duration # Define example images and their true labels for users to choose from example_data = [ ["./demo/demo.jpg", 0.8], ["./demo/dota_demo.jpg", 0.8], ["./demo/Satellite_Image_Marina_New_Zealand.jpg", 0.8], ["./demo/Pleiades_HD15_Miami_Marina.jpg", 0.8], ["./demo/SPOT_Storage.jpg", 0.8], ["./demo/airport01.jpg", 0.8], ["./demo/Pleiades_Neo_Tucson_USA.jpg", 0.8], # Add more example images and labels as needed ] # Define CSS for some elements css = """ .image-preview { height: 820px !important; width: 800px !important; } """ TITLE = "Oriented bounding boxes 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. This model is based on the MMRotate framework which provides oriented bounding boxes. We believe that oriented bouding boxes are better suited for detection in satellite images. This model has been trained on the DOTA dataset which contains 15 classes: plane, ship, storage tank, baseball diamond, tennis court, basketball court, ground track field, harbor, bridge, large vehicle, small vehicle, helicopter, roundabout, soccer ball field and swimming pool.
The associated licenses are GoogleEarth fair use and CC-BY-SA-NC. This demonstration CANNOT be used for commercial puposes. Please contact me for more information on how you could get access to a commercial grade model or API.
""") demo.launch( inline=False, show_api=False, debug=False )