First commit
Browse files- .gitattributes +6 -0
- README.md +1 -1
- app.py +152 -0
- demo/Pleiades_HD15_Miami_Marina.jpg +3 -0
- demo/Pleiades_Neo_Tucson_USA.jpg +3 -0
- demo/SPOT_Storage.jpg +3 -0
- demo/Satellite_Image_Marina_New_Zealand.jpg +3 -0
- demo/airport01.jpg +3 -0
- demo/two-dogs-with-a-stick.jpg +3 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,9 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
demo/two-dogs-with-a-stick.jpg filter=lfs diff=lfs merge=lfs -text
|
37 |
+
demo/Pleiades_HD15_Miami_Marina.jpg filter=lfs diff=lfs merge=lfs -text
|
38 |
+
demo/Pleiades_Neo_Tucson_USA.jpg filter=lfs diff=lfs merge=lfs -text
|
39 |
+
demo/SPOT_Storage.jpg filter=lfs diff=lfs merge=lfs -text
|
40 |
+
demo/Satellite_Image_Marina_New_Zealand.jpg filter=lfs diff=lfs merge=lfs -text
|
41 |
+
demo/airport01.jpg filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: Open Detection Optical Satellite
|
3 |
-
emoji:
|
4 |
colorFrom: indigo
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
|
|
1 |
---
|
2 |
title: Open Detection Optical Satellite
|
3 |
+
emoji: 👀
|
4 |
colorFrom: indigo
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
app.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import socket
|
3 |
+
import gradio as gr
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image, ImageDraw
|
6 |
+
from pathlib import Path
|
7 |
+
from loguru import logger
|
8 |
+
import cv2
|
9 |
+
import torch
|
10 |
+
import ultralytics
|
11 |
+
from ultralytics import YOLO
|
12 |
+
import time
|
13 |
+
import base64
|
14 |
+
import requests
|
15 |
+
import json
|
16 |
+
|
17 |
+
# API for inferences
|
18 |
+
DL4EO_API_URL = "https://dl4eo--groundingdino-predict-dev.modal.run"
|
19 |
+
#DL4EO_API_URL = "https://dl4eo--groundingdino-predict.modal.run"
|
20 |
+
|
21 |
+
# Auth Token to access API
|
22 |
+
DL4EO_API_KEY = os.environ['DL4EO_API_KEY']
|
23 |
+
|
24 |
+
# width of the boxes on image
|
25 |
+
LINE_WIDTH = 2
|
26 |
+
|
27 |
+
# Check Gradio modules version
|
28 |
+
logger.info(f"Gradio version: {gr.__version__}")
|
29 |
+
|
30 |
+
# Define the inference function
|
31 |
+
def predict_image(image, threshold):
|
32 |
+
|
33 |
+
# Resize the image to the new size
|
34 |
+
#image = image.resize((image.size[0] * 2, image.size[1] * 2))
|
35 |
+
|
36 |
+
if isinstance(image, Image.Image):
|
37 |
+
img = np.array(image)
|
38 |
+
|
39 |
+
if not isinstance(img, np.ndarray) or len(img.shape) != 3 or img.shape[2] != 3:
|
40 |
+
raise BaseException("predit_image(): input 'img' shoud be single RGB image in PIL or Numpy array format.")
|
41 |
+
|
42 |
+
#width, height = img.shape[0], img.shape[1]
|
43 |
+
|
44 |
+
# Encode the image data as base64
|
45 |
+
image_base64 = base64.b64encode(np.ascontiguousarray(img)).decode()
|
46 |
+
|
47 |
+
# Create a dictionary representing the JSON payload
|
48 |
+
payload = {
|
49 |
+
'image': image_base64,
|
50 |
+
'shape': img.shape,
|
51 |
+
'text_prompt': text_prompt,
|
52 |
+
'box_threshold': box_threshold,
|
53 |
+
'text_threshold': text_threshold,
|
54 |
+
}
|
55 |
+
|
56 |
+
headers = {
|
57 |
+
'Authorization': 'Bearer ' + DL4EO_API_KEY,
|
58 |
+
'Content-Type': 'application/json' # Adjust the content type as needed
|
59 |
+
}
|
60 |
+
|
61 |
+
# Send the POST request to the API endpoint with the image file as binary payload
|
62 |
+
response = requests.post(DL4EO_API_URL, json=payload, headers=headers)
|
63 |
+
|
64 |
+
# Check the response status
|
65 |
+
if response.status_code != 200:
|
66 |
+
raise Exception(
|
67 |
+
f"Received status code={response.status_code} in inference API"
|
68 |
+
)
|
69 |
+
|
70 |
+
json_data = json.loads(response.content)
|
71 |
+
duration = json_data['duration']
|
72 |
+
boxes = json_data['boxes']
|
73 |
+
|
74 |
+
# drow boxes on image
|
75 |
+
draw = ImageDraw.Draw(image)
|
76 |
+
|
77 |
+
for box in boxes:
|
78 |
+
left, top, right, bottom = box
|
79 |
+
|
80 |
+
if left <= 0: left = -LINE_WIDTH
|
81 |
+
if top <= 0: top = top - LINE_WIDTH
|
82 |
+
if right >= img.shape[0] - 1: right = img.shape[0] - 1 + LINE_WIDTH
|
83 |
+
if bottom >= img.shape[1] - 1: bottom = img.shape[1] - 1 + LINE_WIDTH
|
84 |
+
|
85 |
+
draw.rectangle([left, top, right, bottom], outline="red", width=LINE_WIDTH)
|
86 |
+
|
87 |
+
return image, str(image.size), len(boxes), duration
|
88 |
+
|
89 |
+
|
90 |
+
# Define example images and their true labels for users to choose from
|
91 |
+
example_data = [
|
92 |
+
["./demo/Pleiades_Neo_Tucson_USA.jpg", 'plane', 0.24, 0.24],
|
93 |
+
["./demo/Pleiades_Neo_Tucson_USA.jpg", 'building', 0.24, 0.24],
|
94 |
+
["./demo/Pleiades_Neo_Tucson_USA.jpg", 'tree', 0.24, 0.24],
|
95 |
+
["./demo/two-dogs-with-a-stick.jpg", "dog", 0.25, 0.25],
|
96 |
+
["./demo/airport01.jpg", "aircraft", 0.25, 0.25],
|
97 |
+
["./demo/SPOT_Storage.jpg", "storage", 0.25, 0.25],
|
98 |
+
["./demo/Satellite_Image_Marina_New_Zealand.jpg", "ship", 0.25, 0.25],
|
99 |
+
["./demo/Pleiades_HD15_Miami_Marina.jpg", "ship", 0.25, 0.25],
|
100 |
+
]
|
101 |
+
|
102 |
+
# Define CSS for some elements
|
103 |
+
css = """
|
104 |
+
.image-preview {
|
105 |
+
height: 820px !important;
|
106 |
+
width: 800px !important;
|
107 |
+
}
|
108 |
+
"""
|
109 |
+
TITLE = "Open detection on optical satellite images"
|
110 |
+
|
111 |
+
# Define the Gradio Interface
|
112 |
+
demo = gr.Blocks(title=TITLE, css=css).queue()
|
113 |
+
with demo:
|
114 |
+
gr.Markdown(f"<h1><center>{TITLE}<center><h1>")
|
115 |
+
|
116 |
+
with gr.Row():
|
117 |
+
with gr.Column(scale=0):
|
118 |
+
input_image = gr.Image(type="pil", interactive=True, scale=1)
|
119 |
+
text_prompt = gr.Textbox(label="Text prompt")
|
120 |
+
run_button = gr.Button(value="Run", scale=0)
|
121 |
+
with gr.Accordion("Advanced options", open=True):
|
122 |
+
box_threshold = gr.Slider(label="Box threshold", minimum=0.0, maximum=1.0, value=0.24, step=0.01)
|
123 |
+
text_threshold = gr.Slider(label="Text threshold", minimum=0.0, maximum=1.0, value=0.24, step=0.01)
|
124 |
+
dimensions = gr.Textbox(label="Image size", interactive=False)
|
125 |
+
detections = gr.Number(label="Predicted objects", interactive=False)
|
126 |
+
stopwatch = gr.Number(label="Execution time (sec.)", interactive=False, precision=3)
|
127 |
+
|
128 |
+
with gr.Column(scale=2):
|
129 |
+
output_image = gr.Image(type="pil", elem_classes='image-preview', interactive=False, width=800, height=800)
|
130 |
+
|
131 |
+
run_button.click(fn=predict_image, inputs=[input_image, text_prompt, box_threshold, text_threshold], outputs=[output_image, dimensions, detections, stopwatch])
|
132 |
+
gr.Examples(
|
133 |
+
examples=example_data,
|
134 |
+
inputs = [input_image, text_prompt, box_threshold, text_threshold],
|
135 |
+
outputs = [output_image, dimensions, detections, stopwatch],
|
136 |
+
fn=predict_image,
|
137 |
+
cache_examples=True,
|
138 |
+
label='Try these images!'
|
139 |
+
)
|
140 |
+
|
141 |
+
gr.Markdown("<p>This demo is provided by <a href='https://www.linkedin.com/in/faudi/'>Jeff Faudi</a> \
|
142 |
+
and <a href='https://www.dl4eo.com/'>DL4EO</a>. The demonstration images are Pléiades \
|
143 |
+
images provided by CNES with distribution by Airbus DS. The model architecture and weights \
|
144 |
+
are provided <a href='https://github.com/IDEA-Research/GroundingDINO'>Grounding DINO</a>. \
|
145 |
+
This is a demonstration only. Please contact <a href='mailto:[email protected]'>me</a> \
|
146 |
+
for more information on how you could get access to a commercial model or API. </p>")
|
147 |
+
|
148 |
+
demo.launch(
|
149 |
+
inline=False,
|
150 |
+
show_api=False,
|
151 |
+
debug=False
|
152 |
+
)
|
demo/Pleiades_HD15_Miami_Marina.jpg
ADDED
Git LFS Details
|
demo/Pleiades_Neo_Tucson_USA.jpg
ADDED
Git LFS Details
|
demo/SPOT_Storage.jpg
ADDED
Git LFS Details
|
demo/Satellite_Image_Marina_New_Zealand.jpg
ADDED
Git LFS Details
|
demo/airport01.jpg
ADDED
Git LFS Details
|
demo/two-dogs-with-a-stick.jpg
ADDED
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
loguru
|
2 |
+
ultralytics==8.1.18
|
3 |
+
gradio==3.35.2
|