(image_bytes: bytes, prompt_text: str | None = None, include_debug: bool = False)
| 826 | |
| 827 | engine = get_engine() |
| 828 | ocr_result = engine(image) |
| 829 | items = ocr_items(ocr_result) |
| 830 | |
| 831 | prompt_item = None |
| 832 | prompt_chars = parse_prompt_chars(prompt_text) if prompt_text else [] |
| 833 | |
| 834 | if len(prompt_chars) < 2: |
| 835 | try: |
| 836 | found_prompt_item, found_prompt_chars = build_prompt_chars(items, image, engine) |
| 837 | prompt_item = found_prompt_item |
| 838 | prompt_chars = found_prompt_chars |
| 839 | except RuntimeError: |
| 840 | raise RuntimeError('未识别到验证码提示文本,当前截图疑似不是验证码弹窗。') |
| 841 | |
| 842 | if prompt_item is not None and not is_prompt_like_text(prompt_item['text']) and len(parse_prompt_chars(prompt_item['text'])) <= 2: |
| 843 | raise RuntimeError(f'识别到的顶部文本不像验证码提示:{prompt_item["text"]}') |
| 844 | |
| 845 | candidate_chars = build_candidate_chars( |
| 846 | image, |
| 847 | items, |
| 848 | prompt_item, |
| 849 | image.shape[0], |
| 850 | image.shape[1], |
| 851 | prompt_chars=prompt_chars, |
| 852 | engine=engine, |
| 853 | ) |
| 854 | click_items = match_prompt_to_candidates(prompt_chars, candidate_chars) |
| 855 | fallback_method = '' |
| 856 | |
| 857 | if any(item is None for item in click_items) and len(prompt_chars) < 2: |
| 858 | top_result = engine(image[: int(image.shape[0] * 0.16), :]) |
| 859 | top_items = ocr_items(top_result) |
| 860 | try: |
| 861 | extra_prompt_item, extra_prompt_chars = build_prompt_chars(top_items, image, engine) |
| 862 | if len(extra_prompt_chars) > len(prompt_chars): |
| 863 | prompt_item = extra_prompt_item |
| 864 | prompt_chars = extra_prompt_chars |
| 865 | candidate_chars = build_candidate_chars( |
| 866 | image, |
| 867 | items, |
| 868 | prompt_item, |
| 869 | image.shape[0], |
| 870 | image.shape[1], |
| 871 | prompt_chars=prompt_chars, |
| 872 | engine=engine, |
| 873 | ) |
| 874 | click_items = match_prompt_to_candidates(prompt_chars, candidate_chars) |
| 875 | except RuntimeError: |
| 876 | pass |
| 877 | |
| 878 | if len(prompt_chars) >= 2: |
| 879 | current_matches = sum(1 for item in click_items if item is not None) |
| 880 | yellow_candidates = build_yellow_fallback_candidates(image, prompt_chars, engine) |
| 881 | yellow_click_items = match_prompt_to_candidates(prompt_chars, yellow_candidates) |
| 882 | yellow_matches = sum(1 for item in yellow_click_items if item is not None) |
| 883 | if yellow_matches == len(prompt_chars) and yellow_matches > current_matches: |
| 884 | candidate_chars = yellow_candidates |
| 885 | click_items = yellow_click_items |
no test coverage detected