rambo 1 месяц назад
Родитель
Сommit
35939221e1
4 измененных файлов с 161 добавлено и 49 удалено
  1. 68 18
      python/api.py
  2. 7 0
      python/models.py
  3. 39 0
      python/service/remove_bg_ali.py
  4. 47 31
      python/service/run_main.py

+ 68 - 18
python/api.py

@@ -35,6 +35,7 @@ import functools
 import traceback,stat
 import concurrent.futures
 from sockets.message_handler import handlerFolderDelete
+from service.remove_bg_ali import RemoveBgALi
 def log_exception_with_context(context_message=""):
     """装饰器:为函数添加异常日志上下文"""
 
@@ -186,6 +187,10 @@ async def handle_detail_background(
     request: Request, params: HandlerDetail, background_tasks: BackgroundTasks
 ):
     goods_art_no_arrays = params.goods_art_no
+    # 模板类型,0系统模板,1自定义模板
+    template_type = params.template_type
+    # 自定义模板数据
+    customer_template_json = params.customer_template_json
     is_check = params.is_check
     obj = None
     token = "Bearer " + params.token
@@ -250,7 +255,10 @@ async def process_handle_detail(request: Request, params: HandlerDetail):
         # 初始化基础变量
         handler_result = []
         handler_result_folder = ""
-        
+        # 模板类型,0系统模板,1自定义模板
+        template_type = params.template_type
+        # 自定义模板数据
+        customer_template_json = params.customer_template_json
         # 处理参数
         obj, token, uuid = None, "Bearer " + params.token, params.uuid
         aigc_clazz = AIGCDataRequest(token)
@@ -288,7 +296,7 @@ async def process_handle_detail(request: Request, params: HandlerDetail):
             run_main, config_data, goods_art_no_arrays, move_folder_array
         )
         # 处理场景图和模特图
-        return_data_check_before_detail = run_main.check_before_detail(config_data,is_detail)
+        return_data_check_before_detail = run_main.check_before_detail(config_data,is_detail,template_type)
         
         # 检查处理结果
         success_handler = return_data_check_before_detail.get("data", {}).get("config_data", {}).get("success_handler", [])
@@ -336,7 +344,8 @@ async def process_handle_detail(request: Request, params: HandlerDetail):
         if is_detail == 1:
             handler_result_folder, handler_result= await _process_detail_pages(
                 run_main, return_data_check_before_detail, onlineData, 
-                online_stores, goods_art_no_arrays, handler_result_folder
+                online_stores, goods_art_no_arrays, handler_result_folder,
+                params
             )
             # 如果需要上传到第三方平台
             if online_stores:
@@ -459,7 +468,8 @@ async def _build_config_data(params, goods_art_no_arrays):
     """构建配置数据"""
     temp_class = {}
     temp_name_list = []
-    
+    # 模板类型;0系统模板;1自定义模板
+    template_type = params.template_type
     for tempItem in params.temp_list:
         temp_class[tempItem.template_id] = tempItem.template_local_classes
         temp_name_list.append(tempItem.template_id)
@@ -500,20 +510,22 @@ async def _build_config_data(params, goods_art_no_arrays):
         "temp_name_list": temp_name_list,
         "target_error_folder": f"{limit_path}/软件-生成详情错误",
         "success_handler": [],
+        "template_type":template_type,
     }
-    
+    if template_type == 0:
+        # 如果是系统模板,才进行动态类导入操作
     # 动态导入类
-    try:
-        temp_class_dict = {}
-        for key, class_path in config_data["temp_class"].items():
-            module_path, class_name = class_path.rsplit(".", 1)
-            module = importlib.import_module(module_path)
-            cls = getattr(module, class_name)
-            temp_class_dict[key] = cls
-    except:
-      raise UnicornException("详情页模板不存在或未下载完成,请重启软件后重试")
-        
-    config_data["temp_class"] = temp_class_dict
+        try:
+            temp_class_dict = {}
+            for key, class_path in config_data["temp_class"].items():
+                module_path, class_name = class_path.rsplit(".", 1)
+                module = importlib.import_module(module_path)
+                cls = getattr(module, class_name)
+                temp_class_dict[key] = cls
+        except:
+            raise UnicornException("详情页模板不存在或未下载完成,请重启软件后重试")
+            
+        config_data["temp_class"] = temp_class_dict
     return config_data
 
 async def _process_cutout(run_main, config_data, goods_art_no_arrays, move_folder_array):
