You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

imgprocess.py 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # !/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. """
  4. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================
  15. """
  16. import logging
  17. import time
  18. import cv2
  19. import numpy as np
  20. import shutil
  21. import os
  22. from ACE import ACE_color
  23. from dehaze import deHaze, addHaze
  24. from hist_equalize import adaptive_hist_equalize
  25. def execute(task):
  26. return start_enhance_task(task)
  27. def start_enhance_task(taskParameters):
  28. """
  29. Enhance task method.
  30. Args:
  31. enhanceTaskId: enhance task id.
  32. redisClient: redis client.
  33. """
  34. dataset_id = taskParameters['id']
  35. img_save_path = taskParameters['enhanceFilePath']
  36. ann_save_path = taskParameters["enhanceAnnotationPath"]
  37. file_list = taskParameters['fileDtos']
  38. nums_, img_path_list, ann_path_list = img_ann_list_gen(file_list)
  39. process_type = taskParameters['type']
  40. re_task_id = taskParameters['reTaskId']
  41. img_process_config = [dataset_id, img_save_path,
  42. ann_save_path, img_path_list,
  43. ann_path_list, process_type, re_task_id]
  44. return image_enhance_process(img_process_config)
  45. logging.info(str(nums_) + ' images for augment')
  46. def img_ann_list_gen(file_list):
  47. """Analyze the json request and convert to list"""
  48. nums_ = len(file_list)
  49. img_list = []
  50. ann_list = []
  51. for i in range(nums_):
  52. img_list.append(file_list[i]['filePath'])
  53. ann_list.append(file_list[i]['annotationPath'])
  54. return nums_, img_list, ann_list
  55. def image_enhance_process(img_task):
  56. """The implementation of image augmentation thread"""
  57. global finish_key
  58. global re_task_id
  59. logging.info('img_process server start'.center(66, '-'))
  60. result = True
  61. try:
  62. dataset_id = img_task[0]
  63. img_save_path = img_task[1]
  64. ann_save_path = img_task[2]
  65. img_list = img_task[3]
  66. ann_list = img_task[4]
  67. method = img_task[5]
  68. re_task_id = img_task[6]
  69. suffix = '_enchanced_' + re_task_id
  70. logging.info("dataset_id " + str(dataset_id))
  71. finish_key = {"processKey": re_task_id}
  72. finish_data = {"id": re_task_id,
  73. "suffix": suffix}
  74. for j in range(len(ann_list)):
  75. img_path = img_list[j]
  76. ann_path = ann_list[j]
  77. img_process(suffix, img_path, ann_path,
  78. img_save_path, ann_save_path, method)
  79. logging.info('suffix:' + suffix)
  80. logging.info("End img_process of dataset:" + str(dataset_id))
  81. return finish_data
  82. except Exception as e:
  83. result = False
  84. return finish_data
  85. logging.error("Error imgProcess")
  86. logging.error(e)
  87. time.sleep(0.01)
  88. def img_process(suffix, img_path, ann_path, img_save_path, ann_save_path, method_ind):
  89. """Process images and save in specified path"""
  90. inds2method = {1: deHaze, 2: addHaze, 3: ACE_color, 4: adaptive_hist_equalize}
  91. method = inds2method[method_ind]
  92. img_raw = cv2.imdecode(np.fromfile(img_path.encode('utf-8'), dtype=np.uint8), 1)
  93. img_suffix = os.path.splitext(img_path)[-1]
  94. ann_name = os.path.basename(ann_path)
  95. if method_ind <= 3:
  96. processed_img = method(img_raw / 255.0) * 255
  97. else:
  98. processed_img = method(img_raw)
  99. cv2.imwrite(img_save_path + "/" + ann_name + suffix + img_suffix,
  100. processed_img.astype(np.uint8))
  101. shutil.copyfile(ann_path.encode('utf-8'), (ann_save_path + "/" + ann_name + suffix).encode('utf-8'))

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能