LAB4 / src /gradio_interface.py
nnibras's picture
Update src/gradio_interface.py
e28bfd7 verified
from src.segmentation import segment_person
from src.stereoscopic_insert import insert_person
from src.anaglyph_converter import create_anaglyph
from src.trial_insert import insert_person_V2
import cv2
from PIL import Image
def generate_anaglyph(
person_image_path, left_image_path, right_image_path, depth="medium"
):
"""
Generate an anaglyph 3D image by segmenting a person from an uploaded image,
inserting the segmented person into a stereoscopic pair, and converting the result
to an anaglyph format.
Parameters:
- person_image_path: file path to the uploaded person image.
- left_image_path: file path to the uploaded left stereoscopic image.
- right_image_path: file path to the uploaded right stereoscopic image.
- depth: depth level for the person in the 3D scene ("close", "medium", or "far").
Returns:
- Anaglyph PIL image ready for display.
"""
# Segment the person from the uploaded image
person_image = segment_person(person_image_path)
# Save the segmented image temporarily for overlay purposes
person_image.save("temp_person.png")
# Insert the segmented person into the stereoscopic images
left_image, right_image = insert_person_V2(
left_image_path, right_image_path, "temp_person.png", depth
)
# Create the final anaglyph image from the left and right images
anaglyph_image = create_anaglyph(left_image, right_image)
anaglyph_pil = Image.fromarray(cv2.cvtColor(anaglyph_image, cv2.COLOR_BGR2RGB))
return anaglyph_pil