Files
AiDA_Python/app/service/design/items/pipelines/segmentation.py

77 lines
3.1 KiB
Python
Raw Normal View History

2024-08-20 10:36:05 +08:00
import logging
2024-07-19 15:10:28 +08:00
import os
2024-08-28 11:45:33 +08:00
import cv2
2024-07-19 15:10:28 +08:00
import numpy as np
from app.core.config import SEG_CACHE_PATH
2024-08-28 11:45:33 +08:00
from app.service.utils.oss_client import oss_get_image
2024-05-28 15:22:11 +08:00
from ..builder import PIPELINES
from ...utils.design_ensemble import get_seg_result
2024-08-20 10:36:05 +08:00
logger = logging.getLogger()
2024-05-28 15:22:11 +08:00
@PIPELINES.register_module()
class Segmentation(object):
2024-07-19 15:17:54 +08:00
# @ClassCallRunTime
2024-05-28 15:22:11 +08:00
def __call__(self, result):
2024-08-28 11:45:33 +08:00
if "seg_mask_url" in result.keys() and result['seg_mask_url'] != "":
seg_mask = oss_get_image(bucket=result['seg_mask_url'].split('/')[0], object_name=result['seg_mask_url'][result['seg_mask_url'].find('/') + 1:], data_type="cv2")
seg_mask = cv2.resize(seg_mask, (result['img_shape'][1], result['img_shape'][0]))
# 转换颜色空间为 RGBOpenCV 默认是 BGR
image_rgb = cv2.cvtColor(seg_mask, cv2.COLOR_BGR2RGB)
# 定义红色和绿色的颜色范围
# 红色范围: 下界 [R-10, G-10, B-10], 上界 [R+10, G+10, B+10]
red_lower = np.array([50, 0, 0], dtype=np.uint8)
red_upper = np.array([255, 50, 50], dtype=np.uint8)
# 绿色范围: 下界 [R-10, G-10, B-10], 上界 [R+10, G+10, B+10]
green_lower = np.array([0, 50, 0], dtype=np.uint8)
green_upper = np.array([50, 255, 50], dtype=np.uint8)
2024-08-28 11:45:33 +08:00
# 创建红色和绿色掩码
result['front_mask'] = cv2.inRange(image_rgb, red_lower, red_upper)
result['back_mask'] = cv2.inRange(image_rgb, green_lower, green_upper)
2024-08-28 11:45:33 +08:00
result['mask'] = result['front_mask'] + result['back_mask']
else:
# 本地查询seg 缓存是否存在
_, seg_result = self.load_seg_result(result["image_id"])
result['seg_result'] = seg_result
if not _:
# 推理获得seg 结果
seg_result = get_seg_result(result["image_id"], result['image'])[0]
self.save_seg_result(seg_result, result['image_id'])
# 处理前片后片
temp_front = seg_result == 1.0
result['front_mask'] = (255 * (temp_front + 0).astype(np.uint8))
temp_back = seg_result == 2.0
result['back_mask'] = (255 * (temp_back + 0).astype(np.uint8))
result['mask'] = result['front_mask'] + result['back_mask']
2024-05-28 15:22:11 +08:00
return result
2024-07-19 15:10:28 +08:00
@staticmethod
def save_seg_result(seg_result, image_id):
file_path = f"{SEG_CACHE_PATH}{image_id}.npy"
try:
np.save(file_path, seg_result)
print("保存成功", os.path.abspath(file_path))
except Exception as e:
print(f"保存失败: {e}")
@staticmethod
def load_seg_result(image_id):
file_path = f"{SEG_CACHE_PATH}{image_id}.npy"
2024-08-20 10:36:05 +08:00
logger.info(f"load seg file name is :{SEG_CACHE_PATH}{image_id}.npy")
2024-07-19 15:10:28 +08:00
try:
seg_result = np.load(file_path)
return True, seg_result
except FileNotFoundError:
print("文件不存在")
return False, None
except Exception as e:
print(f"加载失败: {e}")
return False, None