@@ -842,10 +854,10 @@ async def _process_model_images(aigc_clazz, run_main, return_data_check_before_d
     return return_data_check_before_detail
 
 async def _process_detail_pages(run_main, return_data_check_before_detail, onlineData, 
-                               online_stores, goods_art_no_arrays, handler_result_folder):
+                               online_stores, goods_art_no_arrays, handler_result_folder,request_params):
     """处理详情页生成和上传"""
     check_for_detail_first_res = run_main.check_for_detail_first_call_back(
-        return_data_check_before_detail
+        return_data_check_before_detail,request_params
     )
     print("<======>check_for_detail_first_res<======>",check_for_detail_first_res)
     if isinstance(check_for_detail_first_res, partial):
@@ -1477,3 +1489,41 @@ def sync_action_configs(params: SyncLocalConfigs):
     # syncUserJsonConfigs(hlm_token)
     session.close()
     return {"code": 0, "msg": "操作成功", "data": None}
+
+@app.get("/get_goods_image_json", description="关闭窗口")
+def get_goods_image_json(goods_art_no: str,token:str):
+    remove_pic_ins = RemoveBgALi()
+    if goods_art_no == None or goods_art_no == "":
+                # 判断货号是否存在
+        raise UnicornException("货号不能为空")
+    session = SqlQuery()
+    photoRecord = CRUD(PhotoRecord)
+    goods_art_record = photoRecord.read_all(
+        session, conditions={"goods_art_no": goods_art_no}
+    )
+    if not goods_art_record:
+        raise UnicornException("该货号拍摄记录不存在")
+    action_id_array = [record.action_id for record in goods_art_record]
+    devices = CRUD(DeviceConfig)
+    devices_record = devices.read_all(session,conditions={"id": action_id_array})
+    # 提取 action_name 字段并拼接
+    action_names = [str(record.action_name) for record in devices_record]
+    action_names_str = ",".join(action_names)
+    image_arrays = []
+    for goods_art_record_item in goods_art_record:
+        image_path = goods_art_record_item.image_path
+        try:
+            image_url = uploadImage(remove_pic_ins=remove_pic_ins,token=token, local_path=image_path)
+        except Exception as e:
+            raise UnicornException("网络异常,请重试")
+        image_arrays.append(image_url)
+    session.close()
+    return {"code": 0, "msg": "关闭失败", "data": {"customer_template_images": image_arrays,"template_image_order":action_names_str}}
+def uploadImage(remove_pic_ins,token:str, local_path: str) -> str:
+        im = remove_pic_ins.get_image_cut_new(file_path=local_path)
+        post_headers = {"Authorization": "Bearer " +token}
+        url = settings.DOMAIN + "/api/upload"
+        resultData = requests.post(
+            url, files={"file":im}, headers=post_headers
+        ).json()
+        return resultData["data"]["url"]

+ 7 - 0
python/models.py

@@ -115,6 +115,8 @@ class HandlerDetail(BaseModel):
     upper_footer_params: Optional[dict] = Field(
         default=None, description="上脚图参数配置"
     )
+    template_type : Optional[int] = Field(default=0, description="模板类型;0系统模板;1自定义模板")
+    customer_template_json : Optional[list] = Field(default=None, description="自定义模板数据")
 
 
 class LogoParams(BaseModel):
@@ -146,3 +148,8 @@ class SyncLocalConfigs(BaseModel):
 
     token: str = Field(default=None, description="用户token")
     
+
+class GenerateImageJson(BaseModel):
+    """货号json数据生成"""
+
+    goods_art_no: str = Field(default=None, description="货号")

+ 39 - 0
python/service/remove_bg_ali.py

@@ -145,7 +145,46 @@ class RemoveBgALi(object):
     def __init__(self):
         self.saver = ImageSaver()
         self.segment = Segment()
+    @func_set_timeout(40)
+    def get_image_cut_new(self, file_path, out_file_path=None, original_im=None):
+        if original_im:
+            original_pic = Picture(in_path=None, im=original_im)
+        else:
+            original_pic = Picture(file_path)
+
+        if original_pic.im.mode != "RGB":
+            original_pic.im = original_pic.im.convert("RGB")
 
