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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """
  2. /**
  3. * Copyright 2020 Zhejiang Lab. All Rights Reserved.
  4. *
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. * =============================================================
  17. */
  18. """
  19. import json
  20. import os
  21. import sched
  22. import time
  23. from datetime import datetime
  24. import luascript.finishtaskscript as finish_script
  25. import luascript.failedtaskscript as failed_script
  26. import luascript.delaytaskscript as delay_script
  27. import common.config as config
  28. import cv2
  29. schedule = sched.scheduler(time.time, time.sleep)
  30. datasetIdKey = ""
  31. def sampleProcess(datasetId, path, frameList, redisClient):
  32. """Video sampling method.
  33. Args:
  34. datasetId: dataset id.
  35. path: video file path.
  36. frameList: picture frame number list.
  37. redisClient: redis client.
  38. """
  39. global datasetIdKey
  40. datasetIdJson = {'datasetIdKey': datasetId}
  41. datasetIdKey = json.dumps(datasetIdJson, separators=(',', ':'))
  42. try:
  43. videoName = path.split('/')[-1]
  44. save_path = path.split(videoName)[0].replace("video", "origin")
  45. is_exists = os.path.exists(save_path)
  46. if not is_exists:
  47. os.makedirs(save_path)
  48. print('path of %s is build' % save_path)
  49. else:
  50. print('path of %s already exist and start' % save_path)
  51. cap = cv2.VideoCapture(path)
  52. for i in frameList:
  53. cap.set(cv2.CAP_PROP_POS_FRAMES, i)
  54. success, video_capture = cap.read()
  55. # 保存图片
  56. if success is True and video_capture is not None:
  57. save_name = save_path + videoName.split('.')[0] + '_' + str(i) + '.jpg'
  58. cv2.imwrite(save_name, video_capture)
  59. redisClient.lpush("videoSample_pictures:" + datasetId,
  60. '{' + '\"pictureName\":' + "\"" + save_name + "\"" + '}')
  61. print('image of %s is saved' % save_name)
  62. print('video is all read')
  63. redisClient.eval(finish_script.finishTaskLua, 3, config.videoStartQueue, config.videoFinishQueue,
  64. "videoSample:" + str(datasetId),
  65. datasetIdKey, str(datasetIdKey))
  66. except Exception as e:
  67. print(e)
  68. redisClient.eval(failed_script.failedTaskLua, 4, config.videoStartQueue, config.videoFailedQueue,
  69. "videoSample_pictures:" + datasetId,
  70. "videoSample:" + str(datasetId),
  71. datasetIdKey, str(datasetIdKey))
  72. def delaySchduled(inc, redisClient):
  73. """Delay task method.
  74. Args:
  75. inc: scheduled task time.
  76. redisClient: redis client.
  77. """
  78. try:
  79. print("delay:" + datetime.now().strftime("B%Y-%m-%d %H:%M:%S"))
  80. redisClient.eval(delay_script.delayTaskLua, 1, config.videoStartQueue, datasetIdKey, int(time.time()))
  81. schedule.enter(inc, 0, delaySchduled, (inc, redisClient))
  82. except Exception as e:
  83. print("delay error" + e)
  84. def delayKeyThread(redisClient):
  85. """Delay task thread.
  86. Args:
  87. redisClient: redis client.
  88. """
  89. schedule.enter(0, 0, delaySchduled, (5, redisClient))
  90. schedule.run()

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

Contributors (1)