deal_cutout.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import os.path
  2. import time
  3. from concurrent.futures import ThreadPoolExecutor, wait
  4. import threading
  5. from middleware import UnicornException
  6. from .deal_one_image import DealOneImage, DealOneImageBeforehand
  7. from logger import logger
  8. from .online_request.module_online_data import GetOnlineDataHLM
  9. class DealCutout():
  10. # signal_data = Signal(dict)
  11. def __init__(self, windows,token):
  12. super().__init__()
  13. self.windows = windows
  14. self.token = token
  15. self.lock = threading.Lock()
  16. self.need_cutout_images = {}
  17. self.state = 2 # 1进行中 2停止 状态 用于中途取消, 3已结束
  18. # 当前阿里预处理后的图片数量(未消费)
  19. self.is_upload_pic_num = 0
  20. self.is_deal_num = 0
  21. # 图片列表
  22. self.upload_pic_dict = {}
  23. self.logger = logger
  24. self.remaining_times = 0 # 剩余总次数
  25. self.get_online_data = GetOnlineDataHLM(token)
  26. self.resultData = [] # 最终的结果
  27. def refresh_times(self, is_online=True, remaining_times=None):
  28. # 刷新剩余次数
  29. if remaining_times is not None:
  30. self.remaining_times = remaining_times
  31. if is_online:
  32. _ = self.get_online_data.get_cutout_image_times()
  33. if _ is False:
  34. self.remaining_times = 0
  35. else:
  36. if "balance" in _:
  37. self.remaining_times = _["balance"]
  38. if self.remaining_times <= 0:
  39. return False
  40. return True
  41. def send_sign(self, data):
  42. # show_info complete
  43. self.signal_data.emit(data)
  44. def check_before(self):
  45. self.refresh_times()
  46. if self.remaining_times <= 0:
  47. # self.send_sign({"type": "show_info",
  48. # "message": "精细化抠图余量不足",
  49. # })
  50. raise UnicornException("精细化抠图余量不足")
  51. return False
  52. return True
  53. async def run(self):
  54. """
  55. need_cutout_images 结构:
  56. [
  57. "file_name": file_name, # 文件名
  58. "file_e": file_e, # 后缀,.jpg
  59. "file_path": image_path, # 完整路径
  60. "file": file, # 图片文件名,带后缀
  61. "need_cutout": True,# 必须,需要抠图
  62. "out_path":图片输出路径
  63. ]
  64. """
  65. if not self.check_before():
  66. # self.signal_data.emit({"_type": "complete",
  67. # "data": []})
  68. self.state = 3
  69. return
  70. # executor = ThreadPoolExecutor(max_workers=4)
  71. # executor_pic_upload = ThreadPoolExecutor(max_workers=2)
  72. # tasks_1 = []
  73. # tasks_2 = []
  74. self.state = 1
  75. self.resultData = []
  76. self.is_upload_pic_num = 0
  77. self.is_deal_num = 0
  78. num = 0
  79. success_image_path = []
  80. for image_data in self.need_cutout_images:
  81. if not image_data["need_cutout"]:
  82. continue
  83. num += 1
  84. deal2 = DealOneImageBeforehand(
  85. image_data=image_data,
  86. lock=self.lock,
  87. windows=self,
  88. num=num,
  89. token=self.token,
  90. )
  91. res2 = await deal2.run()
  92. try:
  93. image_path = res2
  94. if image_path:
  95. if isinstance(image_path, str):
  96. if os.path.exists(image_path):
  97. success_image_path.append(image_path)
  98. except BaseException as e:
  99. self.logger.info("有线程出错:{}".format(e))
  100. deal1 = DealOneImage(
  101. image_data=image_data,
  102. lock=self.lock,
  103. windows=self,
  104. num=num,
  105. token=self.token,
  106. )
  107. res1 = await deal1.run()
  108. # tasks_1.append(task_1)
  109. print("res1===>", res1)
  110. try:
  111. image_path = res1
  112. if image_path:
  113. if isinstance(image_path, str):
  114. if os.path.exists(image_path):
  115. success_image_path.append(image_path)
  116. except BaseException as e:
  117. self.logger.info("有线程出错:{}".format(e))
  118. # self.check_thread(tasks_1, tasks_2)
  119. self.resultData = success_image_path
  120. self.state = 3
  121. def __del__(self):
  122. self.state = 2
  123. def check_thread(self, *tasks_list):
  124. time.sleep(2)
  125. success_image_path = []
  126. while 1:
  127. f = True
  128. for tasks in tasks_list:
  129. done, not_done = wait(tasks)
  130. if not_done:
  131. time.sleep(2)
  132. f = False
  133. continue
  134. for task in done:
  135. try:
  136. image_path = task.result()
  137. if image_path:
  138. if isinstance(image_path, str):
  139. if os.path.exists(image_path):
  140. success_image_path.append(image_path)
  141. except BaseException as e:
  142. self.logger.info("有线程出错:{}".format(e))
  143. if f:
  144. break
  145. # self.signal_data.emit({"_type": "complete",
  146. # "data": success_image_path})
  147. self.resultData = success_image_path
  148. self.state = 3