File size: 10,701 Bytes
a49c70b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106517e
a49c70b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106517e
a49c70b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106517e
a49c70b
 
 
 
106517e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a49c70b
106517e
a49c70b
106517e
a49c70b
 
 
 
 
 
 
 
 
 
 
106517e
a49c70b
 
 
 
106517e
 
 
a49c70b
 
 
 
 
 
106517e
 
 
 
 
 
 
 
a49c70b
106517e
a49c70b
106517e
a49c70b
106517e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a49c70b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""Main pipeline: orchestrates segmentation, saliency, and modifications."""

from __future__ import annotations

import logging
import random
import time
from typing import Callable

import cv2
import numpy as np

from src.models.segment import Segment
from src.models.difference import Difference, GenerationResult
from src.services.segmentation import SegmentationService
from src.services.saliency import SaliencyService
from src.services.inpainting import InpaintingService
from src.services.color_changer import ColorChanger
from src.services.object_duplicator import ObjectDuplicator
from src.services.quality_evaluator import QualityEvaluator

logger = logging.getLogger(__name__)

ProgressCallback = Callable[[int, str], None] | None


class DifferenceGenerator:
    """Generates spot-the-difference images from a single input image."""

    def __init__(
        self,
        segmentation: SegmentationService,
        saliency: SaliencyService,
        inpainting: InpaintingService,
        color_changer: ColorChanger,
        object_duplicator: ObjectDuplicator,
        difficulty_config: dict,
        segment_min_area_ratio: float = 0.002,
        segment_max_area_ratio: float = 0.15,
    ) -> None:
        self._seg = segmentation
        self._sal = saliency
        self._inp = inpainting
        self._col = color_changer
        self._dup = object_duplicator
        self._quality = QualityEvaluator()
        self._difficulty_config = difficulty_config
        self._min_area_ratio = segment_min_area_ratio
        self._max_area_ratio = segment_max_area_ratio

    def generate(
        self,
        image: np.ndarray,
        difficulty: str = "medium",
        progress: ProgressCallback = None,
    ) -> GenerationResult:
        """Run the full generation pipeline.

        Args:
            image: BGR image (H, W, 3) uint8.
            difficulty: "easy", "medium", or "hard".
            progress: Optional callback(percent, step_name).

        Returns:
            GenerationResult with original, modified image, and difference metadata.
        """
        timings: dict[str, float] = {}
        _notify(progress, 5, "セグメンテーション開始...")

        # 1. Segmentation
        t0 = time.time()
        segments = self._seg.segment(
            image,
            min_area_ratio=self._min_area_ratio,
            max_area_ratio=self._max_area_ratio,
        )
        timings["segmentation"] = time.time() - t0
        _notify(progress, 40, f"セグメンテーション完了 ({len(segments)}個検出)")

        if len(segments) < 2:
            logger.warning("Too few segments (%d) to generate differences.", len(segments))
            return GenerationResult(
                original_image=image,
                modified_image=image.copy(),
                differences=[],
                metadata={"error": "検出されたオブジェクトが少なすぎます"},
            )

        # 2. Saliency analysis
        t0 = time.time()
        saliency_map = self._sal.compute_map(image)
        ranked = self._sal.rank_segments(segments, saliency_map)
        timings["saliency"] = time.time() - t0
        _notify(progress, 50, "顕著性解析完了")

        # 3. Select segments based on difficulty
        selected = self._select_segments(ranked, difficulty)
        _notify(progress, 55, f"{len(selected)}個のオブジェクトを変更します")

        # 4. Apply changes
        t0 = time.time()
        modified, differences = self._apply_changes(image, selected, progress)
        timings["changes"] = time.time() - t0

        total_time = sum(timings.values())
        timings["total"] = total_time

        _notify(progress, 95, "メタデータを生成中...")

        metadata = {
            "difficulty": difficulty,
            "processing_times": {k: round(v, 2) for k, v in timings.items()},
            "segments_detected": len(segments),
            "model_versions": {
                "segmentation": "FastSAM-x",
                "saliency": "OpenCV SpectralResidual",
                "inpainting": "OpenCV Navier-Stokes",
            },
        }

        _notify(progress, 100, "完了")

        return GenerationResult(
            original_image=image,
            modified_image=modified,
            differences=differences,
            metadata=metadata,
        )

    def _select_segments(
        self, ranked: list[Segment], difficulty: str
    ) -> list[Segment]:
        """Pick segments based on difficulty settings with quality filtering."""
        config = self._difficulty_config[difficulty]
        num_changes = config["num_changes"]
        max_saliency = config["max_saliency"]

        # First, filter by quality
        quality_filtered = []
        for seg in ranked:
            is_acceptable, quality_score, reason = self._quality.evaluate_segment_quality(
                np.zeros((seg.mask.shape[0], seg.mask.shape[1], 3), dtype=np.uint8),
                seg,
            )
            if is_acceptable:
                seg.quality_score = quality_score  # Store for reference
                quality_filtered.append(seg)
            else:
                logger.debug(f"Segment {seg.id} rejected: {reason}")

        logger.info(f"Quality filtered: {len(quality_filtered)} / {len(ranked)} segments passed")

        # Then filter by saliency
        candidates = [s for s in quality_filtered if s.saliency_score <= max_saliency]

        # If not enough candidates below threshold, relax and take from full quality list
        if len(candidates) < num_changes:
            candidates = quality_filtered.copy()

        # If still not enough, take from ranked list (without quality filter)
        if len(candidates) < num_changes:
            logger.warning(f"Not enough high-quality segments, relaxing quality requirements")
            candidates = ranked.copy()

        n = min(num_changes, len(candidates))
        return random.sample(candidates, n)

    def _apply_changes(
        self,
        image: np.ndarray,
        segments: list[Segment],
        progress: ProgressCallback,
    ) -> tuple[np.ndarray, list[Difference]]:
        """Apply a random change to each selected segment with quality checking."""
        modified = image.copy()
        differences: list[Difference] = []

        total = len(segments)
        successful_changes = 0
        max_retries = 2

        for i, seg in enumerate(segments):
            pct = 55 + int((i / max(total, 1)) * 35)
            change_type = self._decide_change_type(seg)

            _notify(progress, pct, f"変更を適用中 ({i + 1}/{total}): {change_type}")

            # Try to apply change with quality check
            for attempt in range(max_retries):
                temp_modified = modified.copy()
                x1, y1, x2, y2 = seg.bbox
                original_region = image[y1:y2, x1:x2].copy()
                new_bbox_result = None

                # Apply the change
                if change_type == "deletion":
                    temp_modified = self._inp.inpaint(temp_modified, seg.mask)
                elif change_type == "color_change":
                    temp_modified, _ = self._col.change_hue(temp_modified, seg.mask)
                elif change_type == "addition":
                    temp_modified, new_bbox_result = self._dup.duplicate(temp_modified, seg)
                    if new_bbox_result is None:
                        logger.debug(f"Addition failed for segment {seg.id}, trying different type")
                        change_type = random.choice(["deletion", "color_change"])
                        continue

                # Check quality of the modification
                modified_region = temp_modified[y1:y2, x1:x2]
                local_mask = seg.mask[y1:y2, x1:x2]

                is_acceptable, quality_score, reason = self._quality.evaluate_modification_quality(
                    original_region,
                    modified_region,
                    local_mask,
                    change_type,
                )

                if is_acceptable or attempt == max_retries - 1:
                    # Accept this modification
                    modified = temp_modified
                    diff = self._apply_single_change(modified, seg, change_type, successful_changes + 1)

                    if diff is not None:
                        if change_type == "addition" and new_bbox_result is not None:
                            diff.bbox = new_bbox_result
                        differences.append(diff)
                        successful_changes += 1

                    if not is_acceptable:
                        logger.warning(f"Accepting lower quality change ({reason}) after {max_retries} attempts")
                    else:
                        logger.debug(f"Change accepted with quality score: {quality_score:.2f}")
                    break
                else:
                    logger.debug(f"Modification rejected ({reason}), retrying with different parameters...")
                    # Try a different change type on retry
                    if change_type == "color_change":
                        change_type = random.choice(["deletion", "addition"])
                    elif change_type == "addition":
                        change_type = "color_change"

        logger.info(f"Successfully applied {successful_changes} / {total} changes")
        return modified, differences

    def _apply_single_change(
        self,
        image: np.ndarray,
        seg: Segment,
        change_type: str,
        diff_id: int,
    ) -> Difference | None:
        """Create a Difference record. Actual mutation happens in _apply_changes."""
        descriptions = {
            "deletion": "オブジェクトを削除",
            "color_change": "色を変更",
            "addition": "オブジェクトを追加",
        }
        return Difference(
            id=diff_id,
            type=change_type,
            bbox=seg.bbox,
            saliency_score=seg.saliency_score,
            description=descriptions.get(change_type, ""),
        )

    def _decide_change_type(self, seg: Segment) -> str:
        """Choose change type, preferring colour/addition for large objects."""
        image_area_ratio = seg.area / max(seg.mask.size, 1)
        if image_area_ratio > 0.08:
            # Large object — inpainting quality degrades, avoid deletion
            return random.choice(["color_change", "addition"])
        return random.choice(["deletion", "color_change", "addition"])


def _notify(cb: ProgressCallback, percent: int, step: str) -> None:
    if cb is not None:
        cb(percent, step)