|
@@ -87,6 +87,17 @@ class NovaImageTagObject(object):
|
|
})
|
|
})
|
|
return formatted_results
|
|
return formatted_results
|
|
|
|
|
|
|
|
+ def normalize_b64(self, b64_str: str) -> str:
|
|
|
|
+ """清理并补齐base64字符串"""
|
|
|
|
+ if not b64_str:
|
|
|
|
+ return ""
|
|
|
|
+ b64_str = b64_str.strip().replace("\n", "").replace(" ", "")
|
|
|
|
+ # 补齐到4的倍数
|
|
|
|
+ padding = 4 - (len(b64_str) % 4)
|
|
|
|
+ if padding and padding != 4:
|
|
|
|
+ b64_str += "=" * padding
|
|
|
|
+ return b64_str
|
|
|
|
+
|
|
def process_image_batch(self, base64_images: list, categories: list, uid=''):
|
|
def process_image_batch(self, base64_images: list, categories: list, uid=''):
|
|
"""
|
|
"""
|
|
通过单次API调用处理一批图片,并返回结构化的检测结果。
|
|
通过单次API调用处理一批图片,并返回结构化的检测结果。
|
|
@@ -97,16 +108,28 @@ class NovaImageTagObject(object):
|
|
|
|
|
|
image_contents = []
|
|
image_contents = []
|
|
img_bytes_list = []
|
|
img_bytes_list = []
|
|
- for b64_image in base64_images:
|
|
|
|
|
|
+ for idx, b64_image in enumerate(base64_images, start=1):
|
|
try:
|
|
try:
|
|
|
|
+ # 规范化 base64
|
|
|
|
+ b64_image = self.normalize_b64(b64_image)
|
|
|
|
+ if not b64_image:
|
|
|
|
+ raise ValueError("空的base64字符串")
|
|
|
|
+
|
|
img_bytes = base64.b64decode(b64_image)
|
|
img_bytes = base64.b64decode(b64_image)
|
|
img_type = imghdr.what(None, h=img_bytes)
|
|
img_type = imghdr.what(None, h=img_bytes)
|
|
- if img_type.lower() not in ["jpeg", "jpg", "png", "webp"]:
|
|
|
|
|
|
+
|
|
|
|
+ if not img_type or img_type.lower() not in ["jpeg", "jpg", "png", "webp"]:
|
|
raise ValueError(f"不支持的图片格式: {img_type}")
|
|
raise ValueError(f"不支持的图片格式: {img_type}")
|
|
- image_contents.append({"image": {"format": img_type, "source": {"bytes": img_bytes}}})
|
|
|
|
|
|
+
|
|
|
|
+ image_contents.append({
|
|
|
|
+ "image": {"format": img_type, "source": {"bytes": img_bytes}}
|
|
|
|
+ })
|
|
img_bytes_list.append(img_bytes)
|
|
img_bytes_list.append(img_bytes)
|
|
|
|
+
|
|
|
|
+ LOGGER.info(f"{uid} 第{idx}张图处理成功, 格式={img_type}, 大小={len(img_bytes)}B")
|
|
|
|
+
|
|
except Exception as e:
|
|
except Exception as e:
|
|
- LOGGER.error(f"{uid}处理图片时出错,已跳过: {repr(e)}")
|
|
|
|
|
|
+ LOGGER.error(f"{uid} 第{idx}张图处理失败,已跳过: {repr(e)} (长度={len(b64_image) if b64_image else 0})")
|
|
img_bytes_list.append(None) # 添加占位符以保持索引一致
|
|
img_bytes_list.append(None) # 添加占位符以保持索引一致
|
|
|
|
|
|
if not image_contents:
|
|
if not image_contents:
|