Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,14 @@
|
|
| 1 |
-
import
|
| 2 |
-
import streamlit as st
|
| 3 |
import requests
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
-
from copy import deepcopy
|
| 7 |
-
from streamlit_drawable_canvas import st_canvas
|
| 8 |
|
| 9 |
-
# Hàm
|
| 10 |
def process_inpaint_via_api(image, mask):
|
| 11 |
url = "https://api.magicstudio.com/magiceraser/erase"
|
|
|
|
| 12 |
headers = {
|
| 13 |
-
'accessToken': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.
|
| 14 |
}
|
| 15 |
|
| 16 |
# Chuyển đổi ảnh và mask thành định dạng PNG để gửi tới API
|
|
@@ -36,77 +34,38 @@ def process_inpaint_via_api(image, mask):
|
|
| 36 |
return Image.open(BytesIO(response.content))
|
| 37 |
else:
|
| 38 |
# Nếu có lỗi, hiển thị lỗi
|
| 39 |
-
|
| 40 |
-
return None
|
| 41 |
-
|
| 42 |
-
# Thiết lập giao diện Streamlit
|
| 43 |
-
st.title("AI Photo Object Removal")
|
| 44 |
-
|
| 45 |
-
# Tải ảnh từ người dùng
|
| 46 |
-
uploaded_file = st.file_uploader("Choose image", accept_multiple_files=False, type=["png", "jpg", "jpeg"])
|
| 47 |
-
|
| 48 |
-
if uploaded_file is not None:
|
| 49 |
-
# Đọc ảnh từ người dùng
|
| 50 |
-
img_input = Image.open(uploaded_file).convert("RGBA")
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
if img_width > img_height:
|
| 57 |
-
new_width = max_size
|
| 58 |
-
new_height = int((max_size / img_width) * img_height)
|
| 59 |
-
else:
|
| 60 |
-
new_height = max_size
|
| 61 |
-
new_width = int((max_size / img_height) * img_width)
|
| 62 |
-
img_input = img_input.resize((new_width, new_height))
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
st.write("**Now draw (brush) the part of image that you want to remove.**")
|
| 68 |
-
|
| 69 |
-
# Canvas để người dùng vẽ mask trên ảnh
|
| 70 |
-
canvas_bg = deepcopy(img_input)
|
| 71 |
-
aspect_ratio = canvas_bg.width / canvas_bg.height
|
| 72 |
-
streamlit_width = 720
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
if
|
| 76 |
-
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
drawing_mode="freedraw", # Chế độ vẽ tự do
|
| 85 |
-
key="canvas"
|
| 86 |
-
)
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
im = np.array(Image.fromarray(canvas_result.image_data.astype(np.uint8)).resize(img_input.size))
|
| 92 |
-
background = np.where(
|
| 93 |
-
(im[:, :, 0] == 0) &
|
| 94 |
-
(im[:, :, 1] == 0) &
|
| 95 |
-
(im[:, :, 2] == 0)
|
| 96 |
-
)
|
| 97 |
-
drawing = np.where(
|
| 98 |
-
(im[:, :, 0] == 255) &
|
| 99 |
-
(im[:, :, 1] == 0) &
|
| 100 |
-
(im[:, :, 2] == 255)
|
| 101 |
-
)
|
| 102 |
-
im[background] = [0, 0, 0, 255] # Đặt nền thành đen
|
| 103 |
-
im[drawing] = [0, 0, 0, 0] # Đặt mask thành trong suốt (alpha)
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
st.write("AI has finished the job!")
|
| 112 |
-
st.image(img_output)
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
import requests
|
| 3 |
from PIL import Image
|
| 4 |
from io import BytesIO
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Hàm xử lý gửi ảnh và mask tới API Magic Studio
|
| 7 |
def process_inpaint_via_api(image, mask):
|
| 8 |
url = "https://api.magicstudio.com/magiceraser/erase"
|
| 9 |
+
|
| 10 |
headers = {
|
| 11 |
+
'accessToken': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjbGllbnRfaWQiOiIyRGZQSm1sY1BLalBuaGh4ajBxSVpmQktfbS12RFhTc1NMRFNOX1gtZlVVIiwiZXhwIjoxNzI3MTY0MTcyLCJhcHBfbmFtZSI6IjE3NTMwODQiLCJtZXRhbnRfdHlwZSI6ImNyZWQifQ.24oHGH9ial_gqeYrpUbYP68MwYly2ArzXbhbE1hGzQo'
|
| 12 |
}
|
| 13 |
|
| 14 |
# Chuyển đổi ảnh và mask thành định dạng PNG để gửi tới API
|
|
|
|
| 34 |
return Image.open(BytesIO(response.content))
|
| 35 |
else:
|
| 36 |
# Nếu có lỗi, hiển thị lỗi
|
| 37 |
+
return f"API Error: {response.status_code} - {response.text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Hàm xử lý chính trong Gradio
|
| 40 |
+
def process(image, mask):
|
| 41 |
+
img = Image.fromarray(image)
|
| 42 |
+
mask_img = Image.fromarray(mask)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# Gọi API với ảnh và mask
|
| 45 |
+
result = process_inpaint_via_api(img, mask_img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# Nếu kết quả trả về là lỗi, hiển thị thông báo
|
| 48 |
+
if isinstance(result, str):
|
| 49 |
+
return None, result
|
| 50 |
|
| 51 |
+
# Trả về ảnh đã xử lý
|
| 52 |
+
return result, "Processing complete."
|
| 53 |
+
|
| 54 |
+
# Tạo giao diện Gradio
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
gr.Markdown("# AI Object Removal using Magic Studio API")
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Khu vực tải lên ảnh và vẽ mask
|
| 59 |
+
with gr.Row():
|
| 60 |
+
image_input = gr.Image(source="upload", tool="sketch", type="numpy", label="Upload an image and draw the mask")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# Khu vực hiển thị kết quả
|
| 63 |
+
result_output = gr.Image(label="Output Image")
|
| 64 |
+
output_message = gr.Textbox(label="Message", lines=2)
|
| 65 |
+
|
| 66 |
+
# Nút xử lý và kết nối các thành phần với hàm process
|
| 67 |
+
process_button = gr.Button("Submit")
|
| 68 |
+
process_button.click(process, inputs=[image_input, image_input], outputs=[result_output, output_message])
|
| 69 |
|
| 70 |
+
# Khởi chạy ứng dụng Gradio
|
| 71 |
+
demo.launch()
|
|
|
|
|
|