| import cv2
|
| import numpy as np
|
|
|
|
|
| def insert_person(left_image_path, right_image_path, person_image_path, depth="medium"):
|
|
|
| left_image = cv2.imread(left_image_path)
|
| right_image = cv2.imread(right_image_path)
|
|
|
|
|
| person = cv2.imread(person_image_path, cv2.IMREAD_UNCHANGED)
|
|
|
|
|
| depth_settings = {
|
| "close": {
|
| "scale": 1.2,
|
| "disparity": 15,
|
| },
|
| "medium": {
|
| "scale": 1.0,
|
| "disparity": 10,
|
| },
|
| "far": {
|
| "scale": 0.7,
|
| "disparity": 5,
|
| },
|
| }
|
|
|
|
|
| scale_factor = depth_settings[depth]["scale"]
|
| disparity = depth_settings[depth]["disparity"]
|
|
|
|
|
| person_h, person_w = person.shape[:2]
|
| new_size = (int(person_w * scale_factor), int(person_h * scale_factor))
|
| person_resized = cv2.resize(person, new_size, interpolation=cv2.INTER_AREA)
|
|
|
|
|
|
|
| left_x, left_y = (
|
| 50,
|
| left_image.shape[0] - person_resized.shape[0] - 50,
|
| )
|
| right_x = left_x + disparity
|
|
|
|
|
| for img, x in zip((left_image, right_image), (left_x, right_x)):
|
|
|
| y, x = max(0, left_y), max(0, x)
|
| y_end = min(y + person_resized.shape[0], img.shape[0])
|
| x_end = min(x + person_resized.shape[1], img.shape[1])
|
|
|
|
|
| roi = img[y:y_end, x:x_end]
|
|
|
|
|
| person_alpha = (
|
| person_resized[: y_end - y, : x_end - x, 3] / 255.0
|
| )
|
| for c in range(3):
|
| roi[:, :, c] = (1 - person_alpha) * roi[
|
| :, :, c
|
| ] + person_alpha * person_resized[: y_end - y, : x_end - x, c]
|
|
|
|
|
| img[y:y_end, x:x_end] = roi
|
|
|
|
|
| return left_image, right_image
|
|
|