Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,112 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import requests
|
| 3 |
from PIL import Image
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Hàm
|
| 7 |
-
def
|
| 8 |
url = "https://api.magicstudio.com/magiceraser/erase"
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Chuyển đổi
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
headers = {'accessToken': access_token}
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
if response.status_code == 200:
|
| 24 |
-
#
|
| 25 |
-
return
|
| 26 |
else:
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# Hàm xử lý cho Gradio
|
| 31 |
-
def process(image, mask):
|
| 32 |
-
# Chuyển đổi hình ảnh từ Gradio thành định dạng 'bytes'
|
| 33 |
-
image_bytes = image.convert("RGB").tobytes()
|
| 34 |
-
mask_bytes = mask.convert("RGB").tobytes()
|
| 35 |
-
|
| 36 |
-
# Gọi API với hình ảnh và mask (token được cố định trong code)
|
| 37 |
-
access_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjbGllbnRfaWQiOiIyRGZQSm1sY1BLalBuaGh4ajBxSVpmQktfbS12RFhTc1NMRFNOX1gtZlVVIiwiZXhwIjoxNzI3MTY0MTcyLCJhcHBfbmFtZSI6IjE3NTMwODQiLCJtZXRhZGF0YSI6bnVsbCwiZ3JhbnRfdHlwZSI6ImNyZWQifQ.24oHGH9ial_gqeYrpUbYP68MwYly2ArzXbhbE1hGzQo'
|
| 38 |
-
|
| 39 |
-
result = erase_object(image_bytes, mask_bytes, access_token)
|
| 40 |
-
|
| 41 |
-
return result
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
gr.Markdown("# Magic Eraser - Xóa đối tượng khỏi ảnh")
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
image_input = gr.Image(type="pil", label="Tải ảnh lên")
|
| 50 |
-
mask_input = gr.Sketchpad(shape=(512, 512), label="Vẽ mask để xóa đối tượng")
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 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 để tải ảnh và mask lên API Magic Studio
|
| 10 |
+
def process_inpaint_via_api(image, mask):
|
| 11 |
url = "https://api.magicstudio.com/magiceraser/erase"
|
| 12 |
+
headers = {
|
| 13 |
+
'accessToken': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjbGllbnRfaWQiOiIyRGZQSm1sY1BLalBuaGh4ajBxSVpmQktfbS12RFhTc1NMRFNOX1gtZlVVIiwiZXhwIjoxNzI3MTY0MTcyLCJhcHBfbmFtZSI6IjE3NTMwODQiLCJtZXRhZGF0YSI6bnVsbCwiZ3JhbnRfdHlwZSI6ImNyZWQifQ.24oHGH9ial_gqeYrpUbYP68MwYly2ArzXbhbE1hGzQo'
|
| 14 |
+
}
|
| 15 |
|
| 16 |
+
# Chuyển đổi ảnh và mask thành định dạng PNG để gửi tới API
|
| 17 |
+
image_io = BytesIO()
|
| 18 |
+
mask_io = BytesIO()
|
| 19 |
|
| 20 |
+
image.save(image_io, format="PNG")
|
| 21 |
+
mask.save(mask_io, format="PNG")
|
| 22 |
|
| 23 |
+
image_io.seek(0)
|
| 24 |
+
mask_io.seek(0)
|
|
|
|
| 25 |
|
| 26 |
+
files = [
|
| 27 |
+
('image_file', ('image.png', image_io, 'image/png')),
|
| 28 |
+
('mask_file', ('mask.png', mask_io, 'image/png'))
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
# Gửi yêu cầu POST tới API
|
| 32 |
+
response = requests.post(url, headers=headers, files=files)
|
| 33 |
|
| 34 |
if response.status_code == 200:
|
| 35 |
+
# Nếu API trả về thành công, xử lý và trả về ảnh kết quả
|
| 36 |
+
return Image.open(BytesIO(response.content))
|
| 37 |
else:
|
| 38 |
+
# Nếu có lỗi, hiển thị lỗi
|
| 39 |
+
st.error(f"API Error: {response.status_code} - {response.text}")
|
| 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 |
+
# Resize ảnh để phù hợp với giao diện
|
| 53 |
+
max_size = 2000
|
| 54 |
+
img_width, img_height = img_input.size
|
| 55 |
+
if img_width > max_size or img_height > max_size:
|
| 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 |
+
# Lựa chọn kích thước cọ vẽ
|
| 65 |
+
stroke_width = st.slider("Brush size", 1, 100, 50)
|
| 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 |
+
# Resize ảnh nền để hiển thị đúng kích thước trên Streamlit
|
| 75 |
+
if canvas_bg.width > streamlit_width:
|
| 76 |
+
canvas_bg = canvas_bg.resize((streamlit_width, int(streamlit_width / aspect_ratio)))
|
| 77 |
+
|
| 78 |
+
canvas_result = st_canvas(
|
| 79 |
+
stroke_color="rgba(255, 0, 255, 1)", # Màu của cọ vẽ mask
|
| 80 |
+
stroke_width=stroke_width, # Độ dày cọ vẽ
|
| 81 |
+
background_image=canvas_bg, # Ảnh nền
|
| 82 |
+
width=canvas_bg.width,
|
| 83 |
+
height=canvas_bg.height,
|
| 84 |
+
drawing_mode="freedraw", # Chế độ vẽ tự do
|
| 85 |
+
key="canvas"
|
| 86 |
)
|
| 87 |
+
|
| 88 |
+
# Xử lý khi người dùng đã vẽ mask và nhấn nút Submit
|
| 89 |
+
if canvas_result.image_data is not None:
|
| 90 |
+
# Chuyển đổi dữ liệu từ canvas thành ảnh
|
| 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 |
+
if st.button('Submit'):
|
| 106 |
+
with st.spinner("AI is doing the magic!"):
|
| 107 |
+
img_output = process_inpaint_via_api(img_input, Image.fromarray(im)) # Gọi API xử lý inpainting
|
| 108 |
|
| 109 |
+
# Hiển thị ảnh kết quả sau khi xử lý
|
| 110 |
+
if img_output is not None:
|
| 111 |
+
st.write("AI has finished the job!")
|
| 112 |
+
st.image(img_output)
|