Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ from PIL import Image
|
|
4 |
import numpy as np
|
5 |
import torch
|
6 |
from transformers import pipeline
|
7 |
-
import cv2
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
@@ -12,48 +12,59 @@ app = FastAPI()
|
|
12 |
# ๋ชจ๋ธ ๋ก๋ฉ์ ์ฑ ์์ ์ ํ ๋ฒ๋ง ํ๋๋ก ๊ธ๋ก๋ฒ ๋ณ์๋ก ์ค์
|
13 |
print("Loading Depth Anything model...")
|
14 |
try:
|
15 |
-
|
|
|
|
|
16 |
print("Depth Anything model loaded successfully.")
|
17 |
except Exception as e:
|
18 |
print(f"Error loading Depth Anything model: {e}")
|
19 |
depth_estimator = None # ๋ชจ๋ธ ๋ก๋ ์คํจ ์ None์ผ๋ก ์ค์
|
20 |
|
21 |
def process_image_for_depth(image_path_or_pil_image):
|
|
|
|
|
|
|
22 |
if depth_estimator is None:
|
23 |
-
return None, "Error: Depth Anything model not loaded."
|
24 |
|
25 |
# Gradio๋ PIL Image ๊ฐ์ฒด๋ก ์ด๋ฏธ์ง๋ฅผ ์ ๋ฌํฉ๋๋ค.
|
26 |
if isinstance(image_path_or_pil_image, str):
|
|
|
27 |
image = Image.open(image_path_or_pil_image).convert("RGB")
|
28 |
else:
|
|
|
29 |
image = image_path_or_pil_image.convert("RGB")
|
30 |
|
31 |
try:
|
32 |
# Depth Anything ๋ชจ๋ธ ์ถ๋ก
|
33 |
-
# result๋ ๋์
๋๋ฆฌ๋ก, 'depth' (PIL Image)์ 'depth_npy' (numpy array)๋ฅผ
|
34 |
result = depth_estimator(image)
|
35 |
|
36 |
-
# ๋์ค ๋งต (PIL Image)
|
37 |
depth_image_pil = result["depth"]
|
38 |
|
39 |
-
# ๋์ค ๋งต (Numpy Array) -
|
40 |
-
|
41 |
-
|
42 |
-
normalized_depth_np =
|
43 |
-
|
44 |
-
#
|
45 |
-
depth_grayscale_pil = Image.fromarray(normalized_depth_np)
|
46 |
|
47 |
-
return
|
48 |
except Exception as e:
|
|
|
|
|
49 |
return None, f"Error processing image for depth: {e}"
|
50 |
|
51 |
# Gradio ์ธํฐํ์ด์ค ์ ์
|
52 |
with gr.Blocks() as demo:
|
53 |
gr.Markdown("# ๐งโ๐ป ์ผ๊ตด ๋์ค ๋งต ์ถ์ถ๊ธฐ")
|
54 |
gr.Markdown("์ฌ๋ฌ ์ฅ์ ์ผ๊ตด ์ฌ์ง์ ์
๋ก๋ํ๋ฉด ๊ฐ ์ฌ์ง์์ ๋ฅ๋ฌ๋์ ํตํด ๋์ค ๋งต(๊น์ด ์ ๋ณด)์ ์ถ์ถํฉ๋๋ค.")
|
|
|
55 |
|
56 |
with gr.Row():
|
|
|
|
|
57 |
input_images = gr.File(label="์ผ๊ตด ์ฌ์ง ์
๋ก๋ (์ต๋ 10์ฅ ๊ถ์ฅ)", file_count="multiple", type="filepath")
|
58 |
|
59 |
output_gallery = gr.Gallery(label="์๋ณธ ์ด๋ฏธ์ง ๋ฐ ๋์ค ๋งต", columns=[2], rows=[1], object_fit="contain", height="auto")
|
@@ -61,22 +72,41 @@ with gr.Blocks() as demo:
|
|
61 |
process_button = gr.Button("๋์ค ๋งต ์ถ์ถ ์์")
|
62 |
|
63 |
def process_all_images(image_paths):
|
|
|
|
|
|
|
64 |
if not image_paths:
|
65 |
return [(None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.")]
|
66 |
|
67 |
-
|
|
|
68 |
for i, path in enumerate(image_paths):
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
|
|
80 |
process_button.click(
|
81 |
fn=process_all_images,
|
82 |
inputs=input_images,
|
@@ -84,9 +114,11 @@ with gr.Blocks() as demo:
|
|
84 |
)
|
85 |
|
86 |
# Gradio ์ฑ์ FastAPI์ ๋ง์ดํธ
|
|
|
87 |
app = gr.mount_gradio_app(app, demo, path="/")
|
88 |
|
89 |
# FastAPI ๊ธฐ๋ณธ ์๋ํฌ์ธํธ (์ ํ ์ฌํญ, Gradio ์ฑ์ด ๊ธฐ๋ณธ ๊ฒฝ๋ก๋ฅผ ์ ์ ํจ)
|
|
|
90 |
@app.get("/api")
|
91 |
def read_root():
|
92 |
return {"message": "Welcome to the Face Depth Map Extractor! Visit / for the UI."}
|
|
|
4 |
import numpy as np
|
5 |
import torch
|
6 |
from transformers import pipeline
|
7 |
+
import cv2 # cv2๋ ์ด๋ฏธ์ง ๋ก๋/์ ์ฅ/์ ์ฒ๋ฆฌ ๋ฑ์ ์ฌ์ฉ๋ ์ ์์ง๋ง, ํ์ฌ ์ฝ๋์์๋ PIL๋ง์ผ๋ก๋ ์ถฉ๋ถํฉ๋๋ค.
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
|
|
12 |
# ๋ชจ๋ธ ๋ก๋ฉ์ ์ฑ ์์ ์ ํ ๋ฒ๋ง ํ๋๋ก ๊ธ๋ก๋ฒ ๋ณ์๋ก ์ค์
|
13 |
print("Loading Depth Anything model...")
|
14 |
try:
|
15 |
+
# ๐๐๐ ๋ชจ๋ธ ์ด๋ฆ ์์ : ์ ํํ Depth Anything v2 Large ๋ชจ๋ธ ID ๐๐๐
|
16 |
+
# ๋ ์์ ๋ชจ๋ธ์ ์ํ์๋ฉด "LiangNX/depth-anything-v2-base-nyu" ๋ก ๋ณ๊ฒฝํ์ธ์.
|
17 |
+
depth_estimator = pipeline(task="depth-estimation", model="LiangNX/depth-anything-v2-large-nyu", device="cpu") # GPU ์ฌ์ฉ ์ device="cuda"
|
18 |
print("Depth Anything model loaded successfully.")
|
19 |
except Exception as e:
|
20 |
print(f"Error loading Depth Anything model: {e}")
|
21 |
depth_estimator = None # ๋ชจ๋ธ ๋ก๋ ์คํจ ์ None์ผ๋ก ์ค์
|
22 |
|
23 |
def process_image_for_depth(image_path_or_pil_image):
|
24 |
+
"""
|
25 |
+
๋จ์ผ ์ด๋ฏธ์ง์์ ๋์ค ๋งต์ ์ถ์ถํ๋ ํจ์.
|
26 |
+
"""
|
27 |
if depth_estimator is None:
|
28 |
+
return None, "Error: Depth Anything model not loaded. Check server logs."
|
29 |
|
30 |
# Gradio๋ PIL Image ๊ฐ์ฒด๋ก ์ด๋ฏธ์ง๋ฅผ ์ ๋ฌํฉ๋๋ค.
|
31 |
if isinstance(image_path_or_pil_image, str):
|
32 |
+
# ํ์ผ ๊ฒฝ๋ก๋ก ์ด๋ฏธ์ง๊ฐ ์ ๋ฌ๋ ๊ฒฝ์ฐ (์: Gradio์ `filepath` ํ์
)
|
33 |
image = Image.open(image_path_or_pil_image).convert("RGB")
|
34 |
else:
|
35 |
+
# PIL Image ๊ฐ์ฒด๊ฐ ์ง์ ์ ๋ฌ๋ ๊ฒฝ์ฐ
|
36 |
image = image_path_or_pil_image.convert("RGB")
|
37 |
|
38 |
try:
|
39 |
# Depth Anything ๋ชจ๋ธ ์ถ๋ก
|
40 |
+
# result๋ ๋์
๋๋ฆฌ๋ก, 'depth' (PIL Image)์ 'depth_npy' (numpy array)๋ฅผ ํฌํจํฉ๋๋ค.
|
41 |
result = depth_estimator(image)
|
42 |
|
43 |
+
# ๋์ค ๋งต (PIL Image) - ์ด PIL ์ด๋ฏธ์ง๋ ์ด๋ฏธ ์๊ฐํํ๊ธฐ ์ข์ ํํ๋ก ๋์ด ์์ต๋๋ค.
|
44 |
depth_image_pil = result["depth"]
|
45 |
|
46 |
+
# ๋์ค ๋งต (Numpy Array) - ํ์ํ๋ค๋ฉด ์ถ๊ฐ ์ฒ๋ฆฌ (์: ์ ๊ทํ, ๋ค๋ฅธ ํ์์ผ๋ก ๋ณํ)
|
47 |
+
# ํ์ฌ๋ ํ๋ฐฑ ์ด๋ฏธ์ง๋ก ๋ฐ๋ก ์ฌ์ฉํด๋ ์ถฉ๋ถํ๋ฏ๋ก, ์๋ ์ฝ๋๋ ์ ํ ์ฌํญ์
๋๋ค.
|
48 |
+
# depth_np = result["depth_npy"]
|
49 |
+
# normalized_depth_np = (depth_np - depth_np.min()) / (depth_np.max() - depth_np.min()) * 255
|
50 |
+
# normalized_depth_np = normalized_depth_np.astype(np.uint8)
|
51 |
+
# depth_grayscale_pil = Image.fromarray(normalized_depth_np)
|
|
|
52 |
|
53 |
+
return depth_image_pil, None # ๋์ค ๋งต PIL Image ๋ฐํ
|
54 |
except Exception as e:
|
55 |
+
import traceback
|
56 |
+
traceback.print_exc() # ์๋ฌ ๋ฐ์ ์ ์์ธ ํธ๋ ์ด์ค๋ฐฑ ์ถ๋ ฅ
|
57 |
return None, f"Error processing image for depth: {e}"
|
58 |
|
59 |
# Gradio ์ธํฐํ์ด์ค ์ ์
|
60 |
with gr.Blocks() as demo:
|
61 |
gr.Markdown("# ๐งโ๐ป ์ผ๊ตด ๋์ค ๋งต ์ถ์ถ๊ธฐ")
|
62 |
gr.Markdown("์ฌ๋ฌ ์ฅ์ ์ผ๊ตด ์ฌ์ง์ ์
๋ก๋ํ๋ฉด ๊ฐ ์ฌ์ง์์ ๋ฅ๋ฌ๋์ ํตํด ๋์ค ๋งต(๊น์ด ์ ๋ณด)์ ์ถ์ถํฉ๋๋ค.")
|
63 |
+
gr.Markdown("โ ๏ธ **์ฐธ๊ณ :** ๋ฌด๋ฃ CPU ํ๊ฒฝ์์๋ ๋ชจ๋ธ ๋ก๋ฉ ๋ฐ ์ด๋ฏธ์ง ์ฒ๋ฆฌ ์๊ฐ์ด ๋งค์ฐ ์ค๋ ๊ฑธ๋ฆด ์ ์์ต๋๋ค.")
|
64 |
|
65 |
with gr.Row():
|
66 |
+
# file_count="multiple"๋ก ์ฌ๋ฌ ํ์ผ ์
๋ก๋ ๊ฐ๋ฅํ๊ฒ ์ค์
|
67 |
+
# type="filepath"๋ก ์ค์ ํ์ฌ Gradio๊ฐ ํ์ผ์ ์์ ๊ฒฝ๋ก์ ์ ์ฅํ๋๋ก ํจ
|
68 |
input_images = gr.File(label="์ผ๊ตด ์ฌ์ง ์
๋ก๋ (์ต๋ 10์ฅ ๊ถ์ฅ)", file_count="multiple", type="filepath")
|
69 |
|
70 |
output_gallery = gr.Gallery(label="์๋ณธ ์ด๋ฏธ์ง ๋ฐ ๋์ค ๋งต", columns=[2], rows=[1], object_fit="contain", height="auto")
|
|
|
72 |
process_button = gr.Button("๋์ค ๋งต ์ถ์ถ ์์")
|
73 |
|
74 |
def process_all_images(image_paths):
|
75 |
+
"""
|
76 |
+
์
๋ก๋๋ ๋ชจ๋ ์ด๋ฏธ์ง์ ๋ํด ๋์ค ๋งต ์ถ์ถ์ ์ํํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ ํจ์.
|
77 |
+
"""
|
78 |
if not image_paths:
|
79 |
return [(None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.")]
|
80 |
|
81 |
+
results_for_gallery = [] # Gradio Gallery์ ํ์ํ (PIL Image, Label) ํํ ๋ฆฌ์คํธ
|
82 |
+
|
83 |
for i, path in enumerate(image_paths):
|
84 |
+
try:
|
85 |
+
original_image = Image.open(path).convert("RGB")
|
86 |
+
depth_map_pil, error = process_image_for_depth(original_image)
|
87 |
+
|
88 |
+
# ์๋ณธ ์ด๋ฏธ์ง๋ฅผ ๋จผ์ ์ถ๊ฐ
|
89 |
+
results_for_gallery.append((original_image, f"์๋ณธ ์ด๋ฏธ์ง {i+1}"))
|
90 |
+
|
91 |
+
if error:
|
92 |
+
print(f"Error processing image {i+1}: {error}")
|
93 |
+
# ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ ๋์ค ๋งต ๋์ ์ค๋ฅ ๋ฉ์์ง ํ์
|
94 |
+
# ์ด ๋ถ๋ถ์ Gradio Gallery๊ฐ (Image, Label)์ ๊ธฐ๋ํ๋ฏ๋ก,
|
95 |
+
# ์ค๋ฅ ๋ฉ์์ง๋ฅผ ํ
์คํธ ์ด๋ฏธ์ง๋ก ๋ง๋ค๊ฑฐ๋, ์ค๋ฅ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
|
96 |
+
# ์ฌ๊ธฐ์๋ ๊ฐ๋จํ ๋น ์ด๋ฏธ์ง์ ํจ๊ป ์ค๋ฅ ๋ฉ์์ง๋ฅผ ํ์ํ๋๋ก ํฉ๋๋ค.
|
97 |
+
dummy_error_image = Image.new('RGB', original_image.size, color = 'red')
|
98 |
+
results_for_gallery.append((dummy_error_image, f"์ค๋ฅ ๋ฐ์: {error}"))
|
99 |
+
else:
|
100 |
+
results_for_gallery.append((depth_map_pil, f"๋์ค ๋งต {i+1}"))
|
101 |
+
except Exception as e:
|
102 |
+
# ํ์ผ ์์ฒด๋ฅผ ์ฌ๋ ๋ฐ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ
|
103 |
+
print(f"Failed to open or process file {path}: {e}")
|
104 |
+
dummy_error_image = Image.new('RGB', (200, 200), color = 'red') # ์์ ์๋ฌ ์ด๋ฏธ์ง
|
105 |
+
results_for_gallery.append((dummy_error_image, f"ํ์ผ ์ฒ๋ฆฌ ์ค๋ฅ: {e}"))
|
106 |
+
|
107 |
+
return results_for_gallery
|
108 |
|
109 |
+
# ๋ฒํผ ํด๋ฆญ ์ ํจ์ ์ฐ๊ฒฐ
|
110 |
process_button.click(
|
111 |
fn=process_all_images,
|
112 |
inputs=input_images,
|
|
|
114 |
)
|
115 |
|
116 |
# Gradio ์ฑ์ FastAPI์ ๋ง์ดํธ
|
117 |
+
# ๊ธฐ๋ณธ ๊ฒฝ๋ก (path="/")์ Gradio ์ฑ์ ๋ง์ดํธํ์ฌ ์น ํ์ด์ง ์ ์ ์ ๋ฐ๋ก UI๊ฐ ๋ณด์ด๋๋ก ํฉ๋๋ค.
|
118 |
app = gr.mount_gradio_app(app, demo, path="/")
|
119 |
|
120 |
# FastAPI ๊ธฐ๋ณธ ์๋ํฌ์ธํธ (์ ํ ์ฌํญ, Gradio ์ฑ์ด ๊ธฐ๋ณธ ๊ฒฝ๋ก๋ฅผ ์ ์ ํจ)
|
121 |
+
# ์ด ์๋ํฌ์ธํธ๋ /api ๊ฒฝ๋ก๋ก ์ ์ํ์ ๋๋ง ์๋ํฉ๋๋ค.
|
122 |
@app.get("/api")
|
123 |
def read_root():
|
124 |
return {"message": "Welcome to the Face Depth Map Extractor! Visit / for the UI."}
|