Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Aug 1, 2023
1 parent 567dd27 commit 248cb26
Show file tree
Hide file tree
Showing 322 changed files with 1,080,797 additions and 1,078,840 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,3 @@

## 📹Demo
<img width="90%" src="https://user-images.githubusercontent.com/66928953/217624520-21ad0631-a410-4b00-9622-3ea1dd279fbf.gif"/>

2 changes: 1 addition & 1 deletion app/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def main():
st.session_state["inferenced"] = None

models = get_models()
images = get_images()
get_images()
videos = get_videos()
video_names = get_video_names()

Expand Down
4 changes: 2 additions & 2 deletions app/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ image:
format:
- .jpg
- .png

video:
directory: /opt/ml/input/video/
format:
Expand All @@ -36,7 +36,7 @@ image_output_path:
google:
bucket: 'awesome-gcp12'
path: 'https://storage.cloud.google.com/'


server:
host: 0.0.0.0
Expand Down
18 changes: 10 additions & 8 deletions app/db_models/img.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from sqlalchemy import Column, Table
from sqlalchemy.sql.sqltypes import INTEGER, String, VARCHAR
from ..db_config.db import meta, engine
from sqlalchemy.sql.sqltypes import VARCHAR

from ..db_config.db import engine, meta

imgs = Table('img', meta,
Column('id', VARCHAR(100), primary_key=True),
Column('input_path', VARCHAR(100)),
Column('output_path', VARCHAR(100)),
)
imgs = Table(
"img",
meta,
Column("id", VARCHAR(100), primary_key=True),
Column("input_path", VARCHAR(100)),
Column("output_path", VARCHAR(100)),
)

meta.create_all(engine)
meta.create_all(engine)
19 changes: 10 additions & 9 deletions app/db_models/user.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from sqlalchemy import Column, Table
from sqlalchemy.sql.sqltypes import INTEGER, String
from ..db_config.db import meta, engine

from ..db_config.db import engine, meta

users = Table('users', meta,
Column('id', INTEGER(), primary_key=True),
Column('name', String(255)),
Column('email', String(255)),
Column('password', String(255)),
)
users = Table(
"users",
meta,
Column("id", INTEGER(), primary_key=True),
Column("name", String(255)),
Column("email", String(255)),
Column("password", String(255)),
)



meta.create_all(engine)
meta.create_all(engine)
8 changes: 3 additions & 5 deletions app/frontend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import streamlit as st


def intro():
import streamlit as st

Expand All @@ -10,15 +7,16 @@ def intro():
st.markdown(
"""
**👈 Select a demo from the left**
**👈 Select a demo from the left**
### What kind of work do you want to do?
- Image
- Video
"""
)



page_names_to_funcs = {
"frontend": intro,
}
Expand Down
128 changes: 74 additions & 54 deletions app/pages/1_📷 Image.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import datetime as dt
import io
import logging
from uuid import uuid4

import httpx
import streamlit as st
from config import read_config
from PIL import Image
import io
import httpx
from uuid import UUID, uuid4
import datetime as dt



log = logging.getLogger(__file__)

Expand All @@ -16,31 +15,35 @@
CONFIG = read_config()
BASE_URL = f"http://localhost:{CONFIG['server']['port']}"


def make_database_dir():
import os

_input_path = CONFIG["image_input_path"]["path"]
_output_path = CONFIG["image_output_path"]["path"]

def _mkdir(path):
if not os.path.exists(path):
os.makedirs(path)

_mkdir(_input_path)
_mkdir(_output_path)



def get_models() -> dict:
if st.session_state["models"] is None:
log.info("get_models()")
r = httpx.get(BASE_URL + "/models")
models = r.json()
st.session_state["models"] = models
else:
#log.info("get_models22()")
# log.info("get_models22()")
models = st.session_state["models"]
return [model["name"] for model in models]


def session_init():
print('session init')
print("session init")
if "inferenced" not in st.session_state:
st.session_state["inferenced"] = None
if "models" not in st.session_state:
Expand All @@ -51,98 +54,115 @@ def session_init():
st.session_state["name"] = None
if "image_input_path" not in st.session_state:
st.session_state["image_input_path"] = None



def inference(model_name: str, media_filepath: str) -> str:
log.info(f"inference(model_name={model_name!r}, media_filepath={media_filepath!r})")

