uyen22 commited on
Commit
efe0d17
·
verified ·
1 Parent(s): 60d2cbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -73
app.py CHANGED
@@ -1,16 +1,14 @@
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
@@ -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
- 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)
 
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()