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 2.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 sys
  17. import os
  18. import codecs
  19. import logging
  20. import predict_with_print_box as yolo_demo
  21. logging.basicConfig(format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s',
  22. level=logging.DEBUG)
  23. sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
  24. current_dir = os.path.dirname(os.path.abspath(__file__))
  25. label_to_name_file = current_dir + os.sep + "coco.names"
  26. label_2_name = []
  27. with open(label_to_name_file, 'r') as f:
  28. label_2_name = f.readlines()
  29. def execute(task):
  30. return annotationExecutor(task)
  31. def annotationExecutor(jsonObject):
  32. """Annotation task method.
  33. Args:
  34. redisClient: redis client.
  35. key: annotation task key.
  36. """
  37. try:
  38. image_path_list = []
  39. id_list = []
  40. label_list = jsonObject['labels']
  41. labels = []
  42. for label in label_list:
  43. for i in range(0, len(label_2_name)):
  44. if (label == label_2_name[i].rstrip('\n')):
  45. labels.append(i)
  46. for fileObject in jsonObject['files']:
  47. pic_url = fileObject['url']
  48. image_path_list.append(pic_url)
  49. id_list.append(fileObject['id'])
  50. annotations = _annotation(0, image_path_list, id_list, labels);
  51. finish_data = {"annotations": annotations}
  52. return finish_data
  53. except Exception as e:
  54. finish_data = {"annotations": annotations}
  55. return finish_data
  56. def _init():
  57. print('init yolo_obj')
  58. global yolo_obj
  59. yolo_obj = yolo_demo.YoloInference()
  60. def _annotation(type_, image_path_list, id_list, label_list):
  61. """Perform automatic annotation task."""
  62. image_num = len(image_path_list)
  63. if image_num < 16:
  64. for i in range(16 - image_num):
  65. image_path_list.append(image_path_list[0])
  66. id_list.append(id_list[0])
  67. image_num = len(image_path_list)
  68. annotations = yolo_obj.yolo_inference(type_, id_list, image_path_list, label_list)
  69. return annotations[0:image_num]

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