fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解 - 修复缺失的urllib.parse导入
This commit is contained in:
@@ -33,6 +33,7 @@ try:
|
||||
except ImportError:
|
||||
PYTESSERACT_AVAILABLE = False
|
||||
|
||||
|
||||
@dataclass
|
||||
class ImageEntity:
|
||||
"""图片中检测到的实体"""
|
||||
@@ -42,6 +43,7 @@ class ImageEntity:
|
||||
confidence: float
|
||||
bbox: tuple[int, int, int, int] | None = None # (x, y, width, height)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ImageRelation:
|
||||
"""图片中检测到的关系"""
|
||||
@@ -51,6 +53,7 @@ class ImageRelation:
|
||||
relation_type: str
|
||||
confidence: float
|
||||
|
||||
|
||||
@dataclass
|
||||
class ImageProcessingResult:
|
||||
"""图片处理结果"""
|
||||
@@ -66,6 +69,7 @@ class ImageProcessingResult:
|
||||
success: bool
|
||||
error_message: str = ""
|
||||
|
||||
|
||||
@dataclass
|
||||
class BatchProcessingResult:
|
||||
"""批量图片处理结果"""
|
||||
@@ -75,6 +79,7 @@ class BatchProcessingResult:
|
||||
success_count: int
|
||||
failed_count: int
|
||||
|
||||
|
||||
class ImageProcessor:
|
||||
"""图片处理器 - 处理各种类型图片"""
|
||||
|
||||
@@ -213,7 +218,10 @@ class ImageProcessor:
|
||||
return "handwritten"
|
||||
|
||||
# 检测是否为截图(可能有UI元素)
|
||||
if any(keyword in ocr_text.lower() for keyword in ["button", "menu", "click", "登录", "确定", "取消"]):
|
||||
if any(
|
||||
keyword in ocr_text.lower()
|
||||
for keyword in ["button", "menu", "click", "登录", "确定", "取消"]
|
||||
):
|
||||
return "screenshot"
|
||||
|
||||
# 默认文档类型
|
||||
@@ -316,7 +324,9 @@ class ImageProcessor:
|
||||
|
||||
return unique_entities
|
||||
|
||||
def generate_description(self, image_type: str, ocr_text: str, entities: list[ImageEntity]) -> str:
|
||||
def generate_description(
|
||||
self, image_type: str, ocr_text: str, entities: list[ImageEntity]
|
||||
) -> str:
|
||||
"""
|
||||
生成图片描述
|
||||
|
||||
@@ -346,7 +356,11 @@ class ImageProcessor:
|
||||
return " ".join(description_parts)
|
||||
|
||||
def process_image(
|
||||
self, image_data: bytes, filename: str = None, image_id: str = None, detect_type: bool = True
|
||||
self,
|
||||
image_data: bytes,
|
||||
filename: str = None,
|
||||
image_id: str = None,
|
||||
detect_type: bool = True,
|
||||
) -> ImageProcessingResult:
|
||||
"""
|
||||
处理单张图片
|
||||
@@ -469,7 +483,9 @@ class ImageProcessor:
|
||||
|
||||
return relations
|
||||
|
||||
def process_batch(self, images_data: list[tuple[bytes, str]], project_id: str = None) -> BatchProcessingResult:
|
||||
def process_batch(
|
||||
self, images_data: list[tuple[bytes, str]], project_id: str = None
|
||||
) -> BatchProcessingResult:
|
||||
"""
|
||||
批量处理图片
|
||||
|
||||
@@ -494,7 +510,10 @@ class ImageProcessor:
|
||||
failed_count += 1
|
||||
|
||||
return BatchProcessingResult(
|
||||
results=results, total_count=len(results), success_count=success_count, failed_count=failed_count
|
||||
results=results,
|
||||
total_count=len(results),
|
||||
success_count=success_count,
|
||||
failed_count=failed_count,
|
||||
)
|
||||
|
||||
def image_to_base64(self, image_data: bytes) -> str:
|
||||
@@ -534,9 +553,11 @@ class ImageProcessor:
|
||||
print(f"Thumbnail generation error: {e}")
|
||||
return image_data
|
||||
|
||||
|
||||
# Singleton instance
|
||||
_image_processor = None
|
||||
|
||||
|
||||
def get_image_processor(temp_dir: str = None) -> ImageProcessor:
|
||||
"""获取图片处理器单例"""
|
||||
global _image_processor
|
||||
|
||||
Reference in New Issue
Block a user