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.

annotation.py 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 codecs
  17. import os
  18. import sched
  19. import logging
  20. import time
  21. import sys
  22. from program.exec.annotation import predict_with_print_box as yolo_demo
  23. from common.config.log_config import setup_log
  24. from abc import ABC
  25. from program.abstract.algorithm import Algorithm
  26. logging.basicConfig(format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s',
  27. level=logging.DEBUG)
  28. schedule = sched.scheduler(time.time, time.sleep)
  29. sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
  30. label_log = setup_log('dev', 'label.log')
  31. class Annotation(Algorithm, ABC):
  32. def __init__(self):
  33. pass
  34. def execute(task):
  35. return Annotation.annotationExecutor(task)
  36. def annotationExecutor(jsonObject):
  37. """Annotation task method.
  38. Args:
  39. redisClient: redis client.
  40. key: annotation task key.
  41. """
  42. print('-------------process one-----------------')
  43. try:
  44. image_path_list = []
  45. id_list = []
  46. annotation_url_list = []
  47. label_list = jsonObject['labels']
  48. for fileObject in jsonObject['files']:
  49. pic_url = '/nfs/' + fileObject['url']
  50. image_path_list.append(pic_url)
  51. annotation_url = pic_url.replace("origin/", "annotation/")
  52. annotation_url_list.append(os.path.splitext(annotation_url)[0])
  53. isExists = os.path.exists(os.path.dirname(annotation_url))
  54. if not isExists:
  55. try:
  56. os.makedirs(os.path.dirname(annotation_url))
  57. except Exception as exception:
  58. logging.error(exception)
  59. id_list.append(fileObject['id'])
  60. print(image_path_list)
  61. print(annotation_url_list)
  62. print(label_list)
  63. coco_flag = 0
  64. if "labelType" in jsonObject:
  65. label_type = jsonObject['labelType']
  66. if label_type == 3:
  67. coco_flag = 80
  68. annotations = Annotation._annotation(0, image_path_list, id_list, annotation_url_list, label_list,
  69. coco_flag);
  70. finish_data = {"reTaskId": jsonObject["reTaskId"], "annotations": annotations}
  71. return finish_data, True
  72. except Exception as e:
  73. print(e)
  74. finish_data = {"reTaskId": jsonObject["reTaskId"], "annotations": annotations}
  75. return finish_data, True
  76. @staticmethod
  77. def _init():
  78. print('init yolo_obj')
  79. global yolo_obj
  80. yolo_obj = yolo_demo.YoloInference(label_log)
  81. def _annotation(type_, image_path_list, id_list, annotation_url_list, label_list, coco_flag=0):
  82. """Perform automatic annotation task."""
  83. image_num = len(image_path_list)
  84. if image_num < 16:
  85. for i in range(16 - image_num):
  86. image_path_list.append(image_path_list[0])
  87. id_list.append(id_list[0])
  88. annotation_url_list.append(annotation_url_list[0])
  89. image_num = len(image_path_list)
  90. annotations = yolo_obj.yolo_inference(type_, id_list, annotation_url_list, image_path_list, label_list,
  91. coco_flag)
  92. return annotations[0:image_num]

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