MCPcopy
hub / github.com/sml2h3/ddddocr / predict

Method predict

ddddocr/core/detection_engine.py:49–87  ·  view source on GitHub ↗

执行目标检测 Args: image: 输入图像 Returns: 检测到的边界框列表,每个边界框格式为[x1, y1, x2, y2] Raises: ImageProcessError: 当图像处理失败时 ModelLoadError: 当模型未初始化时

(self, image: Union[bytes, str, Image.Image])

Source from the content-addressed store, hash-verified

47 raise ModelLoadError(f"检测引擎初始化失败: {str(e)}") from e
48
49 def predict(self, image: Union[bytes, str, Image.Image]) -> List[List[int]]:
50 """
51 执行目标检测
52
53 Args:
54 image: 输入图像
55
56 Returns:
57 检测到的边界框列表,每个边界框格式为[x1, y1, x2, y2]
58
59 Raises:
60 ImageProcessError: 当图像处理失败时
61 ModelLoadError: 当模型未初始化时
62 """
63 if not self.is_ready():
64 raise ModelLoadError("检测引擎未初始化")
65
66 # 验证输入
67 validate_image_input(image)
68
69 try:
70 # 直接使用原始的get_bbox方法
71 if isinstance(image, bytes):
72 return self.get_bbox(image)
73 elif isinstance(image, Image.Image):
74 import io
75 img_bytes = io.BytesIO()
76 image.save(img_bytes, format='PNG')
77 return self.get_bbox(img_bytes.getvalue())
78 else:
79 # 其他类型先转换为PIL Image再处理
80 pil_image = load_image_from_input(image)
81 import io
82 img_bytes = io.BytesIO()
83 pil_image.save(img_bytes, format='PNG')
84 return self.get_bbox(img_bytes.getvalue())
85
86 except Exception as e:
87 raise ImageProcessError(f"目标检测失败: {str(e)}") from e
88
89 def preproc(self, img, input_size, swap=(2, 0, 1)):
90 """预处理函数(来自原始代码)"""

Callers

nothing calls this directly

Calls 6

get_bboxMethod · 0.95
ModelLoadErrorClass · 0.85
validate_image_inputFunction · 0.85
load_image_from_inputFunction · 0.85
ImageProcessErrorClass · 0.85
is_readyMethod · 0.45

Tested by

no test coverage detected