+        new_pic = copy.copy(original_pic)
+        after_need_resize = False
+        if new_pic.x > new_pic.y:
+            if new_pic.x > 2000:
+                after_need_resize = True
+                new_pic.resize(2000)
+        else:
+            if new_pic.y > 2000:
+                after_need_resize = True
+                new_pic.resize_by_heigh(heigh=2000)
+
+        # new_pic.im.show()
+        body = self.segment.get_no_bg_goods(file_path=None, _im=new_pic.im)
+        body = eval(str(body))
+        try:
+            image_url = body["Data"]["ImageURL"]
+        except BaseException as e:
+            print("阿里抠图错误:", e)
+            # 处理失败,需要删除过程图片
+            return None
+        # 字节流转PIL对象
+        response = requests.get(image_url)
+        pic = response.content
+        _img_im = Image.open(BytesIO(pic))  # 阿里返回的抠图结果 已转PIL对象
+        box_size = _img_im.getbbox()
+        new_pp4_im = _img_im.crop(box_size)
+        byte_io = BytesIO()
+        new_pp4_im.save(byte_io, format='PNG')  # 将图像保存为 PNG 格式到 BytesIO 对象
+        byte_io.seek(0)  # 将指针重置到流的开头,以便后续读取
+        return byte_io
     @func_set_timeout(40)
     def get_image_cut(self, file_path, out_file_path=None, original_im=None):
         if original_im:

+ 47 - 31
python/service/run_main.py

@@ -15,7 +15,7 @@ from PIL import Image
 from io import BytesIO
 import os, re
 from functools import partial
-
+from service.customer_template_service import CustomerTemplateService
 # from multiprocessing import Process, Queue
 import pickle
 from .base_deal import BaseDealImage
@@ -390,7 +390,7 @@ class RunMain:
         config_data["sign_text"] = "已结束抠图处理"
         self.run_end_sign.emit(config_data)
 
-    def check_before_detail(self, config_data,is_detail):
+    def check_before_detail(self, config_data,is_detail,template_type):
 
         # =============
         # 整体数据校验,返回错误内容,以及
@@ -474,16 +474,17 @@ class RunMain:
         # 在访问 temp_class[temp_name].need_view 前增加检查
         need_view_list = []
         if is_detail == 1:
-            if temp_name not in temp_class or temp_class[temp_name] is None:
-                raise UnicornException(f"模板 {temp_name} 未正确初始化或不存在")
+            if template_type == 0:
+                if temp_name not in temp_class or temp_class[temp_name] is None:
+                    raise UnicornException(f"模板 {temp_name} 未正确初始化或不存在")
 
-            # 确保 temp_class[temp_name] 是可调用的
-            if not callable(temp_class[temp_name]):
-                raise UnicornException(f"模板 {temp_name} 不是有效的可调用对象")
-            try:
-                need_view_list = temp_class[temp_name].need_view
-            except KeyError as ke:
-                raise UnicornException("未选择详情页模板,请检查")
+                # 确保 temp_class[temp_name] 是可调用的
+                if not callable(temp_class[temp_name]):
+                    raise UnicornException(f"模板 {temp_name} 不是有效的可调用对象")
+                try:
+                    need_view_list = temp_class[temp_name].need_view
+                except KeyError as ke:
+                    raise UnicornException("未选择详情页模板,请检查")
         _all_dir_info_data = get_all_dir_info_and_pic_info(
             image_dir, folder_name_list, need_view_list
         )
