Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import base64
|
| 3 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
-
def
|
| 7 |
if image is None:
|
| 8 |
raise gr.Error('No input image')
|
| 9 |
|
|
@@ -11,18 +14,41 @@ def run(image):
|
|
| 11 |
image.save(buffer, format='PNG')
|
| 12 |
img_str = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 13 |
return img_str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
with gr.Blocks() as demo:
|
| 16 |
-
with gr.
|
| 17 |
-
with gr.
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
inputs=[image],
|
| 25 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
demo.launch(show_error=True)
|
|
|
|
|
|
|
| 1 |
import base64
|
| 2 |
from io import BytesIO
|
| 3 |
+
import cv2
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import gradio as gr
|
| 7 |
|
| 8 |
|
| 9 |
+
def encode(image):
|
| 10 |
if image is None:
|
| 11 |
raise gr.Error('No input image')
|
| 12 |
|
|
|
|
| 14 |
image.save(buffer, format='PNG')
|
| 15 |
img_str = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 16 |
return img_str
|
| 17 |
+
|
| 18 |
+
def decode(image_str):
|
| 19 |
+
if image_str is None:
|
| 20 |
+
raise gr.Error('No input image string')
|
| 21 |
+
|
| 22 |
+
image_byte = base64.b64deocde(image_str)
|
| 23 |
+
image = cv2.imdecode(np.frombuffer(image_byte, np.uint8), cv2.IMREAD_UNCHANGED)
|
| 24 |
+
image = Image.fromarray(image[:,:,::-1])
|
| 25 |
+
return image
|
| 26 |
|
| 27 |
with gr.Blocks() as demo:
|
| 28 |
+
with gr.Tab():
|
| 29 |
+
with gr.Row():
|
| 30 |
+
with gr.Column():
|
| 31 |
+
image = gr.Image(type='pil', interactive=True)
|
| 32 |
+
btn_enc = gr.Button()
|
| 33 |
+
output_image_str = gr.Textbox(label='image string', interactive=False)
|
| 34 |
+
with gr.Tab():
|
| 35 |
+
with gr.Row():
|
| 36 |
+
with gr.Column():
|
| 37 |
+
image_str = gr.Textbox(label='image string', interactive=True)
|
| 38 |
+
btn_dec = gr.Button()
|
| 39 |
+
output_image = gr.Image(type='pil', interactive=True)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
btn_enc.click(
|
| 43 |
+
fn=encode,
|
| 44 |
inputs=[image],
|
| 45 |
+
outputs=[output_image_str],
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
btn_dec.click(
|
| 49 |
+
fn=decode,
|
| 50 |
+
inputs=[image_str],
|
| 51 |
+
outputs=[output_image],
|
| 52 |
)
|
| 53 |
|
| 54 |
demo.launch(show_error=True)
|