处理入口 - 分组提取图片中的所有元素 Args: context: 处理上下文,需要包含 image_path Returns: ProcessingResult: 包含所有提取的ElementInfo
(self, context: ProcessingContext)
| 407 | self._sam3_model.load() |
| 408 | |
| 409 | def process(self, context: ProcessingContext) -> ProcessingResult: |
| 410 | """ |
| 411 | 处理入口 - 分组提取图片中的所有元素 |
| 412 | |
| 413 | Args: |
| 414 | context: 处理上下文,需要包含 image_path |
| 415 | |
| 416 | Returns: |
| 417 | ProcessingResult: 包含所有提取的ElementInfo |
| 418 | """ |
| 419 | self._log(f"开始处理: {context.image_path}") |
| 420 | |
| 421 | # 保存当前图像路径(供去重分析使用) |
| 422 | self._current_image_path = context.image_path |
| 423 | |
| 424 | self.load_model() |
| 425 | |
| 426 | pil_image = Image.open(context.image_path) |
| 427 | context.canvas_width, context.canvas_height = pil_image.size |
| 428 | |
| 429 | all_elements = [] |
| 430 | group_stats = {} |
| 431 | process_order = [ |
| 432 | PromptGroup.BACKGROUND, |
| 433 | PromptGroup.BASIC_SHAPE, |
| 434 | PromptGroup.IMAGE, |
| 435 | PromptGroup.ARROW |
| 436 | ] |
| 437 | |
| 438 | for group_type in process_order: |
| 439 | if group_type not in self.prompt_groups: |
| 440 | continue |
| 441 | |
| 442 | group_config = self.prompt_groups[group_type] |
| 443 | |
| 444 | if not group_config.prompts: |
| 445 | continue |
| 446 | |
| 447 | self._log(f" 处理组 [{group_config.name}]: {len(group_config.prompts)}个提示词") |
| 448 | |
| 449 | raw_results = self._sam3_model.predict( |
| 450 | context.image_path, |
| 451 | group_config.prompts, |
| 452 | score_threshold=group_config.score_threshold, |
| 453 | min_area=group_config.min_area |
| 454 | ) |
| 455 | raw_results = self._filter_text_elements(raw_results) |
| 456 | |
| 457 | elements = self._convert_to_elements( |
| 458 | raw_results, |
| 459 | start_id=len(all_elements), |
| 460 | source_group=group_type.value, |
| 461 | group_priority=group_config.priority |
| 462 | ) |
| 463 | |
| 464 | all_elements.extend(elements) |
| 465 | group_stats[group_config.name] = len(elements) |
| 466 |
no test coverage detected