Spaces:
Runtime error
Runtime error
Andrey
commited on
Upload images and bboxes to s3. Refactor. (#5)
Browse files* Upload images and bboxes to s3. Refactor.
* Remove comment.
- .gitignore +2 -0
- README.md +3 -9
- config.toml +0 -8
- src/utils.py +35 -8
- st_app.py +5 -4
.gitignore
CHANGED
|
@@ -150,3 +150,5 @@ cython_debug/
|
|
| 150 |
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 151 |
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 152 |
#.idea/
|
|
|
|
|
|
|
|
|
| 150 |
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 151 |
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 152 |
#.idea/
|
| 153 |
+
# config with secrets
|
| 154 |
+
config.toml
|
README.md
CHANGED
|
@@ -4,14 +4,8 @@
|
|
| 4 |
steps:
|
| 5 |
* use git lfs for the model +
|
| 6 |
* write better code +
|
| 7 |
-
* convert model to onnx or some other format
|
| 8 |
-
* deploy bare working app, without nice things
|
|
|
|
| 9 |
* make better design
|
| 10 |
* think about descriptions on the site
|
| 11 |
-
|
| 12 |
-
On using git lfs:
|
| 13 |
-
```shell
|
| 14 |
-
git lfs install
|
| 15 |
-
git lfs track "*.psd"
|
| 16 |
-
git add .gitattributes
|
| 17 |
-
```
|
|
|
|
| 4 |
steps:
|
| 5 |
* use git lfs for the model +
|
| 6 |
* write better code +
|
| 7 |
+
* convert model to onnx or some other format?. not needed
|
| 8 |
+
* deploy bare working app, without nice things +
|
| 9 |
+
* save images to amazon+
|
| 10 |
* make better design
|
| 11 |
* think about descriptions on the site
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config.toml
DELETED
|
@@ -1,8 +0,0 @@
|
|
| 1 |
-
# Everything in this section will be available as an environment variable
|
| 2 |
-
db_username = "Jane"
|
| 3 |
-
db_password = "12345qwerty"
|
| 4 |
-
|
| 5 |
-
AWS_ACCESS_KEY_ID = 'AKIAI4JDKBYRCHGT77VQ'
|
| 6 |
-
AWS_SECRET_ACCESS_KEY = 'ewSheQRxUKM/QTtHUPlESpMhl4bBQfihGWpBFy4s'
|
| 7 |
-
S3_BUCKET = 'digitdrawdetect'
|
| 8 |
-
S3_BUCKET_NAME = 'digitdrawdetect'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/utils.py
CHANGED
|
@@ -1,10 +1,16 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
| 3 |
import matplotlib
|
| 4 |
import matplotlib.patches as patches
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
import numpy.typing as npt
|
| 7 |
-
import
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def plot_img_with_rects(
|
|
@@ -48,14 +54,35 @@ def plot_img_with_rects(
|
|
| 48 |
return fig
|
| 49 |
|
| 50 |
|
| 51 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
"""
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
Returns:
|
| 56 |
-
|
|
|
|
| 57 |
"""
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
return
|
|
|
|
| 1 |
+
import datetime
|
| 2 |
+
import json
|
| 3 |
+
import uuid
|
| 4 |
+
from typing import List
|
| 5 |
|
| 6 |
+
import boto3
|
| 7 |
import matplotlib
|
| 8 |
import matplotlib.patches as patches
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
import numpy.typing as npt
|
| 11 |
+
import streamlit as st
|
| 12 |
+
|
| 13 |
+
client = boto3.client('s3')
|
| 14 |
|
| 15 |
|
| 16 |
def plot_img_with_rects(
|
|
|
|
| 54 |
return fig
|
| 55 |
|
| 56 |
|
| 57 |
+
def save_object_to_s3(filename, s3_filename):
|
| 58 |
+
client.upload_file(filename, 'digitdrawdetect', s3_filename)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@st.cache(show_spinner=False)
|
| 62 |
+
def save_image(image: npt.ArrayLike, pred: List[List]) -> str:
|
| 63 |
"""
|
| 64 |
+
Save the image and upload the image with bboxes to s3.
|
| 65 |
+
|
| 66 |
+
Args:
|
| 67 |
+
image: np.array with image
|
| 68 |
+
pred: bboxes
|
| 69 |
|
| 70 |
Returns:
|
| 71 |
+
image name
|
| 72 |
+
|
| 73 |
"""
|
| 74 |
+
# create a figure and save it
|
| 75 |
+
fig, ax = plt.subplots(1, figsize=(4, 4))
|
| 76 |
+
ax.imshow(image)
|
| 77 |
+
file_name = str(datetime.datetime.today().date()) + str(uuid.uuid1())
|
| 78 |
+
fig.savefig(f'{file_name}.png')
|
| 79 |
+
|
| 80 |
+
# dump bboxes in a local file
|
| 81 |
+
with open(f'{file_name}.json', 'w') as f:
|
| 82 |
+
json.dump({f'{file_name}.png': pred}, f)
|
| 83 |
+
|
| 84 |
+
# upload the image and the bboxes to s3.
|
| 85 |
+
save_object_to_s3(f'{file_name}.png', f'images/{file_name}.png')
|
| 86 |
+
save_object_to_s3(f'{file_name}.json', f'labels/{file_name}.json')
|
| 87 |
|
| 88 |
+
return file_name
|
st_app.py
CHANGED
|
@@ -6,7 +6,7 @@ from PIL import Image
|
|
| 6 |
from streamlit_drawable_canvas import st_canvas
|
| 7 |
|
| 8 |
from src.ml_utils import predict, get_model, transforms
|
| 9 |
-
from src.utils import plot_img_with_rects,
|
| 10 |
|
| 11 |
logging.info('Starting')
|
| 12 |
|
|
@@ -26,7 +26,6 @@ with col1:
|
|
| 26 |
key='canvas',
|
| 27 |
)
|
| 28 |
with col2:
|
| 29 |
-
data = get_config()
|
| 30 |
logging.info('canvas ready')
|
| 31 |
if canvas_result.image_data is not None:
|
| 32 |
# convert a drawn image into numpy array with RGB from a canvas image with RGBA
|
|
@@ -37,9 +36,11 @@ with col2:
|
|
| 37 |
logging.info('model ready')
|
| 38 |
pred = predict(model, image)
|
| 39 |
logging.info('prediction done')
|
|
|
|
|
|
|
| 40 |
threshold = st.slider('Bbox probability slider', min_value=0.0, max_value=1.0, value=0.5)
|
| 41 |
|
| 42 |
fig = plot_img_with_rects(image.permute(1, 2, 0).numpy(), pred, threshold, coef=192)
|
| 43 |
-
fig.savefig('
|
| 44 |
-
image = Image.open('
|
| 45 |
st.image(image)
|
|
|
|
| 6 |
from streamlit_drawable_canvas import st_canvas
|
| 7 |
|
| 8 |
from src.ml_utils import predict, get_model, transforms
|
| 9 |
+
from src.utils import plot_img_with_rects, save_image
|
| 10 |
|
| 11 |
logging.info('Starting')
|
| 12 |
|
|
|
|
| 26 |
key='canvas',
|
| 27 |
)
|
| 28 |
with col2:
|
|
|
|
| 29 |
logging.info('canvas ready')
|
| 30 |
if canvas_result.image_data is not None:
|
| 31 |
# convert a drawn image into numpy array with RGB from a canvas image with RGBA
|
|
|
|
| 36 |
logging.info('model ready')
|
| 37 |
pred = predict(model, image)
|
| 38 |
logging.info('prediction done')
|
| 39 |
+
|
| 40 |
+
file_name = save_image(image.permute(1, 2, 0).numpy(), pred)
|
| 41 |
threshold = st.slider('Bbox probability slider', min_value=0.0, max_value=1.0, value=0.5)
|
| 42 |
|
| 43 |
fig = plot_img_with_rects(image.permute(1, 2, 0).numpy(), pred, threshold, coef=192)
|
| 44 |
+
fig.savefig(f'{file_name}_temp.png')
|
| 45 |
+
image = Image.open(f'{file_name}_temp.png')
|
| 46 |
st.image(image)
|