guohanghui commited on
Commit
452bc23
·
verified ·
1 Parent(s): f1887b6

Update tesseract/mcp_output/mcp_plugin/mcp_service.py

Browse files
tesseract/mcp_output/mcp_plugin/mcp_service.py CHANGED
@@ -7,6 +7,7 @@ sys.path.insert(0, source_path)
7
  from fastmcp import FastMCP
8
  from PIL import Image
9
  import pytesseract
 
10
 
11
  mcp = FastMCP("tesseract_service")
12
 
@@ -51,24 +52,34 @@ def set_image(image_data: bytes, width: int, height: int) -> dict:
51
  return {"success": False, "result": None, "error": str(e)}
52
 
53
  @mcp.tool(name="recognize_text", description="Perform OCR on the provided image and return recognized text.")
54
- def recognize_text(image_path: str) -> dict:
55
  """
56
  Perform OCR on the provided image and return recognized text.
57
 
58
  Args:
59
  image_path (str): Path to the image to recognize.
 
60
 
61
  Returns:
62
  dict: A dictionary containing recognized text or error message.
63
  """
64
  try:
65
- # 检查图片路径是否存在
66
- if not os.path.exists(image_path):
67
- return {"success": False, "result": None, "error": f"Image not found: {image_path}"}
68
-
69
- # 使用 PIL 打开图片
70
- image = Image.open(image_path)
71
- print(f"Processing image: {image_path}, size: {image.size}")
 
 
 
 
 
 
 
 
 
72
 
73
  # 尝试多种语言配置进行OCR识别
74
  recognized_text = ""
 
7
  from fastmcp import FastMCP
8
  from PIL import Image
9
  import pytesseract
10
+ import io
11
 
12
  mcp = FastMCP("tesseract_service")
13
 
 
52
  return {"success": False, "result": None, "error": str(e)}
53
 
54
  @mcp.tool(name="recognize_text", description="Perform OCR on the provided image and return recognized text.")
55
+ def recognize_text(image_path: str = None, image_base64: str = None) -> dict:
56
  """
57
  Perform OCR on the provided image and return recognized text.
58
 
59
  Args:
60
  image_path (str): Path to the image to recognize.
61
+ image_base64 (str): Base64 encoded image data.
62
 
63
  Returns:
64
  dict: A dictionary containing recognized text or error message.
65
  """
66
  try:
67
+ # 处理base64数据或文件路径
68
+ if image_base64:
69
+ # 从base64数据创建图片
70
+ import base64
71
+ image_data = base64.b64decode(image_base64)
72
+ image = Image.open(io.BytesIO(image_data))
73
+ print(f"Processing base64 image, size: {image.size}")
74
+ elif image_path:
75
+ # 检查图片路径是否存在
76
+ if not os.path.exists(image_path):
77
+ return {"success": False, "result": None, "error": f"Image not found: {image_path}"}
78
+ # 使用 PIL 打开图片
79
+ image = Image.open(image_path)
80
+ print(f"Processing image: {image_path}, size: {image.size}")
81
+ else:
82
+ return {"success": False, "result": None, "error": "Either image_path or image_base64 must be provided"}
83
 
84
  # 尝试多种语言配置进行OCR识别
85
  recognized_text = ""