deal_cutout.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 True
  52. def run(self):
  53. """
  54. need_cutout_images 结构:
  55. [
  56. "file_name": file_name, # 文件名
  57. "file_e": file_e, # 后缀,.jpg
  58. "file_path": image_path, # 完整路径
  59. "file": file, # 图片文件名,带后缀
  60. "need_cutout": True,# 必须,需要抠图
  61. "out_path":图片输出路径
  62. ]
  63. """
  64. if not self.check_before():
  65. # self.signal_data.emit({"_type": "complete",
  66. # "data": []})
  67. self.state = 3
  68. return
  69. # executor = ThreadPoolExecutor(max_workers=4)
  70. # executor_pic_upload = ThreadPoolExecutor(max_workers=2)
  71. # tasks_1 = []
  72. # tasks_2 = []
  73. self.state = 1
  74. self.resultData = []
  75. self.is_upload_pic_num = 0
  76. self.is_deal_num = 0
  77. num = 0
  78. success_image_path = []
  79. for image_data in self.need_cutout_images:
  80. if not image_data["need_cutout"]:
  81. continue
  82. num += 1
  83. deal2 = DealOneImageBeforehand(
  84. image_data=image_data,
  85. lock=self.lock,
  86. windows=self,
  87. num=num,
  88. token=self.token,
  89. )
  90. res2 = deal2.run()
  91. try:
  92. image_path = res2
  93. if image_path:
  94. if isinstance(image_path, str):
  95. if os.path.exists(image_path):
  96. success_image_path.append(image_path)
  97. except BaseException as e:
  98. self.logger.info("有线程出错:{}".format(e))
  99. deal1 = DealOneImage(
  100. image_data=image_data,
  101. lock=self.lock,
  102. windows=self,
  103. num=num,
  104. token=self.token,
  105. )
  106. res1 = deal1.run()
  107. # tasks_1.append(task_1)
  108. print("res1===>", res1)
  109. try:
  110. image_path = res1
  111. if image_path:
  112. if isinstance(image_path, str):
  113. if os.path.exists(image_path):
  114. success_image_path.append(image_path)
  115. except BaseException as e:
  116. self.logger.info("有线程出错:{}".format(e))
  117. # self.check_thread(tasks_1, tasks_2)
  118. self.resultData = success_image_path
  119. self.state = 3
  120. def __del__(self):
  121. self.state = 2
  122. def check_thread(self, *tasks_list):
  123. time.sleep(2)
  124. success_image_path = []
  125. while 1:
  126. f = True
  127. for tasks in tasks_list:
  128. done, not_done = wait(tasks)
  129. if not_done:
  130. time.sleep(2)
  131. f = False
  132. continue
  133. for task in done:
  134. try:
  135. image_path = task.result()
  136. if image_path:
  137. if isinstance(image_path, str):
  138. if os.path.exists(image_path):
  139. success_image_path.append(image_path)
  140. except BaseException as e:
  141. self.logger.info("有线程出错:{}".format(e))
  142. if f:
  143. break
  144. # self.signal_data.emit({"_type": "complete",
  145. # "data": success_image_path})
  146. self.resultData = success_image_path
  147. self.state = 3