File size: 1,549 Bytes
e28bfd7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
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
|