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.

videosample.py 2.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 json
  17. import os
  18. import cv2
  19. datasetIdKey = ""
  20. def execute(task):
  21. return sampleProcess(task)
  22. def sampleProcess(taskParameters):
  23. """Video sampling method.
  24. Args:
  25. taskParameters: taskParameters.
  26. """
  27. global datasetIdKey
  28. path = taskParameters['path']
  29. frameList = taskParameters['frames']
  30. datasetId = taskParameters['datasetId']
  31. task_id = taskParameters['id']
  32. datasetIdJson = {'datasetIdKey': datasetId}
  33. datasetIdKey = json.dumps(datasetIdJson, separators=(',', ':'))
  34. try:
  35. videoName = path.split('/')[-1]
  36. save_path = path.split(videoName)[0].replace("video", "origin")
  37. is_exists = os.path.exists(save_path)
  38. if not is_exists:
  39. os.makedirs(save_path)
  40. print('path of %s is build' % save_path)
  41. else:
  42. print('path of %s already exist and start' % save_path)
  43. cap = cv2.VideoCapture(path)
  44. pic_name_list = []
  45. finish_json = {}
  46. for i in frameList:
  47. cap.set(cv2.CAP_PROP_POS_FRAMES, i)
  48. success, video_capture = cap.read()
  49. # 保存图片
  50. if success is True and video_capture is not None:
  51. save_name = save_path + videoName.split('.')[0] + '_' + str(i) + '.jpg'
  52. cv2.imwrite(save_name, video_capture)
  53. pic_name_list.append(save_name)
  54. print('image of %s is saved' % save_name)
  55. pic_name_list.reverse()
  56. finish_json['pictureNames'] = pic_name_list
  57. finish_json['datasetIdAndSub'] = datasetId
  58. finish_json['id'] = task_id
  59. print('video is all read')
  60. return finish_json
  61. except Exception as e:
  62. failed_json = {'datasetIdAndSub': datasetId}
  63. return failed_json

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