Spaces:
Sleeping
Sleeping
Siyun He
commited on
Commit
·
f3a22a2
1
Parent(s):
c8c1f44
add files
Browse files- app.py +120 -0
- sbs_neu.jpg +0 -0
- trump_full.png +0 -0
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from torchvision import models, transforms
|
4 |
+
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
+
import cv2
|
7 |
+
|
8 |
+
# Load pre-trained segmentation model
|
9 |
+
model = models.segmentation.deeplabv3_resnet101(pretrained=True).eval()
|
10 |
+
|
11 |
+
# Function to perform segmentation
|
12 |
+
def segment_person(image):
|
13 |
+
try:
|
14 |
+
if not isinstance(image, Image.Image):
|
15 |
+
raise ValueError("Invalid image format. Please upload a valid image.")
|
16 |
+
|
17 |
+
preprocess = transforms.Compose([
|
18 |
+
transforms.ToTensor(),
|
19 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
20 |
+
])
|
21 |
+
input_tensor = preprocess(image).unsqueeze(0)
|
22 |
+
|
23 |
+
with torch.no_grad():
|
24 |
+
output = model(input_tensor)['out'][0]
|
25 |
+
|
26 |
+
output_predictions = output.argmax(0)
|
27 |
+
person_class = 15 # Person class in DeepLabV3
|
28 |
+
mask = output_predictions == person_class
|
29 |
+
mask_np = mask.byte().cpu().numpy()
|
30 |
+
mask_resized = cv2.resize(mask_np, (image.width, image.height), interpolation=cv2.INTER_NEAREST)
|
31 |
+
|
32 |
+
person = np.array(image)
|
33 |
+
alpha_channel = (mask_resized * 255).astype(np.uint8)
|
34 |
+
rgba_image = np.dstack((person, alpha_channel))
|
35 |
+
return Image.fromarray(rgba_image, 'RGBA')
|
36 |
+
|
37 |
+
except Exception as e:
|
38 |
+
raise RuntimeError(f"Error in segmenting person: {e}")
|
39 |
+
|
40 |
+
# Function to create stereoscopic images with inserted person
|
41 |
+
def create_stereo_images(stereo_image, person_image, depth_level, x_position, y_position):
|
42 |
+
try:
|
43 |
+
if not isinstance(stereo_image, np.ndarray):
|
44 |
+
raise ValueError("Invalid stereo image format. Please upload a valid image.")
|
45 |
+
|
46 |
+
height, width, _ = stereo_image.shape
|
47 |
+
midpoint = width // 2
|
48 |
+
left_image = stereo_image[:, :midpoint]
|
49 |
+
right_image = stereo_image[:, midpoint:]
|
50 |
+
|
51 |
+
left_image = Image.fromarray(cv2.cvtColor(left_image, cv2.COLOR_BGR2RGBA))
|
52 |
+
right_image = Image.fromarray(cv2.cvtColor(right_image, cv2.COLOR_BGR2RGBA))
|
53 |
+
|
54 |
+
disparity_map = {"close": 10, "medium": 1, "far": -10}
|
55 |
+
scaling_factors = {"close": 1.2, "medium": 1.0, "far": 0.8}
|
56 |
+
|
57 |
+
if depth_level not in disparity_map:
|
58 |
+
raise ValueError("Invalid depth level. Choose from 'close', 'medium', or 'far'.")
|
59 |
+
|
60 |
+
disparity = disparity_map[depth_level]
|
61 |
+
person_scale = scaling_factors[depth_level]
|
62 |
+
person_image = person_image.resize(
|
63 |
+
(int(person_image.width * person_scale), int(person_image.height * person_scale)),
|
64 |
+
Image.LANCZOS
|
65 |
+
)
|
66 |
+
|
67 |
+
left_x, left_y = x_position, y_position
|
68 |
+
right_x = left_x + disparity
|
69 |
+
|
70 |
+
left_image.paste(person_image, (left_x, left_y), person_image)
|
71 |
+
right_image.paste(person_image, (right_x, left_y), person_image)
|
72 |
+
|
73 |
+
return left_image, right_image
|
74 |
+
|
75 |
+
except Exception as e:
|
76 |
+
raise RuntimeError(f"Error in creating stereo images: {e}")
|
77 |
+
|
78 |
+
# Function to create an anaglyph image
|
79 |
+
def create_anaglyph(left_image, right_image):
|
80 |
+
try:
|
81 |
+
left_r, _, _ = left_image.convert("RGB").split()
|
82 |
+
_, right_g, right_b = right_image.convert("RGB").split()
|
83 |
+
anaglyph_image = Image.merge("RGB", (left_r, right_g, right_b))
|
84 |
+
return anaglyph_image
|
85 |
+
except Exception as e:
|
86 |
+
raise RuntimeError(f"Error in creating anaglyph image: {e}")
|
87 |
+
|
88 |
+
# Gradio interface function
|
89 |
+
def process_images(person_image, stereo_image, depth_level, x_position, y_position):
|
90 |
+
try:
|
91 |
+
person_image = segment_person(person_image)
|
92 |
+
stereo_image = np.array(stereo_image)
|
93 |
+
|
94 |
+
left_image, right_image = create_stereo_images(stereo_image, person_image, depth_level, x_position, y_position)
|
95 |
+
anaglyph_image = create_anaglyph(left_image, right_image)
|
96 |
+
|
97 |
+
return anaglyph_image
|
98 |
+
except Exception as e:
|
99 |
+
return f"An error occurred: {e}"
|
100 |
+
|
101 |
+
# Gradio app setup
|
102 |
+
with gr.Blocks() as demo:
|
103 |
+
gr.Markdown("# 3D Anaglyph Image Creator")
|
104 |
+
gr.Markdown("Upload a person image, select depth, adjust position, and create a 3D anaglyph image.")
|
105 |
+
|
106 |
+
with gr.Row():
|
107 |
+
person_image = gr.Image(label="Upload Person Image", type="pil")
|
108 |
+
stereo_image = gr.Image(label="Upload Stereo Background Image", type="pil")
|
109 |
+
|
110 |
+
depth_level = gr.Radio(["close", "medium", "far"], label="Select Depth Level")
|
111 |
+
x_position = gr.Slider(0, 2000, step=1, label="X Position")
|
112 |
+
y_position = gr.Slider(0, 2000, step=1, label="Y Position")
|
113 |
+
|
114 |
+
output_image = gr.Image(label="Generated Anaglyph Image")
|
115 |
+
|
116 |
+
generate_button = gr.Button("Generate Anaglyph Image")
|
117 |
+
generate_button.click(process_images, [person_image, stereo_image, depth_level, x_position, y_position], output_image)
|
118 |
+
|
119 |
+
# Launch the app
|
120 |
+
demo.launch()
|
sbs_neu.jpg
ADDED
![]() |
trump_full.png
ADDED
![]() |