capturecard_manager.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # -*- coding:utf-8 -*-
  2. import os
  3. import sys
  4. import time
  5. # from device_manager import *
  6. from ssat_sdk.utils import LoggingUtil
  7. from ssat_sdk import sat_environment
  8. from ssat_sdk.utils import string_util
  9. from ssat_sdk.service.service_manager import ServiceManager
  10. from ssat_sdk.device_manage.ub530_manager import UB530Manager
  11. import CD750SSDK
  12. import UB530SDK
  13. UB530 = 'TC-UB530'
  14. UB530EX = 'TC-UB530EX'
  15. CD750 = 'CD750'
  16. class CCardManager():
  17. status = False
  18. exe_path = sys.prefix + r"/Tools/CD750/AVerCapSDKDemo.exe"
  19. sManager = ServiceManager()
  20. DEVICE_NAME = sat_environment.getCCard_Selected()
  21. print u"视频采集卡名称:", DEVICE_NAME
  22. def __init__(self):
  23. if string_util.strcmp(UB530, self.DEVICE_NAME): # Python版本;
  24. self.ccard = UB530Manager()
  25. elif string_util.strcmp(UB530EX, self.DEVICE_NAME): # VC++版本;
  26. CCardManager.exe_path = sys.prefix + r"/Tools/SATHelper/SATHelper.exe"
  27. else:
  28. CCardManager.exe_path = sys.prefix + r"/Tools/CD750/AVerCapSDKDemo.exe"
  29. def start(self):
  30. print "CCardManager ssdk"
  31. if string_util.strcmp(self.DEVICE_NAME, CD750):
  32. self.status = CD750SSDK.StartApp(CCardManager.exe_path)
  33. time.sleep(3)
  34. if self.status == True:
  35. LoggingUtil.printLog("打开CD750 AVerCapSDKDemo成功")
  36. # 连接设备
  37. CD750SSDK.ConnectDevice(0)
  38. CCardManager.status = True
  39. # 开始流操作
  40. time.sleep(1)
  41. CD750SSDK.StreamOption(True)
  42. else:
  43. LoggingUtil.printLog("打开CD750 AVerCapSDKDemo失败")
  44. return self.status
  45. elif string_util.strcmp(self.DEVICE_NAME, UB530): # Python版本;
  46. self.sManager.startServiceThread("ub530")
  47. time.sleep(2)
  48. return self.sManager.getServiceStatus("ub530")
  49. elif string_util.strcmp(self.DEVICE_NAME, UB530EX): # VC++版本;
  50. self.status = UB530SDK.StartApp(CCardManager.exe_path)
  51. time.sleep(3)
  52. if self.status == True:
  53. LoggingUtil.printLog("打开UB530 VideoCapture成功")
  54. # 连接设备
  55. UB530SDK.ConnectDevice()
  56. CCardManager.status = True
  57. else:
  58. LoggingUtil.printLog("打开UB530 VideoCapture失败")
  59. return self.status
  60. def isExeRunning(self):
  61. if string_util.strcmp(self.DEVICE_NAME, CD750):
  62. return CD750SSDK.IsAppRunning(CCardManager.exe_path)
  63. elif string_util.strcmp(self.DEVICE_NAME, UB530EX): # VC++版本;
  64. return UB530SDK.IsAppRunning(CCardManager.exe_path)
  65. elif string_util.strcmp(self.DEVICE_NAME, UB530): # Python版本;
  66. return self.sManager.getServiceStatus("ub530")
  67. # start app
  68. def startApp(self):
  69. self.status = CD750SSDK.StartApp(CCardManager.exe_path)
  70. return self.status
  71. # close app;
  72. def stopApp(self):
  73. self.status = not CD750SSDK.StopApp(CCardManager.exe_path)
  74. return self.status
  75. # connect device
  76. def connect(self, index=0):
  77. return CD750SSDK.ConnectDevice(index)
  78. # disconnect device
  79. def disconnect(self, index=0):
  80. return CD750SSDK.DisconnectDevice(index)
  81. # show app
  82. def showApp(self):
  83. CD750SSDK.ShowApp()
  84. # hide app
  85. def hideApp(self):
  86. CD750SSDK.HideApp()
  87. # stream option,开始/停止流操作
  88. def streamOption(self, enable=True):
  89. return CD750SSDK.StreamOption(enable)
  90. # 截图单张
  91. def captureSingleImage(self, picpath):
  92. return CD750SSDK.CaptureSingleImage(picpath, 3, True, 0, 0, 0, 0)
  93. # 按张数截图:picpath图片路径,count截图张数
  94. def captureImageByCount(self, count, picpath, prefix="CD750"):
  95. return CD750SSDK.CaptureImageByCount(count, picpath, prefix, 3, True, 0, 0, 0, 0)
  96. # 按时间截图:times持续时间,单位毫秒; perCount每秒截图的张数; picpath图片保存路径;
  97. def captureImageByTime(self, times, perCount, picpath, prefix="CD750"):
  98. if string_util.strcmp(CD750, self.DEVICE_NAME):
  99. return CD750SSDK.CaptureImageByTime(times, perCount, picpath, prefix, 3, True, 0, 0, 0, 0)
  100. elif string_util.strcmp(UB530EX, self.DEVICE_NAME): # VC++版本;
  101. return UB530SDK.CaptureImageByTime(times, 0, picpath, prefix, 2)
  102. elif string_util.strcmp(UB530, self.DEVICE_NAME): # Python版本;
  103. count = 0
  104. runTime = time.time()
  105. endWork = False
  106. lastIndex = 1
  107. times = times/1000
  108. while True:
  109. lastCount = lastIndex + perCount
  110. for i in range(lastIndex, lastCount):
  111. path = "%s%s_%d.jpg" % (picpath, prefix, i)
  112. if self.ccard.takePicture(path) == 0:
  113. count += 1
  114. time.sleep(0.2) # 回调响应时间;
  115. if (time.time() - runTime >= times):
  116. endWork = True
  117. break
  118. lastIndex += perCount
  119. if endWork:
  120. break
  121. # print '实际生成数量%d,目标生成数量%d'%(count,perCount*times)
  122. return True if count == perCount*times else False
  123. # 抓单张图;
  124. def takePicture(self, picpath):
  125. status = -1
  126. if string_util.strcmp(CD750, self.DEVICE_NAME):
  127. status = CD750SSDK.CaptureSingleImage(picpath, 3, True, 0, 0, 0, 0)
  128. time.sleep(0.5)
  129. elif string_util.strcmp(UB530EX, self.DEVICE_NAME): # VC++版本;
  130. status = UB530SDK.CaptureSingleImage(picpath, 2)
  131. count = 0
  132. while os.path.exists(picpath) is False:
  133. time.sleep(0.1) # 等待磁盘完成响应;
  134. if count == 5:
  135. break
  136. count = count + 1
  137. # 超过5次认为截图失败;
  138. status = True if count < 5 else False
  139. elif string_util.strcmp(UB530, self.DEVICE_NAME): # Python版本;
  140. status = self.ccard.takePicture(picpath)
  141. if status == 0:
  142. status = 1
  143. else:
  144. status = 0
  145. time.sleep(1)
  146. return status
  147. # 返回保存的文件列表;
  148. def getCaptureImageList(self, picpath):
  149. list = []
  150. if string_util.strcmp(CD750, self.DEVICE_NAME):
  151. list = CD750SSDK.GetCaptureImageList(picpath)
  152. elif string_util.strcmp(UB530EX, self.DEVICE_NAME): # VC++版本;
  153. list = UB530SDK.GetCaptureImageList(picpath)
  154. elif string_util.strcmp(UB530, self.DEVICE_NAME): # Python版本;
  155. list = []
  156. # 过滤掉多余的'/'
  157. newpath = ''
  158. for i in range(0, picpath.__len__(), 2):
  159. char1 = picpath[i]
  160. if i < picpath.__len__()-1:
  161. char2 = picpath[i+1]
  162. newpath += char1
  163. if char1 == char2 and char1 == '/':
  164. continue
  165. newpath += char2
  166. else:
  167. newpath += char1
  168. picpath = newpath.replace('/', '\\')
  169. index = picpath.rfind('\\')
  170. # 路径;
  171. dir = picpath[:index]
  172. # 文件名;
  173. name = picpath[index+1:]
  174. # 前缀;
  175. preFix = name[:name.rfind("_")]
  176. for dirpath, dirname, filenames in os.walk(dir):
  177. for filepath in filenames:
  178. list.append(os.path.join(dirpath, filepath))
  179. # endif
  180. list.sort()
  181. return list
  182. # 停止抓图;
  183. def stopCaptureImage(self):
  184. CD750SSDK.StopCaptureImage()
  185. # 开始录制视频;
  186. def startCaptureAudio(self, audiopath):
  187. CD750SSDK.StartCaptureAudio(audiopath)
  188. # 停止录制视频;
  189. def stopCaptureAudio(self):
  190. CD750SSDK.StopCaptureAudio()
  191. # 异步录制视频;
  192. def asyStartCaptureAudio(self, keeptime, audiopath):
  193. CD750SSDK.AsyCaptureAudio(keeptime, audiopath)
  194. if __name__ == "__main__":
  195. cardManager = CCardManager()
  196. print cardManager.start()
  197. # time.sleep(1)
  198. # print u"开始截图"
  199. time_start = time.time()
  200. cardManager.takePicture("D:\\SAT\\test1.png")
  201. time_end = time.time()
  202. print('totally cost', time_end-time_start)
  203. # print u"按时间截图"
  204. # cardManager.captureImageByTime(5000, 5, "D:/SAT/aaa/")
  205. # time.sleep(5)
  206. # print u"遍历设备"
  207. # list = CD750SSDK.GetCaptureImageList("D:/SAT/aaa/")
  208. # if list != None:
  209. # for file in list:
  210. # print file
  211. # time.sleep(2)
  212. # # 断开连接
  213. # print u"断开设备"
  214. # cardManager.disconnect(0)
  215. # time.sleep(3)
  216. # # 关闭app
  217. # print u"关闭程序(宿主程序需要管理员权限)"
  218. # cardManager.stopApp()