Update app.py
Browse files
app.py
CHANGED
@@ -9,12 +9,35 @@ import time
|
|
9 |
from google import genai
|
10 |
from google.genai import types
|
11 |
from dotenv import load_dotenv
|
|
|
|
|
12 |
load_dotenv()
|
13 |
|
14 |
# 기존 코드 유지 (로깅, 함수 등 모든 기능 코드)
|
15 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
16 |
logger = logging.getLogger(__name__)
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def save_binary_file(file_name, data):
|
19 |
with open(file_name, "wb") as f:
|
20 |
f.write(data)
|
@@ -837,6 +860,33 @@ with gr.Blocks(css=custom_css) as demo:
|
|
837 |
gfpgan_output = gr.Image(type="numpy", label="업스케일 결과", elem_classes="image-container")
|
838 |
|
839 |
gfpgan_btn = gr.Button('✨ 업스케일 및 복원', elem_classes="custom-button primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
840 |
|
841 |
# 버튼 이벤트 연결
|
842 |
image_change_btn1.click(
|
@@ -895,6 +945,13 @@ with gr.Blocks(css=custom_css) as demo:
|
|
895 |
inputs=gfpgan_input,
|
896 |
outputs=gfpgan_output
|
897 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
898 |
|
899 |
demo.queue()
|
900 |
demo.launch()
|
|
|
9 |
from google import genai
|
10 |
from google.genai import types
|
11 |
from dotenv import load_dotenv
|
12 |
+
from transformers import pipeline # 새로 추가된 import
|
13 |
+
|
14 |
load_dotenv()
|
15 |
|
16 |
# 기존 코드 유지 (로깅, 함수 등 모든 기능 코드)
|
17 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
18 |
logger = logging.getLogger(__name__)
|
19 |
|
20 |
+
# 배경 제거 기능 추가
|
21 |
+
def remove_background(image):
|
22 |
+
if image is None:
|
23 |
+
return None, "이미지가 업로드되지 않았습니다."
|
24 |
+
|
25 |
+
try:
|
26 |
+
logger.info("배경 제거 시작")
|
27 |
+
|
28 |
+
# 모델을 처음 로드할 때는 다운로드할 수 있으므로 시간이 걸릴 수 있음
|
29 |
+
pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True)
|
30 |
+
|
31 |
+
# 배경이 제거된 이미지 가져오기
|
32 |
+
output_image = pipe(image)
|
33 |
+
|
34 |
+
logger.info("배경 제거 완료")
|
35 |
+
return output_image, "배경이 성공적으로 제거되었습니다."
|
36 |
+
except Exception as e:
|
37 |
+
logger.exception("배경 제거 중 오류 발생:")
|
38 |
+
return None, f"오류 발생: {str(e)}"
|
39 |
+
|
40 |
+
|
41 |
def save_binary_file(file_name, data):
|
42 |
with open(file_name, "wb") as f:
|
43 |
f.write(data)
|
|
|
860 |
gfpgan_output = gr.Image(type="numpy", label="업스케일 결과", elem_classes="image-container")
|
861 |
|
862 |
gfpgan_btn = gr.Button('✨ 업스케일 및 복원', elem_classes="custom-button primary")
|
863 |
+
|
864 |
+
# 새로운 탭: 배경 지우기 기능
|
865 |
+
with gr.TabItem("✂️ 배경 지우기", elem_classes="tab-content"):
|
866 |
+
with gr.Row():
|
867 |
+
with gr.Column(elem_classes="panel"):
|
868 |
+
gr.HTML('<div class="section-title"><i class="fas fa-cut"></i> 이미지 배경 제거</div>')
|
869 |
+
gr.HTML("""
|
870 |
+
<p style="margin-bottom: 1rem; font-size: 1.05rem;">
|
871 |
+
이미지에서 배경을 자동으로 제거하여 투명한 PNG 이미지로 변환합니다.
|
872 |
+
</p>
|
873 |
+
<div class="tip-box">
|
874 |
+
<i class="fas fa-lightbulb"></i> <strong>팁:</strong> 배경과 주요 피사체 간의 대비가 뚜렷할수록 더 나은 결과를 얻을 수 있습니다.
|
875 |
+
</div>
|
876 |
+
""")
|
877 |
+
|
878 |
+
with gr.Row():
|
879 |
+
with gr.Column():
|
880 |
+
gr.HTML('<div class="section-title"><i class="fas fa-upload"></i> 입력 이미지</div>')
|
881 |
+
bg_remove_input = gr.Image(type="pil", label="업로드", elem_classes="image-container")
|
882 |
+
|
883 |
+
with gr.Column():
|
884 |
+
gr.HTML('<div class="section-title"><i class="fas fa-download"></i> 결과 이미지</div>')
|
885 |
+
bg_remove_output = gr.Image(type="pil", label="배경 제거 결과", elem_classes="image-container", format="png")
|
886 |
+
|
887 |
+
with gr.Row():
|
888 |
+
bg_remove_status = gr.Textbox(label="상태", lines=2, elem_classes="custom-input")
|
889 |
+
bg_remove_btn = gr.Button('✂️ 배경 제거하기', elem_classes="custom-button primary")
|
890 |
|
891 |
# 버튼 이벤트 연결
|
892 |
image_change_btn1.click(
|
|
|
945 |
inputs=gfpgan_input,
|
946 |
outputs=gfpgan_output
|
947 |
)
|
948 |
+
|
949 |
+
# 배경 제거 버튼 이벤트 연결
|
950 |
+
bg_remove_btn.click(
|
951 |
+
fn=remove_background,
|
952 |
+
inputs=bg_remove_input,
|
953 |
+
outputs=[bg_remove_output, bg_remove_status]
|
954 |
+
)
|
955 |
|
956 |
demo.queue()
|
957 |
demo.launch()
|