From 136b52c8763f9f9beb2f85acf07224a5592634f8 Mon Sep 17 00:00:00 2001 From: tsuky_chen <3107760494@qq.com> Date: Sat, 13 Jan 2024 01:49:46 +0800 Subject: [PATCH] eval gimp compare pics --- desktop_env/evaluators/metrics/__init__.py | 2 +- desktop_env/evaluators/metrics/gimp.py | 47 ++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/desktop_env/evaluators/metrics/__init__.py b/desktop_env/evaluators/metrics/__init__.py index 289428c..85f09ea 100644 --- a/desktop_env/evaluators/metrics/__init__.py +++ b/desktop_env/evaluators/metrics/__init__.py @@ -11,7 +11,7 @@ from .table import compare_table from .vlc import is_vlc_playing, is_vlc_recordings_folder, is_vlc_fullscreen, compare_images, compare_audios, \ compare_videos -from .gimp import increase_saturation, decrease_brightness +from .gimp import increase_saturation, decrease_brightness, check_file_exists, compare_triangle_positions from .general import check_csv, check_accessibility_tree, check_list from .thunderbird import check_thunderbird_prefs, check_thunderbird_filter diff --git a/desktop_env/evaluators/metrics/gimp.py b/desktop_env/evaluators/metrics/gimp.py index cac7635..1e919ef 100644 --- a/desktop_env/evaluators/metrics/gimp.py +++ b/desktop_env/evaluators/metrics/gimp.py @@ -20,6 +20,10 @@ def get_gimp_export_path(): # Handle the case where the configuration file is not found print("GIMP configuration file not found") return False + +def check_file_exists(directory, filename): + file_path = os.path.join(directory, filename) + return 1 if os.path.isfile(file_path) else 0 def increase_saturation(image1_path: str, image2_path: str) -> float: def calculate_saturation(image): @@ -61,6 +65,49 @@ def decrease_brightness(image1_path: str, image2_path: str) -> float: brightness2 = calculate_brightness(image2) return 1 if brightness1 > brightness2 else 0 +import cv2 +import numpy as np + +def find_yellow_triangle(image): + # Convert the image to RGBA + rgba = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA) + + # define range of yellow color in HSV + lower_yellow = np.array([0, 0, 0], dtype=np.uint8) + upper_yellow = np.array([255, 255, 255], dtype=np.uint8) + + # expand the dimensions of lower and upper yellow to match the image dimensions + lower_yellow = np.reshape(lower_yellow, (1, 1, 3)) + upper_yellow = np.reshape(upper_yellow, (1, 1, 3)) + # build a mask for the yellow color + mask = cv2.inRange(rgba, lower_yellow, upper_yellow) + + # search for contours in the mask + contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + + # choose the largest contour + max_contour = max(contours, key=cv2.contourArea) + + # calculate the center of the contour + M = cv2.moments(max_contour) + cx = int(M['m10'] / M['m00']) + cy = int(M['m01'] / M['m00']) + + return cx, cy + +def compare_triangle_positions(image1, image2): + image1 = cv2.imread(image1, cv2.IMREAD_COLOR) + image2 = cv2.imread(image2, cv2.IMREAD_COLOR) + # find the center of the yellow triangle in each image + cx1, cy1 = find_yellow_triangle(image1) + cx2, cy2 = find_yellow_triangle(image2) + + # calculate the distance between the center of the triangle and the center of the image + center_distance1 = np.sqrt((cx1 - image1.shape[1] // 2)**2 + (cy1 - image1.shape[0] // 2)**2) + center_distance2 = np.sqrt((cx2 - image2.shape[1] // 2)**2 + (cy2 - image2.shape[0] // 2)**2) + + return 1 if center_distance1 > center_distance2 else 0 + if __name__ == "__main__": image1_path = "../Downloads/1.png"