-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage_utils.py
More file actions
159 lines (122 loc) · 5.36 KB
/
Copy pathimage_utils.py
File metadata and controls
159 lines (122 loc) · 5.36 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from rembg import remove
from transformers import pipeline
import torch
import cv2
import numpy as np
import os
import PIL
from diffusers import (
StableDiffusionControlNetInpaintPipeline,
ControlNetModel,
DDIMScheduler,
)
from diffusers.utils import load_image
import random
import argparse
def generate_mask(image):
mask = remove(image, only_mask=True)
img = cv2.cvtColor(remove(image), cv2.COLOR_RGBA2RGB)
inverted_mask = cv2.bitwise_not(mask)
return img, inverted_mask
def generate_cany(image):
canny_image = cv2.Canny(image, 100, 200)
canny_image = canny_image[:, :, None]
canny_image = np.concatenate([canny_image, canny_image, canny_image], axis=2)
return canny_image
def generate_dept(init_image):
print("Generating depth image")
# Use the depth estimation pipeline to generate a depth map from the input image
depth_estimator = pipeline(task="depth-estimation", model="Intel/dpt-large")
image = depth_estimator(init_image)["depth"]
# Convert the depth map to a NumPy array and expand dimensions
image = np.array(image)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
# Convert the NumPy array to a PIL Image
image = PIL.Image.fromarray(image)
return image
def conv_pill(image):
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
im_pil = PIL.Image.fromarray(img)
return im_pil
def check_max_resolution_rescale(image, max_width, max_height):
width, height = image.size
if width > max_width or height > max_height:
ratio = min(max_width / width, max_height / height)
new_width = int(width * ratio)
new_height = int(height * ratio)
image = image.resize(
(new_width, new_height), PIL.Image.LANCZOS
) # Image.LANCZOS bu metod küçültmede oluşan alizing problerini gidermek için
new_width, new_height = image.size
if new_width % 8 != 0 or new_height % 8 != 0:
new_width = new_width - new_width % 8
new_height = new_height - new_height % 8
image = image.resize(
(new_width, new_height), PIL.Image.LANCZOS
) # Image.LANCZOS bu metod küçültmede oluşan alizing problerini gidermek için
image = np.array(image)
image_cv2 = image[:, :, ::-1].copy()
return image_cv2
def open_image(image, mask, w, h):
# Objeyi içeren bölgenin sınırlarını bul
y, x = np.where(mask < 1)
top, bottom, left, right = np.min(y), np.max(y), np.min(x), np.max(x)
# Objeyi içeren bölgeyi kırp
object_cropped = image[top : bottom + 1, left : right + 1]
mask_cropped = cv2.merge(
[
mask[top : bottom + 1, left : right + 1],
mask[top : bottom + 1, left : right + 1],
mask[top : bottom + 1, left : right + 1],
]
)
# Create a black background with twice the dimensions of the input image
background = np.zeros((w, h, 3), dtype=np.uint8)
mask_background = np.ones((w, h, 3), dtype=np.uint8) * 255
# Calculate the offset to center the input image on the black background
x_offset = (background.shape[1] - object_cropped.shape[1]) // 2
y_offset = (background.shape[0] - object_cropped.shape[0]) // 2
# Place the image in the center of the black background
background[
y_offset : y_offset + object_cropped.shape[0],
x_offset : x_offset + object_cropped.shape[1],
] = object_cropped
mask_background[
y_offset : y_offset + mask_cropped.shape[0],
x_offset : x_offset + mask_cropped.shape[1],
] = mask_cropped
return background, mask_background
def ext_image(image, w, h):
# Create a black background with twice the dimensions of the input image
background = np.zeros((w * 2, h * 2, 3), dtype=np.uint8)
# Calculate the offset to center the input image on the black background
x_offset = (background.shape[1] - image.shape[1]) // 2
y_offset = (background.shape[0] - image.shape[0]) // 2
# Place the image in the center of the black background
background[
y_offset : y_offset + image.shape[0], x_offset : x_offset + image.shape[1]
] = image
return background
def make_inpaint_condition(init_image, mask_image):
# Convert the init_image to a NumPy array in RGB format and normalize to [0, 1]
init_image = np.array(init_image.convert("RGB")).astype(np.float32) / 255.0
# Convert the mask_image to a NumPy array in grayscale format and normalize to [0, 1]
mask_image = np.array(mask_image.convert("L")).astype(np.float32) / 255.0
# Ensure that the dimensions of init_image and mask_image match
assert (
init_image.shape[0:1] == mask_image.shape[0:1]
) # "image and image_mask must have the same image size"
# Set masked pixels in init_image to -1.0
init_image[mask_image > 0.5] = -1.0
# Expand dimensions and transpose to the required shape for torch
init_image = np.expand_dims(init_image, 0).transpose(0, 3, 1, 2)
# Convert the NumPy array to a PyTorch tensor
init_image = torch.from_numpy(init_image)
return init_image
def add_fg(full_img, fg_img, mask_img):
full_img = np.array(full_img).astype(np.float32)
fg_img = np.array(fg_img).astype(np.float32)
mask_img = np.array(mask_img).astype(np.float32) / 255.0
full_img = full_img * mask_img + fg_img * (1 - mask_img)
return PIL.Image.fromarray(np.clip(full_img, 0, 255).astype(np.uint8))