Skip to content

Commit

Permalink
Fix #99 add url data template
Browse files Browse the repository at this point in the history
  • Loading branch information
yanfeng-li committed Jun 7, 2022
1 parent a6d2a63 commit ef29c38
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
40 changes: 40 additions & 0 deletions pinferencia/frontend/templates/url_image_to_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import base64

import streamlit as st
from PIL import Image

from .base import BaseTemplate
from .utils import display_text_prediction


class Template(BaseTemplate):

This comment has been minimized.

Copy link
@wjiuhe

wjiuhe Jun 12, 2022

Member

最好新开一个pull request来做这个,这样commit会比较清晰,也可以快点把部分代码merge了。

title = (
'<span style="color:salmon;">Image</span> '
'<span style="color:slategray;">to</span> '
'<span style="color:lightseagreen;">Text</span>'
)

def render(self):
super().render()
with st.form("Image Upload", clear_on_submit=True):
uploaded_file = st.file_uploader(
"Choose an image...", type=["jpg", "png", "jpeg"]
)
st.form_submit_button("Upload and Run")

col1, col2 = st.columns(2)
col1.markdown(
'<h3 style="text-align: center;">Input</h3>',
unsafe_allow_html=True,
)
col2.markdown(
'<h3 style="text-align: center;">Result</h3>',
unsafe_allow_html=True,
)
if uploaded_file is not None:
image = Image.open(uploaded_file)
col1.image(image, use_column_width=True)
base64_img_str = base64.b64encode(uploaded_file.getvalue()).decode()
with st.spinner("Waiting for result"):
prediction = self.auto_predict({"base64_img_str": base64_img_str})
display_text_prediction(prediction, component=col2)
2 changes: 2 additions & 0 deletions pinferencia/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
IMAGE_TO_IMAGE = "image_to_image"
TEXT_TO_TEXT = "text_to_text"
TEXT_TO_IMAGE = "text_to_image"
URL_IMAGE_TO_TEXT = "url_image_to_text"

# Specific Task
TRANSLATION = "translation"
Expand All @@ -25,4 +26,5 @@
IMAGE_TO_IMAGE,
TEXT_TO_TEXT,
TEXT_TO_IMAGE,
URL_IMAGE_TO_TEXT,
)
14 changes: 14 additions & 0 deletions pinferencia/tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import base64
import cv2

This comment has been minimized.

Copy link
@wjiuhe

wjiuhe Jun 12, 2022

Member

cv2不是默认依赖。默认不安装。这个地方最好先不要加。

This comment has been minimized.

Copy link
@wjiuhe

wjiuhe Jun 12, 2022

Member

如果要加的话,最好是try一下,然后测试那里要记得改

This comment has been minimized.

Copy link
@wjiuhe

wjiuhe Jun 12, 2022

Member

这个加cv2最好也单开一个issue和pull request。

import numpy as np
from io import BytesIO

from PIL import Image
Expand All @@ -12,3 +14,15 @@ def pil_image_to_base64_str(image: Image) -> str:

def base64_str_to_pil_image(base64_str: str) -> Image:
return Image.open(BytesIO(base64.b64decode(base64_str)))


def base64_str_to_cv2(base64_str: str) -> np.ndarray:
img_string = base64.b64decode(base64_str)
nparr = np.fromstring(img_string, np.uint8)
return cv2.imdecode(nparr, cv2.IMREAD_COLOR)


def cv2_to_base64_str(image: np.ndarray) -> str:
base64_str = cv2.imencode(".jpg", image)[1].tostring()
base64_str = base64.b64encode(base64_str)
return base64_str

0 comments on commit ef29c38

Please sign in to comment.