소스 검색

优化代码base64字符替换

zhangdongming 3 주 전
부모
커밋
feef2d656c
1개의 변경된 파일49개의 추가작업 그리고 15개의 파일을 삭제
  1. 49 15
      Object/NovaImageTagObject.py

+ 49 - 15
Object/NovaImageTagObject.py

@@ -7,12 +7,13 @@
 @Software: PyCharm
 """
 import base64
-import imghdr
+import io
 import json
 import logging
 import re
 
 import boto3
+from PIL import Image
 
 LOGGER = logging.getLogger('time')
 
@@ -87,28 +88,61 @@ class NovaImageTagObject(object):
             })
         return formatted_results
 
+    @staticmethod
+    def normalize_b64(b64_str: str) -> str:
+        """清理并验证base64字符串"""
+        if not b64_str:
+            return ""
+
+        # 移除所有非Base64字符(包括空格、换行等)
+        b64_str = re.sub(r"[^A-Za-z0-9+/=]", "", b64_str)
+
+        # 检查Base64有效性
+        if len(b64_str) % 4 != 0:
+            # 自动补全填充位
+            b64_str += "=" * (4 - len(b64_str) % 4)
+
+        return b64_str
+
     def process_image_batch(self, base64_images: list, categories: list, uid=''):
-        """
-        通过单次API调用处理一批图片,并返回结构化的检测结果。
-        """
         if not base64_images:
             LOGGER.error(f"{uid}错误: 未提供图片数据。")
             return {}
 
         image_contents = []
-        img_bytes_list = []
-        for b64_image in base64_images:
+        for idx, b64_image in enumerate(base64_images, start=1):
             try:
-                LOGGER.info('{}:{}'.format(uid, b64_image))
-                img_bytes = base64.b64decode(b64_image)
-                img_type = imghdr.what(None, h=img_bytes)
-                if img_type.lower() not in ["jpeg", "jpg", "png", "webp"]:
-                    raise ValueError(f"不支持的图片格式: {img_type}")
-                image_contents.append({"image": {"format": img_type, "source": {"bytes": img_bytes}}})
-                img_bytes_list.append(img_bytes)
+                # 规范化base64
+                val = b64_image.replace(' ', '+')
+                img_bytes = base64.b64decode(val)
+
+                # 验证解码后的数据
+                if len(img_bytes) == 0:
+                    raise ValueError("解码后得到空字节数据")
+
+                # 使用PIL处理图像
+                image = Image.open(io.BytesIO(img_bytes))
+
+                # 转换为RGB模式(如果需要)
+                if image.mode != 'RGB':
+                    image = image.convert('RGB')
+
+                # 转换为WebP格式
+                buffer = io.BytesIO()
+                image.save(buffer, format="webp", quality=90)
+                webp_bytes = buffer.getvalue()
+
+                image_contents.append({
+                    "image": {"format": "webp", "source": {"bytes": webp_bytes}}
+                })
+
+                LOGGER.info(f"{uid} 第{idx}张图处理成功, 格式=webp, 大小={len(webp_bytes)}B")
+
             except Exception as e:
-                LOGGER.error(f"{uid}处理图片时出错,已跳过: {repr(e)}")
-                img_bytes_list.append(None)  # 添加占位符以保持索引一致
+                LOGGER.error(f"{uid} 第{idx}张图处理失败: {repr(e)}")
+                LOGGER.debug(f"{uid}失败图像的Base64前100字符: {b64_image[:100]}")
+                # 不要添加None,而是跳过或使用占位符图像
+                continue  # 直接跳过这张图
 
         if not image_contents:
             LOGGER.error(f"{uid}错误: 所有图片均无法处理。")