jhj0517
commited on
Commit
·
87ec96b
1
Parent(s):
4cfbecc
Add test codes
Browse files- tests/test_config.py +46 -0
- tests/test_expression_editing.py +34 -0
tests/test_config.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image, ImageChops
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
import torch
|
| 5 |
+
import functools
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
from modules.utils.paths import *
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
TEST_IMAGE_URL = "https://github.com/microsoft/onnxjs-demo/raw/master/src/assets/EmotionSampleImages/sad_baby.jpg"
|
| 12 |
+
TEST_IMAGE_PATH = os.path.join(PROJECT_ROOT_DIR, "tests", "test.png")
|
| 13 |
+
TEST_EXPRESSION_OUTPUT_PATH = os.path.join(PROJECT_ROOT_DIR, "tests", "edited_expression.png")
|
| 14 |
+
TEST_EXPRESSION_AAA = 100
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def download_image(url, path):
|
| 18 |
+
if os.path.exists(path):
|
| 19 |
+
return
|
| 20 |
+
|
| 21 |
+
response = requests.get(url, stream=True)
|
| 22 |
+
if response.status_code == 200:
|
| 23 |
+
with open(path, 'wb') as file:
|
| 24 |
+
for chunk in response.iter_content(1024):
|
| 25 |
+
file.write(chunk)
|
| 26 |
+
print(f"Image successfully downloaded to {path}")
|
| 27 |
+
else:
|
| 28 |
+
raise Exception(f"Failed to download image. Status code: {response.status_code}")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def are_images_different(image1_path: str, image2_path: str):
|
| 32 |
+
image1 = Image.open(image1_path)
|
| 33 |
+
image2 = Image.open(image2_path)
|
| 34 |
+
|
| 35 |
+
diff = ImageChops.difference(image1, image2)
|
| 36 |
+
|
| 37 |
+
if diff.getbbox() is None:
|
| 38 |
+
return False
|
| 39 |
+
else:
|
| 40 |
+
return True
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@functools.lru_cache
|
| 44 |
+
def is_cuda_available():
|
| 45 |
+
return torch.cuda.is_available()
|
| 46 |
+
|
tests/test_expression_editing.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from test_config import *
|
| 5 |
+
from modules.live_portrait.live_portrait_inferencer import LivePortraitInferencer
|
| 6 |
+
from modules.utils.image_helper import save_image
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@pytest.mark.parametrize(
|
| 10 |
+
"input_image,aaa",
|
| 11 |
+
[
|
| 12 |
+
(TEST_IMAGE_PATH, TEST_EXPRESSION_AAA),
|
| 13 |
+
]
|
| 14 |
+
)
|
| 15 |
+
def test_expression_editing(
|
| 16 |
+
input_image: str,
|
| 17 |
+
aaa: int
|
| 18 |
+
):
|
| 19 |
+
if not os.path.exists(TEST_IMAGE_PATH):
|
| 20 |
+
download_image(
|
| 21 |
+
TEST_IMAGE_URL,
|
| 22 |
+
TEST_IMAGE_PATH
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
inferencer = LivePortraitInferencer()
|
| 26 |
+
|
| 27 |
+
edited_expression = inferencer.edit_expression(
|
| 28 |
+
src_image=input_image,
|
| 29 |
+
aaa=aaa
|
| 30 |
+
)
|
| 31 |
+
save_image(numpy_array=edited_expression, output_path=TEST_EXPRESSION_OUTPUT_PATH)
|
| 32 |
+
|
| 33 |
+
assert os.path.exists(TEST_EXPRESSION_OUTPUT_PATH)
|
| 34 |
+
assert are_images_different(input_image, TEST_EXPRESSION_OUTPUT_PATH)
|