Merge pull request #12 from xlang-ai/zhoujun/multi-app
Update multi-app examples
This commit is contained in:
@@ -69,7 +69,7 @@ from .gimp import (
|
||||
check_brightness_decrease_and_structure_sim,
|
||||
check_contrast_increase_and_structure_sim,
|
||||
check_saturation_increase_and_structure_sim,
|
||||
check_image_size_and_structure_sim,
|
||||
check_image_size,
|
||||
check_image_mirror,
|
||||
check_palette_and_structure_sim,
|
||||
check_textbox_on_leftside,
|
||||
@@ -82,7 +82,9 @@ from .gimp import (
|
||||
increase_saturation,
|
||||
decrease_brightness,
|
||||
check_file_exists,
|
||||
compare_triangle_positions
|
||||
compare_triangle_positions,
|
||||
check_sharper,
|
||||
check_image_file_size
|
||||
)
|
||||
from .libreoffice import check_libre_locale
|
||||
from .pdf import check_pdf_pages
|
||||
@@ -126,11 +128,13 @@ from .vscode import (
|
||||
compare_text_file,
|
||||
compare_config,
|
||||
compare_answer,
|
||||
compare_result_files,
|
||||
is_extension_installed,
|
||||
check_json_settings,
|
||||
check_json_keybindings,
|
||||
check_python_file_by_test_suite,
|
||||
check_python_file_by_gold_file,
|
||||
check_html_background_image,
|
||||
compare_zip_files
|
||||
)
|
||||
from .others import compare_epub, check_mp3_meta
|
||||
|
||||
@@ -5,7 +5,7 @@ from PIL import Image, ImageChops, ImageStat
|
||||
|
||||
|
||||
def compare_image_list(pred_img_path_list: Union[str, List[str]],
|
||||
gold_img_path_list: Union[str, List[str]]) -> float:
|
||||
gold_img_path_list: Union[str, List[str]]) -> float:
|
||||
""" Compare two image lists, only if all images are the same, return 1.0, otherwise return 0.0
|
||||
"""
|
||||
if type(pred_img_path_list) != list:
|
||||
@@ -177,6 +177,16 @@ def calculate_contrast(image):
|
||||
return np.std(pixels)
|
||||
|
||||
|
||||
def calculate_image_sharpness(image_path):
|
||||
# Load the image in grayscale
|
||||
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
||||
# Apply the Laplacian operator
|
||||
laplacian = cv2.Laplacian(image, cv2.CV_64F)
|
||||
# Calculate the variance
|
||||
variance = np.var(laplacian)
|
||||
return variance
|
||||
|
||||
|
||||
def structure_check_by_mse(img1, img2, threshold=0.03):
|
||||
"""Check if two images are approximately the same by MSE"""
|
||||
mse = np.mean(
|
||||
@@ -295,7 +305,8 @@ def check_triangle_position(tgt_path):
|
||||
|
||||
# We assume the triangle is a different color from the background
|
||||
# Find the unique colors
|
||||
unique_colors, counts = np.unique(img_array.reshape(-1, img_array.shape[2]), axis=0, return_counts=True)
|
||||
unique_colors, counts = np.unique(img_array.reshape(-1, img_array.shape[2]), axis=0,
|
||||
return_counts=True)
|
||||
unique_colors_sorted = unique_colors[np.argsort(counts)]
|
||||
|
||||
# Assuming the background is the most common color and the triangle is a different color
|
||||
@@ -337,6 +348,25 @@ def check_structure_sim(src_path, tgt_path):
|
||||
return structure_same
|
||||
|
||||
|
||||
def check_structure_sim_resized(src_path, tgt_path):
|
||||
"""
|
||||
Check if the structure of the two images are similar after resizing.
|
||||
gimp:d16c99dc-2a1e-46f2-b350-d97c86c85c15
|
||||
"""
|
||||
if src_path is None or tgt_path is None:
|
||||
return 0.
|
||||
|
||||
img_src = Image.open(src_path)
|
||||
img_tgt = Image.open(tgt_path)
|
||||
|
||||
# Resize the images to the same size
|
||||
img_src = img_src.resize(img_tgt.size)
|
||||
|
||||
# Check if the structure is similar
|
||||
structure_same = structure_check_by_ssim(img_src, img_tgt)
|
||||
return structure_same
|
||||
|
||||
|
||||
def check_contrast_increase_and_structure_sim(src_path, tgt_path):
|
||||
"""
|
||||
Check if the src image has higher contrast than the tgt image and the structures are similar
|
||||
@@ -388,34 +418,28 @@ def check_config_status(actual_config_path, rule):
|
||||
return 0.
|
||||
|
||||
|
||||
def check_image_size_and_structure_sim(src_path, tgt_path, height=512, width=None):
|
||||
def check_image_size(src_path, rule):
|
||||
"""
|
||||
Check if the size of the src image is correct and the structure of the two images are similar.
|
||||
gimp:d16c99dc-2a1e-46f2-b350-d97c86c85c15
|
||||
Check if the size of the src image is correct
|
||||
multi-apps:42f4d1c7-4521-4161-b646-0a8934e36081
|
||||
"""
|
||||
|
||||
if src_path is None or tgt_path is None:
|
||||
if src_path is None:
|
||||
return 0.
|
||||
|
||||
# Load images
|
||||
source_image = Image.open(src_path)
|
||||
target_image = Image.open(tgt_path)
|
||||
# Load the image
|
||||
img = Image.open(src_path)
|
||||
|
||||
# Check size
|
||||
if width is not None:
|
||||
width_same = source_image.size[0] == width
|
||||
else:
|
||||
width_same = True
|
||||
if height is not None:
|
||||
height_same = source_image.size[1] == height
|
||||
# Check the size
|
||||
if rule["height"] is not None:
|
||||
height_same = img.size[1] == rule["height"]
|
||||
else:
|
||||
height_same = True
|
||||
if rule["width"] is not None:
|
||||
width_same = img.size[0] == rule["width"]
|
||||
else:
|
||||
width_same = True
|
||||
|
||||
# Check structure
|
||||
resized_target_image = target_image.resize(source_image.size)
|
||||
structure_same = structure_check_by_ssim(source_image, resized_target_image)
|
||||
|
||||
if width_same and height_same and structure_same:
|
||||
if height_same and width_same:
|
||||
return 1.
|
||||
else:
|
||||
return 0.
|
||||
@@ -521,6 +545,31 @@ def check_green_background(src_path, tgt_path):
|
||||
return 1.
|
||||
|
||||
|
||||
def check_sharper(src_path, tgt_path):
|
||||
"""
|
||||
Check if the source image is sharper than the target image.
|
||||
multi-app:bb7db4c2-30b5-4be7-8dd7-b8c4ec7d3108
|
||||
"""
|
||||
sharpness_src = calculate_image_sharpness(src_path)
|
||||
sharpness_tgt = calculate_image_sharpness(tgt_path)
|
||||
return 1.0 if sharpness_src > sharpness_tgt else 0.0
|
||||
|
||||
|
||||
def check_image_file_size(src_path, rule):
|
||||
"""
|
||||
Check if the size of the src image within 500KB
|
||||
"""
|
||||
if src_path is None:
|
||||
return 0.0
|
||||
|
||||
# Check the size
|
||||
file_size = os.path.getsize(src_path)
|
||||
if file_size < rule["max_size"]:
|
||||
return 1.0
|
||||
else:
|
||||
return 0.0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
actual_config_path = "../../../cache/sessionrc_test"
|
||||
rule = {
|
||||
@@ -550,3 +599,12 @@ if __name__ == "__main__":
|
||||
tgt_path = "../../../cache/f4aec372-4fb0-4df5-a52b-79e0e2a5d6ce/Triangle_In_The_Middle.png"
|
||||
print(check_triangle_position(tgt_path))
|
||||
|
||||
src_path = "../../../cache/bb7db4c2-30b5-4be7-8dd7-b8c4ec7d3108/anmi_sharper.png"
|
||||
tgt_path = "../../../cache/bb7db4c2-30b5-4be7-8dd7-b8c4ec7d3108/anmi.png"
|
||||
print(check_sharper(src_path, tgt_path))
|
||||
|
||||
src_path = "../../../cache/3c8f201a-009d-4bbe-8b65-a6f8b35bb57f/compressed.jpeg"
|
||||
rule = {
|
||||
"max_size": 500000
|
||||
}
|
||||
print(check_image_file_size(src_path, rule))
|
||||
@@ -203,3 +203,54 @@ def check_python_file_by_test_suite(actual_files, test_file, **options) -> float
|
||||
|
||||
def check_python_file_by_gold_file(actual_files, gold_file: str, **options) -> float:
|
||||
pass
|
||||
|
||||
|
||||
def check_html_background_image(src_path: str, rule: Dict = None) -> float:
|
||||
"""
|
||||
Check if the background image is correctly set.
|
||||
multi-app:bb7db4c2-30b5-4be7-8dd7-b8c4ec7d3108
|
||||
"""
|
||||
from bs4 import BeautifulSoup
|
||||
with open(src_path, 'r') as f:
|
||||
html_content = f.read()
|
||||
soup = BeautifulSoup(html_content, 'html.parser')
|
||||
styles = soup.find_all('style')
|
||||
for style in styles:
|
||||
if f'background-image: url(\'{rule["value"]}\')' in style.text:
|
||||
return 1.0
|
||||
return 0.0
|
||||
|
||||
|
||||
def compare_result_files(src_path, tgt_path):
|
||||
"""
|
||||
Compare whether the content of two files are the same.
|
||||
multi-app:7f35355e-02a6-45b5-b140-f0be698bcf85
|
||||
"""
|
||||
with open(src_path, 'r') as f:
|
||||
src_content = f.read().strip()
|
||||
with open(tgt_path, 'r') as f:
|
||||
tgt_content = f.read().strip()
|
||||
try:
|
||||
# Compare the content as numbers
|
||||
tgt_content_num = float(tgt_content)
|
||||
if tgt_content in src_content:
|
||||
# If the content of tgt is in src, return 1.0 since output src might be
|
||||
# a superset(language description+number) of tgt
|
||||
return 1.0
|
||||
src_content_num = float(src_content)
|
||||
if abs(src_content_num - tgt_content_num) < 1e-4:
|
||||
return 1.0
|
||||
return 0.0
|
||||
except:
|
||||
if src_content == tgt_content:
|
||||
return 1.0
|
||||
return 0.0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
src_path = "../../../cache/bb7db4c2-30b5-4be7-8dd7-b8c4ec7d3108/index.html"
|
||||
rule = {
|
||||
"type:": "value",
|
||||
"value": "anmi_sharper.png"
|
||||
}
|
||||
print(check_html_background_image(src_path, rule))
|
||||
|
||||
Reference in New Issue
Block a user