Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Add image comparison and filtering functions in judge_system.py

Magnus Müller20f188a8521b
diff --git a/eval/judge_system.py b/eval/judge_system.py
index 8a3157b5b..ba4e2c9af 100644
--- a/eval/judge_system.py
+++ b/eval/judge_system.py
@@ -177,6 +177,57 @@ def prepare_agent_steps(complete_history: list[dict]) -> list[str]:
return steps
+def are_images_identical(img_path1: str, img_path2: str) -> bool:
+ """Check if two images are identical by comparing their content."""
+ try:
+ with Image.open(img_path1) as img1, Image.open(img_path2) as img2:
+ # Convert to same format for comparison
+ if img1.mode != img2.mode:
+ img1 = img1.convert('RGB')
+ img2 = img2.convert('RGB')
+
+ # Compare sizes first (quick check)
+ if img1.size != img2.size:
+ return False
+
+ # Compare pixel data
+ return list(img1.getdata()) == list(img2.getdata())
+ except Exception as e:
+ logger.warning(f'Failed to compare images {img_path1} and {img_path2}: {e}')
+ return False
+
+
+def filter_images(screenshot_paths: list[str], max_images: int) -> list[str]:
+ """
+ Filter screenshot paths to:
+ 1. Never include the first image (always white)
+ 2. Remove consecutive duplicate images
+ 3. Return up to max_images from the end
+ """
+ if not screenshot_paths:
+ return []
+
+ # Skip the first image (always white)
+ filtered_paths = screenshot_paths[1:] if len(screenshot_paths) > 1 else []
+
+ if not filtered_paths:
+ return []
+
+ # Remove consecutive duplicates
+ deduplicated_paths = [filtered_paths[0]] # Always include the first non-skipped image
+
+ for i in range(1, len(filtered_paths)):
+ current_path = filtered_paths[i]
+ previous_path = filtered_paths[i - 1]
+
+ # Only add if not identical to previous image
+ if not are_images_identical(current_path, previous_path):
+ deduplicated_paths.append(current_path)
+
+ # Return last max_images images
+ return deduplicated_paths[-max_images:] if len(deduplicated_paths) > max_images else deduplicated_paths
+
+
async def comprehensive_judge(
task: str,
complete_history: list[dict],
@@ -194,8 +245,8 @@ async def comprehensive_judge(
final_result_truncated = truncate_text(final_result or 'No final result', 40000)
agent_steps = prepare_agent_steps(complete_history)
- # Select last N images
- selected_images = screenshot_paths[-max_images:] if screenshot_paths else []
+ # Select and filter images
+ selected_images = filter_images(screenshot_paths, max_images)
# Encode images
encoded_images = []