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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Caption Integration Module
|
| 4 |
+
Adds Qwen VL captions to template matching results
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from typing import Dict, List, Any
|
| 8 |
+
import json
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
from caption_cropped_images import get_captions
|
| 11 |
+
|
| 12 |
+
def add_captions_to_matches(
|
| 13 |
+
matches: List[Dict],
|
| 14 |
+
captions: Dict[str, str]
|
| 15 |
+
) -> List[Dict]:
|
| 16 |
+
"""
|
| 17 |
+
Add captions to template matches.
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
matches: List of matched elements from template matching
|
| 21 |
+
captions: Dictionary of crop_id → caption text
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
Matches with captions added
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
for match in matches:
|
| 28 |
+
crop_id = match.get('template_id', '')
|
| 29 |
+
caption = captions.get(crop_id, 'unknown')
|
| 30 |
+
match['caption'] = caption
|
| 31 |
+
|
| 32 |
+
return matches
|
| 33 |
+
|
| 34 |
+
def add_captions_to_coordinates(
|
| 35 |
+
coordinates: List[Dict],
|
| 36 |
+
captions: Dict[str, str]
|
| 37 |
+
) -> List[Dict]:
|
| 38 |
+
"""
|
| 39 |
+
Add captions to coordinates output.
|
| 40 |
+
|
| 41 |
+
Args:
|
| 42 |
+
coordinates: List of detected elements
|
| 43 |
+
captions: Dictionary of crop_id → caption
|
| 44 |
+
|
| 45 |
+
Returns:
|
| 46 |
+
Coordinates with captions added
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
for elem in coordinates:
|
| 50 |
+
crop_id = elem.get('template_id') or elem.get('element_id', '')
|
| 51 |
+
caption = captions.get(crop_id, 'unknown')
|
| 52 |
+
elem['caption'] = caption
|
| 53 |
+
|
| 54 |
+
return coordinates
|
| 55 |
+
|
| 56 |
+
def create_labeled_output(
|
| 57 |
+
analysis_result: Dict,
|
| 58 |
+
captions: Dict[str, str],
|
| 59 |
+
include_caption: str = "caption"
|
| 60 |
+
) -> Dict:
|
| 61 |
+
"""
|
| 62 |
+
Create labeled output with captions.
|
| 63 |
+
|
| 64 |
+
Args:
|
| 65 |
+
analysis_result: Original analysis result
|
| 66 |
+
captions: Captions dictionary
|
| 67 |
+
include_caption: Field name for caption ('caption', 'label', 'description')
|
| 68 |
+
|
| 69 |
+
Returns:
|
| 70 |
+
Updated analysis result with captions
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
if 'analysis' in analysis_result and 'elements' in analysis_result['analysis']:
|
| 74 |
+
for elem in analysis_result['analysis']['elements']:
|
| 75 |
+
crop_id = elem.get('template_id') or elem.get('element_id', '')
|
| 76 |
+
caption = captions.get(crop_id, 'unknown')
|
| 77 |
+
elem[include_caption] = caption
|
| 78 |
+
|
| 79 |
+
if 'exports' in analysis_result:
|
| 80 |
+
analysis_result['exports']['captions'] = captions
|
| 81 |
+
|
| 82 |
+
return analysis_result
|
| 83 |
+
|
| 84 |
+
def generate_csv_with_captions(
|
| 85 |
+
coordinates: List[Dict],
|
| 86 |
+
include_caption: bool = True
|
| 87 |
+
) -> str:
|
| 88 |
+
"""
|
| 89 |
+
Generate CSV with captions.
|
| 90 |
+
|
| 91 |
+
Args:
|
| 92 |
+
coordinates: List of detected elements with captions
|
| 93 |
+
include_caption: Whether to include caption column
|
| 94 |
+
|
| 95 |
+
Returns:
|
| 96 |
+
CSV string
|
| 97 |
+
"""
|
| 98 |
+
|
| 99 |
+
import io
|
| 100 |
+
import csv
|
| 101 |
+
|
| 102 |
+
output = io.StringIO()
|
| 103 |
+
|
| 104 |
+
# Define columns
|
| 105 |
+
if include_caption:
|
| 106 |
+
columns = [
|
| 107 |
+
'Element_ID', 'Caption', 'X', 'Y', 'X1', 'Y1', 'X2', 'Y2',
|
| 108 |
+
'Width', 'Height', 'Confidence'
|
| 109 |
+
]
|
| 110 |
+
else:
|
| 111 |
+
columns = [
|
| 112 |
+
'Element_ID', 'X', 'Y', 'X1', 'Y1', 'X2', 'Y2',
|
| 113 |
+
'Width', 'Height', 'Confidence'
|
| 114 |
+
]
|
| 115 |
+
|
| 116 |
+
writer = csv.writer(output)
|
| 117 |
+
writer.writerow(columns)
|
| 118 |
+
|
| 119 |
+
for i, coord in enumerate(coordinates, 1):
|
| 120 |
+
element_id = coord.get('element_id', f"crop_{i:04d}")
|
| 121 |
+
caption = coord.get('caption', 'unknown')
|
| 122 |
+
x = coord.get('x', 0)
|
| 123 |
+
y = coord.get('y', 0)
|
| 124 |
+
x1 = coord.get('x1', 0)
|
| 125 |
+
y1 = coord.get('y1', 0)
|
| 126 |
+
x2 = coord.get('x2', 0)
|
| 127 |
+
y2 = coord.get('y2', 0)
|
| 128 |
+
width = coord.get('width', 0)
|
| 129 |
+
height = coord.get('height', 0)
|
| 130 |
+
confidence = coord.get('confidence', 0)
|
| 131 |
+
|
| 132 |
+
if include_caption:
|
| 133 |
+
writer.writerow([
|
| 134 |
+
element_id, caption, x, y, x1, y1, x2, y2,
|
| 135 |
+
width, height, f"{confidence:.4f}"
|
| 136 |
+
])
|
| 137 |
+
else:
|
| 138 |
+
writer.writerow([
|
| 139 |
+
element_id, x, y, x1, y1, x2, y2,
|
| 140 |
+
width, height, f"{confidence:.4f}"
|
| 141 |
+
])
|
| 142 |
+
|
| 143 |
+
return output.getvalue()
|
| 144 |
+
|
| 145 |
+
if __name__ == "__main__":
|
| 146 |
+
# Example usage
|
| 147 |
+
print("[Test] Caption Integration Module")
|
| 148 |
+
|
| 149 |
+
# Load test captions
|
| 150 |
+
test_captions = {
|
| 151 |
+
"crop_0001": "close button",
|
| 152 |
+
"crop_0002": "search input",
|
| 153 |
+
"crop_0003": "menu icon"
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
# Test with sample coordinates
|
| 157 |
+
sample_coords = [
|
| 158 |
+
{
|
| 159 |
+
"element_id": "crop_0001",
|
| 160 |
+
"x": 100,
|
| 161 |
+
"y": 50,
|
| 162 |
+
"confidence": 0.95
|
| 163 |
+
}
|
| 164 |
+
]
|
| 165 |
+
|
| 166 |
+
# Add captions
|
| 167 |
+
labeled = add_captions_to_coordinates(sample_coords, test_captions)
|
| 168 |
+
print(f"✓ Added captions: {labeled}")
|