fix(rmbg): 修复队列模式下process_image返回类型不一致问题

- 修复remove_background和remove_background_from_file方法中,队列模式返回dict但代码尝试调用save方法的问题
- 在调用处添加类型检查,兼容队列模式(返回dict)和非队列模式(返回Image)
- 修复process_batch降级处理中的相同问题

修复前错误: 'dict' object has no attribute 'save'
This commit is contained in:
jingrow 2025-12-07 05:34:33 +08:00
parent 409802d5d7
commit 46845e5f57

View File

@ -391,15 +391,13 @@ class RmbgService:
self.executor, lambda: Image.open(image_path).convert("RGB") self.executor, lambda: Image.open(image_path).convert("RGB")
) )
image_no_bg = await self.process_image(image) result = await self.process_image(image)
if isinstance(result, dict):
return result
image_url = await loop.run_in_executor( image_url = await loop.run_in_executor(
self.executor, self.save_image_to_file, image_no_bg self.executor, self.save_image_to_file, result
) )
return {"status": "success", "image_url": image_url}
return {
"status": "success",
"image_url": image_url
}
finally: finally:
if temp_file and os.path.exists(temp_file): if temp_file and os.path.exists(temp_file):
@ -424,15 +422,13 @@ class RmbgService:
self.executor, lambda: Image.open(io.BytesIO(file_content)).convert("RGB") self.executor, lambda: Image.open(io.BytesIO(file_content)).convert("RGB")
) )
image_no_bg = await self.process_image(image) result = await self.process_image(image)
if isinstance(result, dict):
return result
image_url = await loop.run_in_executor( image_url = await loop.run_in_executor(
self.executor, self.save_image_to_file, image_no_bg self.executor, self.save_image_to_file, result
) )
return {"status": "success", "image_url": image_url}
return {
"status": "success",
"image_url": image_url
}
except Exception as e: except Exception as e:
raise Exception(f"处理图片失败: {e}") raise Exception(f"处理图片失败: {e}")
@ -665,10 +661,13 @@ class RmbgService:
# 降级到单张处理 # 降级到单张处理
for image, image_size, index, url_str, _ in valid_items: for image, image_size, index, url_str, _ in valid_items:
try: try:
processed_image = await self.process_image(image) result_data = await self.process_image(image)
image_url = await loop.run_in_executor( if isinstance(result_data, dict):
self.executor, self.save_image_to_file, processed_image image_url = result_data["image_url"]
) else:
image_url = await loop.run_in_executor(
self.executor, self.save_image_to_file, result_data
)
completed_order += 1 completed_order += 1
success_count += 1 success_count += 1
result = { result = {