task.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import os
  2. import time
  3. import json
  4. import base64
  5. import traceback
  6. from _log import logger, EventType
  7. import tools
  8. import shutil
  9. import cv2
  10. def is_image(path: str):
  11. img = cv2.imread(path)
  12. if img is None:
  13. return False
  14. return True
  15. class Interface:
  16. def __init__(self, interface=None):
  17. if interface is None:
  18. interface = os.getenv('PARAMS')
  19. if interface is None:
  20. interface = {
  21. }
  22. self.interface = self.decode_b64(interface)
  23. self.op_type = self.get_op_type()
  24. self.args = self.interface['args']
  25. self.input = self.interface.get('input', '/input')
  26. if not os.path.exists(self.input):
  27. raise FileNotFoundError('The input file does not exist.')
  28. self.result = self.interface.get('result', '/result')
  29. if self.result is None:
  30. raise FileNotFoundError('Need to give the result path.')
  31. self.input_dic = []
  32. self.IOU = self.args.get('IOU', 0.9)
  33. self.template = self.args['template']
  34. self.content = self.args.get('content')
  35. self.precision = self.args.get('precision', 0.9)
  36. self.time = self.args.get('time', 1)
  37. self.divisor = self.args.get('divisor', []) # {'coordinates': 0.4, 'properties': 0.3, 'imageResult': 0.3}
  38. self.distance = self.args.get('distance')
  39. @staticmethod
  40. def decode_b64(interface):
  41. interface = base64.b64decode(interface.encode('utf-8')).decode("utf-8")
  42. interface = json.loads(interface)
  43. assert isinstance(interface, dict), 'The interface must be a dictionary.'
  44. return interface
  45. @staticmethod
  46. def encode_b64(string):
  47. return base64.b64encode(string.encode('utf-8')).decode("utf-8")
  48. def get_op_type(self):
  49. op_type = self.interface.get('type', 'matting')
  50. op_type = op_type.upper()
  51. if op_type not in ['MATTING', 'RESIZE']:
  52. raise TypeError('The type must be matting or resize')
  53. return op_type
  54. def scan_files(self, path: str):
  55. if os.path.isdir(path):
  56. self.scan_files(path)
  57. else:
  58. self.input_dic.append(path)
  59. def run(self):
  60. logger.info("files scanning.", extra={'event_type': EventType.STEP_COMPLETE})
  61. self.scan_files(self.input)
  62. files_in_total = len(self.input_dic)
  63. logger.info("files-in-total",
  64. extra={'event_type': EventType.METRICS, 'desc': '处理文件总个数', 'value': files_in_total})
  65. count = 0
  66. for path in self.input_dic:
  67. count += 1
  68. logger.info("PROGRESS", extra={'event_type': EventType.PROGRESS, 'total': files_in_total, 'current': count})
  69. try:
  70. real_path = os.path.relpath(path, self.input)
  71. shutil.copy(path, os.path.join(self.result, real_path))
  72. if is_image(path):
  73. output = os.path.dirname(path)
  74. output = os.path.join(self.result, os.path.relpath(output, self.input))
  75. tools.seg(path, output)
  76. except Exception as e:
  77. logger.info(e, extra={'event_type': EventType.TASK_CRASHED, 'traceback': traceback.format_exc()})
  78. def start(interface=None):
  79. logger.info('TASK_STARTED', extra={'event_type': EventType.TASK_STARTED})
  80. t = time.time()
  81. try:
  82. Interface(interface=interface).run()
  83. except Exception as e:
  84. logger.info(e, extra={'event_type': EventType.TASK_CRASHED, 'traceback': traceback.format_exc()})
  85. logger.info("TASK_FINISHED", extra={'event_type': EventType.TASK_FINISHED})
  86. logger.info("TASK_FINISHED")
  87. elapsed = time.time() - t
  88. logger.info("time-elapsed", extra={'event_type': EventType.METRICS, 'desc': '总时长', 'value': elapsed})
  89. if __name__ == '__main__':
  90. start()