Example #1
0
 def align_backward(self,
                    style_changed_aligned_patch: np.ndarray) -> np.ndarray:
     '''
     스타일 변환이 완료된 style_changed_aligned_patch로 부터 style_changed_origin_patch를 반환합니다.
     param style_changed_aligned_patch : 기존 aligned_patch에서 스타일만 변경된 이미지
     return : 기존 origin_patch에서 스타일만 변경된 이미지
     '''
     face_center: tuple[int, int] = (len(self.origin_patch[0]) // 2,
                                     len(self.origin_patch) // 2)
     margin: float = self.bounding_box.margin
     rotation: float = self.bounding_box.rotation
     margin = int((1 / math.sqrt(2)) * margin)
     # 1. rotated_patch에 덮어쓰기
     modified_origin_patch = self.rotated_patch.copy()
     modified_origin_patch[face_center[0] - margin:face_center[0] + margin,
                           face_center[1] - margin:face_center[1] +
                           margin] = style_changed_aligned_patch
     # 2. -회전각 만큼 회전
     matrix_rev = cv2.getRotationMatrix2D(face_center, -rotation,
                                          math.sqrt(2))
     modified_origin_patch = cv2.warpAffine(
         modified_origin_patch, matrix_rev,
         (len(modified_origin_patch[0]), len(modified_origin_patch)))
     # 3. origin_patch에 덮어쓰기
     return modified_origin_patch
Example #2
0
def cosine_similarity(x_0, x_1):
    # https://neo4j.com/docs/graph-algorithms/current/labs-algorithms/cosine/
    if (len(x_0) != len(x_1)):
        return 420
    num = 0
    den_x_0, den_x_1 = 0, 0
    for i in range(len(x_0)):
        num += x_0[i] * x_1[i]
        den_x_0 += math.pow(x_0[i], 2)
        den_x_1 += math.pow(x_1[i], 2)
    den_x_0 = math.sqrt(den_x_0)
    den_x_1 = math.sqrt(den_x_1)
    den = den_x_0 * den_x_1
    return num / den
Example #3
0
 def compute_ranks(sigma, v):
     dimensions = len(sigma)
     powered_sigma = tuple(s**2 if i < dimensions else 0.0
                           for i, s in enumerate(sigma))
     ranks = []
     # iterate over columns of matrix (rows of transposed matrix)
     for column_vector in v.T:
         rank = sum(s * v**2 for s, v in zip(powered_sigma, column_vector))
         ranks.append(math.sqrt(rank))
     return ranks
Example #4
0
    def align_forward(self) -> np.ndarray:
        '''
        origin_patch로 부터 aligned_patch를 반환합니다.
        return : margin * margin 크기의 aligned_patch
        '''
        origin_patch: np.ndarray = self.bounding_box.get_origin_patch()
        face_center: tuple[int, int] = (len(self.origin_patch[0]) // 2,
                                        len(self.origin_patch) // 2)
        margin: float = self.bounding_box.margin
        rotation: float = self.bounding_box.rotation

        # 1. origin_patch를 theta만큼 회전
        matrix = cv2.getRotationMatrix2D(face_center, rotation,
                                         1 / math.sqrt(2))
        margin = int((1 / math.sqrt(2)) * margin)
        aligned_patch: np.ndarray = cv2.warpAffine(
            origin_patch, matrix, (len(origin_patch[0]), len(origin_patch)))
        self.rotated_patch = aligned_patch.copy()
        # 2. 얼굴 중심 기준 margin만큼 잘라내기
        aligned_patch = aligned_patch[face_center[0] - margin:face_center[0] +
                                      margin, face_center[1] -
                                      margin:face_center[1] + margin]

        return aligned_patch
Example #5
0
def get_diff_frame(
    img1: np.ndarray,
    img2: np.ndarray,
    chunk_size=16,
    padding_size=2,
    ssim=True,
    min_ssim=0.9987,
) -> Optional[FrameDiff]:
    chunks1 = get_img_chunks(img1, chunk_size)
    chunks2 = get_img_chunks(img2, chunk_size)
    chunks_diff = get_img_diff_chunks(
        img2, chunks1, chunks2, chunk_size, padding_size, ssim, min_ssim
    )

    chunks_diff_pos_list = list(chunks_diff.keys())
    chunks_diff_list = list(chunks_diff.values())

    # add black chunks to complete a square image
    num_black_imgs: int = math.ceil(math.sqrt(len(chunks_diff_list))) ** 2 - len(
        chunks_diff_list
    )
    chunks_diff_list.extend([get_black_img(chunk_size, padding_size)] * num_black_imgs)

    # add padding if the width or height of the image is less than chunk_size
    chunks_diff_list_new: List[np.ndarray] = []
    for chunk in chunks_diff_list:
        if chunk.shape[0] != chunk_size or chunk.shape[1] != chunk_size:
            # tmp_img: np.ndarray = np.zeros((chunk_size, chunk_size, 3), np.uint8)
            tmp_img: np.ndarray = np.zeros(
                (
                    chunk_size + padding_size * 2,
                    chunk_size + padding_size * 2,
                    3,
                ),
                np.uint8,
            )
            # tmp_img[:, 0:] = (0, 255, 0)
            tmp_img[0 : chunk.shape[0], 0 : chunk.shape[1]] = chunk
            chunk = tmp_img
        chunks_diff_list_new.append(chunk)
    chunks_diff_list = chunks_diff_list_new
    # print(len(chunks_diff_list))

    img_diff = get_img_diff(chunks_diff_list, chunk_size, padding_size)
    if img_diff is None:
        return None
    return FrameDiff(img_diff, chunks_diff_pos_list)
Example #6
0
def get_img_diff(
    chunks_diff: List[np.ndarray], chunk_size=16, padding_size=2
) -> Optional[np.ndarray]:
    img_diff_list = []
    num_chunks = int(math.sqrt(len(chunks_diff)))
    if num_chunks == 0:
        return None
    for chunks in [
        chunks_diff[x : x + num_chunks] for x in range(0, len(chunks_diff), num_chunks)
    ]:
        if not np.allclose(
            chunks, [get_black_img(chunk_size, padding_size)] * len(chunks)
        ):
            img_diff = cv2.hconcat(chunks)
            # img_diff = np.concatenate(chunks, axis=0)
            img_diff_list.append(img_diff)
    img_diff = cv2.vconcat(img_diff_list)
    return img_diff
Example #7
0
import numpy as np
from numpy.lib import math
from least_squares import least_squares
import matplotlib.pyplot as plt

x_vals = np.array(
    [0.06813, 0.17032, 0.32361, 0.55355, 0.89845, 1.41581, 2.19184])
y_vals = np.array(
    [121.9718, 123.4432, 125.6814, 129.1098, 134.4159, 142.7555, 156.1629])

plt.scatter(x_vals, y_vals)

coefs = least_squares([lambda x: 1, lambda x: x], x_vals, y_vals)
print(coefs)
plt.plot(x_vals, [coefs[0] + coefs[1] * t for t in x_vals])
error = math.sqrt(
    sum([(y - (coefs[0] + coefs[1] * x))**2
         for x, y in zip(x_vals, y_vals)]) / x_vals.size)
print(error)
plt.show()
print(quadratic_coefs)
print(exp_coefs)
days = np.arange(data.size)

def generate_model_results(days, parameters, coefs):
    model_values = []
    for x in days:
        a = 0
        for i, func in enumerate(parameters):
            a += coefs[i] * func(x)
        model_values.append(a)
    return model_values

linear_model_values = generate_model_results(days, linear, linear_coefs)
plt.plot(days, generate_model_results(days, linear, linear_coefs), label="linear")
print(math.sqrt(sum([(x-y)**2 for x, y in zip(data, linear_model_values)])/data.size))

quadratic_model_values = generate_model_results(days, quadratic, quadratic_coefs)
plt.plot(days, generate_model_results(days, quadratic, quadratic_coefs), label="quadratic")
print(math.sqrt(sum([(x-y)**2 for x, y in zip(data, quadratic_model_values)])/data.size))

# exponential model
a = exp_coefs[0]
b = exp_coefs[1]
exp_model_values = [a*math.exp(b*x) for x in days]
plt.plot(days, exp_model_values, label="exponential")
print(math.sqrt(sum([(x-y)**2 for x, y in zip(data, exp_model_values)])/data.size))

plt.legend()
plt.show()