# 현재 페이지 상단에 spinner가 뜬다.
with st.spinner("Predicting results. Please wait a moment!"):
r = httpx.post(
BASE_URL + "/inference",
json={"model_name": model_name, "media_filepath": media_filepath},
timeout = None) # timeout 설정

timeout=None,
) # timeout 설정

log.info(f"inference: r.json() = {r.json()!r}")
if r.status_code == 200:
output_filepath = r.json()
log.info(f"inference: output_filepath = {output_filepath!r}")
st.session_state["inferenced"] = output_filepath



def mysql_image_insert(id: str, input_path: str, output_path: str):
r = httpx.post(BASE_URL+ "/img/insert",
json={"id": id, "input_path": input_path, "output_path": output_path},
timeout=None)
r = httpx.post(
BASE_URL + "/img/insert",
json={"id": id, "input_path": input_path, "output_path": output_path},
timeout=None,
)
if r.status_code == 200:
log.info("mysql data save success")
else:
log.error("mysql data save fail")



def insert_gcs(path: str, name: str, flag: str):
r = httpx.post(BASE_URL+"/img/gcs",
json={"path": path, "name": name},
timeout=None)
r = httpx.post(
BASE_URL + "/img/gcs", json={"path": path, "name": name}, timeout=None
)
if r.status_code == 200:
log.info(f"gcs {flag} image save success")
else:
log.info(f"gcs {flag} image save success")



def main():
st.title("Sixth Sense Image Demo Page")

# session 초기화
session_init()
session_init()
make_database_dir()

models = get_models()
model_name = st.selectbox("Model List", models)

uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])

col1, col2 = st.columns(2)

if uploaded_file:
image_bytes = uploaded_file.getvalue()
image = Image.open(io.BytesIO(image_bytes))

with col1:
st.image(image, caption='Uploaded Image')
st.image(image, caption="Uploaded Image")

if st.session_state["upload_file"] is None:
# input 이미지 서버에 저장
name = dt.datetime.today().strftime("%Y%m%d") + '_' + str(uuid4()) # 날짜_uuid4
name = (
dt.datetime.today().strftime("%Y%m%d") + "_" + str(uuid4())
) # 날짜_uuid4
image_input_path = f'{CONFIG["image_input_path"]["path"]}/{name}.jpg'
image.save(image_input_path, 'JPEG')

st.button("Inference",
on_click=inference,
kwargs={"model_name": model_name, "media_filepath": image_input_path})

image.save(image_input_path, "JPEG")

st.button(
"Inference",
on_click=inference,
kwargs={
"model_name": model_name,
"media_filepath": image_input_path,
},
)

# input 이미지 gcs에 저장
insert_gcs(image_input_path, f'{name}.jpg', 'input')
insert_gcs(image_input_path, f"{name}.jpg", "input")

st.session_state["upload_file"] = True
st.session_state["name"] = name
st.session_state["image_input_path"] = image_input_path

with col2:
if st.session_state["inferenced"] is not None:
st.image(st.session_state["inferenced"], caption='Infenece Image')
st.image(st.session_state["inferenced"], caption="Infenece Image")
# output 이미지 gcs에 저장
insert_gcs(st.session_state["inferenced"], f'inferenced_{st.session_state["name"]}.jpg', 'output')
insert_gcs(
st.session_state["inferenced"],
f'inferenced_{st.session_state["name"]}.jpg',
"output",
)
# mysql 이미지 data 저장
mysql_image_insert(st.session_state["name"], f'{CONFIG["google"]["path"]}{CONFIG["google"]["bucket"]}/{st.session_state["name"]}.jpg',
f'{CONFIG["google"]["path"]}{CONFIG["google"]["bucket"]}/inferenced_{st.session_state["name"]}.jpg')

mysql_image_insert(
st.session_state["name"],
f'{CONFIG["google"]["path"]}{CONFIG["google"]["bucket"]}/{st.session_state["name"]}.jpg',
f'{CONFIG["google"]["path"]}{CONFIG["google"]["bucket"]}/inferenced_{st.session_state["name"]}.jpg',
)

else:
del st.session_state["upload_file"]
del st.session_state["inferenced"]
del st.session_state["name"]
del st.session_state["image_input_path"]






if __name__ == '__main__':

if __name__ == "__main__":
main()
Loading

0 comments on commit 248cb26

Please sign in to comment.