Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
import numpy as np
|
| 6 |
|
|
@@ -37,13 +37,16 @@ def process_inpaint_via_api(image, mask):
|
|
| 37 |
# Nếu có lỗi, hiển thị lỗi
|
| 38 |
return f"API Error: {response.status_code} - {response.text}"
|
| 39 |
|
| 40 |
-
# Hàm xử lý
|
| 41 |
def process(image, mask):
|
| 42 |
-
img = Image.fromarray(image)
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# Gọi API với ảnh và mask
|
| 46 |
-
result = process_inpaint_via_api(img,
|
| 47 |
|
| 48 |
# Nếu kết quả trả về là lỗi, hiển thị thông báo
|
| 49 |
if isinstance(result, str):
|
|
@@ -56,11 +59,8 @@ def process(image, mask):
|
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
gr.Markdown("# AI Object Removal using Magic Studio API")
|
| 58 |
|
| 59 |
-
# Tải ảnh lên từ người dùng
|
| 60 |
-
image_input = gr.Image(type="numpy", label="Upload an image")
|
| 61 |
-
|
| 62 |
-
# Cho phép vẽ mask đè trực tiếp lên ảnh đã tải lên
|
| 63 |
-
mask_input = gr.Image(type="numpy", tool="sketch", label="Draw the mask on the image")
|
| 64 |
|
| 65 |
# Khu vực hiển thị kết quả
|
| 66 |
result_output = gr.Image(label="Output Image")
|
|
@@ -68,7 +68,7 @@ with gr.Blocks() as demo:
|
|
| 68 |
|
| 69 |
# Nút xử lý và kết nối các thành phần với hàm process
|
| 70 |
process_button = gr.Button("Submit")
|
| 71 |
-
process_button.click(process, inputs=[image_input,
|
| 72 |
|
| 73 |
# Khởi chạy ứng dụng Gradio
|
| 74 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
from PIL import Image, ImageOps
|
| 4 |
from io import BytesIO
|
| 5 |
import numpy as np
|
| 6 |
|
|
|
|
| 37 |
# Nếu có lỗi, hiển thị lỗi
|
| 38 |
return f"API Error: {response.status_code} - {response.text}"
|
| 39 |
|
| 40 |
+
# Hàm xử lý kết hợp ảnh và mask
|
| 41 |
def process(image, mask):
|
| 42 |
+
img = Image.fromarray(image).convert("RGBA")
|
| 43 |
+
|
| 44 |
+
# Mask nhận được từ Image Sketchpad dưới dạng ảnh RGBA, cần chuyển sang ảnh nhị phân
|
| 45 |
+
mask = Image.fromarray(mask).convert("L")
|
| 46 |
+
mask = ImageOps.invert(mask) # Invert lại mask để đảo ngược vùng vẽ
|
| 47 |
|
| 48 |
# Gọi API với ảnh và mask
|
| 49 |
+
result = process_inpaint_via_api(img, mask)
|
| 50 |
|
| 51 |
# Nếu kết quả trả về là lỗi, hiển thị thông báo
|
| 52 |
if isinstance(result, str):
|
|
|
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
gr.Markdown("# AI Object Removal using Magic Studio API")
|
| 61 |
|
| 62 |
+
# Tải ảnh lên từ người dùng và cho phép vẽ mask đè trực tiếp lên ảnh đã tải lên
|
| 63 |
+
image_input = gr.Image(type="numpy", tool="sketch", label="Upload an image and draw mask")
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# Khu vực hiển thị kết quả
|
| 66 |
result_output = gr.Image(label="Output Image")
|
|
|
|
| 68 |
|
| 69 |
# Nút xử lý và kết nối các thành phần với hàm process
|
| 70 |
process_button = gr.Button("Submit")
|
| 71 |
+
process_button.click(process, inputs=[image_input, image_input], outputs=[result_output, output_message])
|
| 72 |
|
| 73 |
# Khởi chạy ứng dụng Gradio
|
| 74 |
demo.launch()
|