@@ -753,13 +754,17 @@ class RunMain:
             msg="开始处理抠图", goods_arts=goods_art_nos, status="开始处理",progress=progress
         )
     '''
-    def check_for_detail_first_call_back(self, data):
+    def check_for_detail_first_call_back(self, data,request_parmas):
         # 首次数据校验的信息返回
         # self.show_message(text="22222222222222222222222")
         # QMessageBox.critical(self, "警告", "1111111", QMessageBox.Ok)
         code = data["code"]
         config_data = data["data"]["config_data"]
         target_error_folder = config_data["target_error_folder"]
+        # 模板类型,0系统模板,1自定义模板
+        template_type = request_parmas.template_type
+        # 自定义模板数据
+        customer_template_json = request_parmas.customer_template_json
         print("635  check_for_detail_first_call_back")
         print(data)
         if code != 0:
@@ -824,7 +829,7 @@ class RunMain:
             # self.set_state(state_value=1)
             getAllData = data["data"]
             base_temp_name = getAllData["temp_name"]
-            set_temp_name = getAllData.get("template_name", "")
+            # set_temp_name = getAllData.get("template_name", "")
             kwargs = {
                 "config_data": config_data,
                 "_goods_no_dict": data["data"]["goods_no_dict"],
@@ -846,6 +851,7 @@ class RunMain:
                 assigned_page_dict=kwargs["assigned_page_dict"],
                 excel_temp_goods_no_data=kwargs["excel_temp_goods_no_data"],
                 finally_goods_no_need_temps=kwargs["finally_goods_no_need_temps"],
+                request_parmas=request_parmas,
             )
             # self._w_3 = WorkerOneThread(func=new_func, name="_w_3")
             # self._w_3.start()
@@ -865,6 +871,7 @@ class RunMain:
         assigned_page_dict,
         excel_temp_goods_no_data,
         finally_goods_no_need_temps,
+        request_parmas
     ):
         """
         excel_temp_goods_no_data: {},  # 表格数据可能存在多模板,数据结构为一个款号下的多个模板的数据列表
@@ -887,7 +894,6 @@ class RunMain:
             print(json.dumps(_goods_no_dict))
 
             print("==============_goods_no_dict  打印-end=================")
-
         all_detail_path_list = []
         out_put_dir_resp = ""
         detail_total_progress = len(finally_goods_no_need_temps.items())
@@ -956,6 +962,7 @@ class RunMain:
                         temp_class=config_data["temp_class"],
                         target_error_folder=config_data["target_error_folder"],
                         image_dir=config_data["image_dir"],
+                        request_parmas=request_parmas,
                     )
                     config_data["success_handler"].append(
                         {"goods_art_no": goods_no, "success": True, "info": "处理成功"}
@@ -974,6 +981,7 @@ class RunMain:
                         data={"goods_art_no": goods_no, "temp_name": _temp_name},
                     )
                 except BaseException as e:
+                    print("详情页打印输出错误信息===>",e)
                     detail_error_progress+=1
                     detail_progress = {"status":"处理失败","goods_art_no":goods_no, "current":detail_finish_progress, "total":detail_total_progress, "error":detail_error_progress,"folder":""}
                     self.sendAsyncMessage(
@@ -989,7 +997,6 @@ class RunMain:
                             "info": "款:{}生成详情异常:{}".format(goods_no, e),
                         }
                     )
-                    print(e)
                     # raise UnicornException("款:{}生成详情异常:{}".format(goods_no, e))
                     config_data["success_handler"].append(
                         {
@@ -1060,7 +1067,12 @@ class RunMain:
         temp_class,
         target_error_folder,
         image_dir,
+        request_parmas,
     ):
+        # 模板类型,0系统模板,1自定义模板
+        template_type = request_parmas.template_type
+        # 自定义模板数据
+        customer_template_json = request_parmas.customer_template_json
         # if self.windows.state == 99:
         #     self.show_progress_detail("用户主动取消:{}".format(goods_no))
         #     return
@@ -1071,21 +1083,12 @@ class RunMain:
         print("模板:", temp_name)
         print("value:", value)
         print("temp_class:", temp_class)
-        if temp_name not in temp_class or temp_class[temp_name] is None:
-            raise UnicornException(f"详情页模板 {temp_name} 未正确加载")
-            
-        if not callable(temp_class[temp_name]):
-            raise UnicornException(f"详情页模板 {temp_name} 不是有效的可调用对象")
-        if settings.IS_TEST:
-            temp_class[temp_name](
-                goods_no,
-                value,
-                out_put_dir=out_put_dir,
-                windows=self.windows,
-                assigned_page_list=assigned_page_list,
-            )
-            is_deal_success = True
-        else:
+        if template_type == 0:
+            if temp_name not in temp_class or temp_class[temp_name] is None:
+                raise UnicornException(f"详情页模板 {temp_name} 未正确加载")
+                
+            if not callable(temp_class[temp_name]):
+                raise UnicornException(f"详情页模板 {temp_name} 不是有效的可调用对象")
             try:
                 # # 处理图片详情图生成
                 temp_class[temp_name](
@@ -1110,7 +1113,20 @@ class RunMain:
                 raise UnicornException(
                     "{}处理失败,失败原因:{}".format(goods_no, error_text)
                 )
-
+        else:
+            try:
+                is_deal_success = True
+                service = CustomerTemplateService()
+                goods_no = value["款号"]
+                save_path = f"{out_put_dir}/详情图-{goods_no}"
+                service.generateTemplate({goods_no:value},customer_template_json,temp_name,save_path)
+            except BaseException as e:
+                self.show_progress_detail("{}处理失败".format(goods_no))
+                error_text = "{}".format(e)
+                print("error_text",error_text)
+                print(
+                    f"发生错误的文件: {e.__traceback__.tb_frame.f_globals['__file__']}"
+                )
         self.n += 1
 
         if not is_deal_success: