Initial commit
Browse files- .gitattributes +5 -0
- .gitignore +3 -0
- README.md +8 -6
- app.py +167 -0
- demo/588fc1fb-b86a-4fb4-8161-d9bd3a1556ca.jpg +3 -0
- demo/605ffac0-69d5-4748-92c2-48d43f51afc1.jpg +3 -0
- demo/67f7c7ad-11a1-4c7f-9f2a-da7ef50bfdd8.jpg +3 -0
- demo/b8c0e212-3669-4ff8-81a5-32191d456f86.jpg +3 -0
- demo/df5ec618-c1f3-4cfe-88b1-86799d23c22d.jpg +3 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,8 @@ 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/67f7c7ad-11a1-4c7f-9f2a-da7ef50bfdd8.jpg filter=lfs diff=lfs merge=lfs -text
|
37 |
+
demo/b8c0e212-3669-4ff8-81a5-32191d456f86.jpg filter=lfs diff=lfs merge=lfs -text
|
38 |
+
demo/df5ec618-c1f3-4cfe-88b1-86799d23c22d.jpg filter=lfs diff=lfs merge=lfs -text
|
39 |
+
demo/588fc1fb-b86a-4fb4-8161-d9bd3a1556ca.jpg filter=lfs diff=lfs merge=lfs -text
|
40 |
+
demo/605ffac0-69d5-4748-92c2-48d43f51afc1.jpg filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
**/.DS_Store
|
2 |
+
**/__pycache__/
|
3 |
+
weights/
|
README.md
CHANGED
@@ -1,11 +1,13 @@
|
|
1 |
---
|
2 |
-
title: Oil Storage Detection
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
-
sdk:
|
|
|
|
|
7 |
pinned: false
|
8 |
-
license: cc-by-nc-4.0
|
9 |
---
|
10 |
|
11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Oil Storage Detection in optical satellite images (Airbus SPOT)
|
3 |
+
emoji: 🛢️
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: blue
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 4.19.2
|
8 |
+
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: cc-by-nc-sa-4.0
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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--oil-storage-predict.modal.run"
|
19 |
+
|
20 |
+
# Auth Token to access API
|
21 |
+
DL4EO_API_KEY = os.environ['DL4EO_API_KEY']
|
22 |
+
|
23 |
+
# width of the boxes on image
|
24 |
+
LINE_WIDTH = 2
|
25 |
+
|
26 |
+
# Load a model if weights are present
|
27 |
+
WEIGHTS_FILE = './weights/best.pt'
|
28 |
+
model = None
|
29 |
+
if os.path.exists(WEIGHTS_FILE):
|
30 |
+
model = YOLO(WEIGHTS_FILE) # previously trained YOLOv8n model
|
31 |
+
logger.info(f"Setup for local inference")
|
32 |
+
|
33 |
+
# check if GPU if available
|
34 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")
|
35 |
+
logger.info(f"Using device: {device}")
|
36 |
+
|
37 |
+
# Check Ultralytics modules version
|
38 |
+
logger.info(f"Ultralytics version: {ultralytics.__version__}")
|
39 |
+
|
40 |
+
logger.info(f"Gradio version: {gr.__version__}")
|
41 |
+
|
42 |
+
# Define the inference function
|
43 |
+
def predict_image(image, threshold):
|
44 |
+
|
45 |
+
# Resize the image to the new size
|
46 |
+
#image = image.resize((image.size[0] * 2, image.size[1] * 2))
|
47 |
+
|
48 |
+
if isinstance(image, Image.Image):
|
49 |
+
img = np.array(image)
|
50 |
+
|
51 |
+
if not isinstance(img, np.ndarray) or len(img.shape) != 3 or img.shape[2] != 3:
|
52 |
+
raise BaseException("predit_image(): input 'img' shoud be single RGB image in PIL or Numpy array format.")
|
53 |
+
|
54 |
+
width, height = img.shape[0], img.shape[1]
|
55 |
+
|
56 |
+
if model is None:
|
57 |
+
# Encode the image data as base64
|
58 |
+
image_base64 = base64.b64encode(np.ascontiguousarray(img)).decode()
|
59 |
+
|
60 |
+
# Create a dictionary representing the JSON payload
|
61 |
+
payload = {
|
62 |
+
'image': image_base64,
|
63 |
+
'shape': img.shape,
|
64 |
+
'threshold': threshold,
|
65 |
+
}
|
66 |
+
|
67 |
+
headers = {
|
68 |
+
'Authorization': 'Bearer ' + DL4EO_API_KEY,
|
69 |
+
'Content-Type': 'application/json' # Adjust the content type as needed
|
70 |
+
}
|
71 |
+
|
72 |
+
# Send the POST request to the API endpoint with the image file as binary payload
|
73 |
+
response = requests.post(DL4EO_API_URL, json=payload, headers=headers)
|
74 |
+
|
75 |
+
# Check the response status
|
76 |
+
if response.status_code != 200:
|
77 |
+
raise Exception(
|
78 |
+
f"Received status code={response.status_code} in inference API"
|
79 |
+
)
|
80 |
+
|
81 |
+
json_data = json.loads(response.content)
|
82 |
+
duration = json_data['duration']
|
83 |
+
boxes = json_data['boxes']
|
84 |
+
else:
|
85 |
+
start_time = time.time()
|
86 |
+
results = model.predict([img], imgsz=(width, height), conf=threshold)
|
87 |
+
end_time = time.time()
|
88 |
+
boxes = [box.xyxy.cpu().squeeze().int().tolist() for box in boxes]
|
89 |
+
duration = end_time - start_time
|
90 |
+
boxes = results[0].boxes
|
91 |
+
|
92 |
+
# drow boxes on image
|
93 |
+
draw = ImageDraw.Draw(image)
|
94 |
+
|
95 |
+
for box in boxes:
|
96 |
+
left, top, right, bottom = box
|
97 |
+
|
98 |
+
if left <= 0: left = -LINE_WIDTH
|
99 |
+
if top <= 0: top = top - LINE_WIDTH
|
100 |
+
if right >= img.shape[0] - 1: right = img.shape[0] - 1 + LINE_WIDTH
|
101 |
+
if bottom >= img.shape[1] - 1: bottom = img.shape[1] - 1 + LINE_WIDTH
|
102 |
+
|
103 |
+
draw.rectangle([left, top, right, bottom], outline="red", width=LINE_WIDTH)
|
104 |
+
|
105 |
+
return image, str(image.size), len(boxes), duration
|
106 |
+
|
107 |
+
|
108 |
+
# Define example images and their true labels for users to choose from
|
109 |
+
example_data = [
|
110 |
+
["./demo/588fc1fb-b86a-4fb4-8161-d9bd3a1556ca.jpg", 0.50],
|
111 |
+
["./demo/605ffac0-69d5-4748-92c2-48d43f51afc1.jpg", 0.50],
|
112 |
+
["./demo/67f7c7ad-11a1-4c7f-9f2a-da7ef50bfdd8.jpg", 0.50],
|
113 |
+
["./demo/b8c0e212-3669-4ff8-81a5-32191d456f86.jpg", 0.50],
|
114 |
+
["./demo/df5ec618-c1f3-4cfe-88b1-86799d23c22d.jpg", 0.50]]
|
115 |
+
|
116 |
+
# Define CSS for some elements
|
117 |
+
css = """
|
118 |
+
.image-preview {
|
119 |
+
height: 820px !important;
|
120 |
+
width: 800px !important;
|
121 |
+
}
|
122 |
+
"""
|
123 |
+
TITLE = "Oil storage detection on SPOT images (1.5 m) with YOLOv8"
|
124 |
+
|
125 |
+
# Define the Gradio Interface
|
126 |
+
demo = gr.Blocks(title=TITLE, css=css).queue()
|
127 |
+
with demo:
|
128 |
+
gr.Markdown(f"<h1><center>{TITLE}<center><h1>")
|
129 |
+
#gr.Markdown("<p>This demo is provided by <a href='https://www.linkedin.com/in/faudi/'>Jeff Faudi</a> and <a href='https://www.dl4eo.com/'>DL4EO</a></p>")
|
130 |
+
|
131 |
+
with gr.Row():
|
132 |
+
with gr.Column(scale=0):
|
133 |
+
input_image = gr.Image(type="pil", interactive=True, scale=1)
|
134 |
+
run_button = gr.Button(value="Run", scale=0)
|
135 |
+
with gr.Accordion("Advanced options", open=True):
|
136 |
+
threshold = gr.Slider(label="Confidence threshold", minimum=0.0, maximum=1.0, value=0.25, step=0.01)
|
137 |
+
dimensions = gr.Textbox(label="Image size", interactive=False)
|
138 |
+
detections = gr.Number(label="Predicted objects", interactive=False)
|
139 |
+
stopwatch = gr.Number(label="Execution time (sec.)", interactive=False, precision=3)
|
140 |
+
|
141 |
+
with gr.Column(scale=2):
|
142 |
+
output_image = gr.Image(type="pil", elem_classes='image-preview', interactive=False, width=800, height=800)
|
143 |
+
|
144 |
+
run_button.click(fn=predict_image, inputs=[input_image, threshold], outputs=[output_image, dimensions, detections, stopwatch])
|
145 |
+
gr.Examples(
|
146 |
+
examples=example_data,
|
147 |
+
inputs = [input_image, threshold],
|
148 |
+
outputs = [output_image, dimensions, detections, stopwatch],
|
149 |
+
fn=predict_image,
|
150 |
+
#cache_examples=True,
|
151 |
+
label='Try these images! They are not included in the training dataset.'
|
152 |
+
)
|
153 |
+
|
154 |
+
gr.Markdown("""<p>This demo is provided by <a href='https://www.linkedin.com/in/faudi/'>Jeff Faudi</a> and <a href='https://www.dl4eo.com/'>DL4EO</a>.
|
155 |
+
The model has been trained with the <a href='https://www.ultralytics.com/yolo'>Ultralytics YOLOv8</a> framework on the
|
156 |
+
<a href='https://www.kaggle.com/datasets/airbusgeo/airbus-oil-storage-detection-dataset'>Airbus Oil Storage Dataset</a>.
|
157 |
+
The associated license is <a href='https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en'>CC-BY-SA-NC</a>.
|
158 |
+
This demonstration CANNOT be used for commercial puposes. Please contact <a href='mailto:jeff@dl4eo.com'>me</a>
|
159 |
+
for more information on how you could get access to a commercial grade model or API. </p>""")
|
160 |
+
|
161 |
+
|
162 |
+
demo.launch(
|
163 |
+
inline=False,
|
164 |
+
show_api=False,
|
165 |
+
debug=False
|
166 |
+
)
|
167 |
+
|
demo/588fc1fb-b86a-4fb4-8161-d9bd3a1556ca.jpg
ADDED
Git LFS Details
|
demo/605ffac0-69d5-4748-92c2-48d43f51afc1.jpg
ADDED
Git LFS Details
|
demo/67f7c7ad-11a1-4c7f-9f2a-da7ef50bfdd8.jpg
ADDED
Git LFS Details
|
demo/b8c0e212-3669-4ff8-81a5-32191d456f86.jpg
ADDED
Git LFS Details
|
demo/df5ec618-c1f3-4cfe-88b1-86799d23c22d.jpg
ADDED
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
loguru
|
2 |
+
ultralytics==8.1.18
|
3 |
+
gradio==3.35.2
|