mms_client.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. # Copyright 2020 Baidu, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  4. # except in compliance with the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the
  9. # License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  10. # either express or implied. See the License for the specific language governing permissions
  11. # and limitations under the License.
  12. """
  13. This module provides a client class for MMS.
  14. """
  15. import copy
  16. import json
  17. import logging
  18. from builtins import str
  19. from builtins import bytes
  20. from baidubce import compat
  21. from baidubce.auth import bce_v1_signer
  22. from baidubce.bce_base_client import BceBaseClient
  23. from baidubce.http import bce_http_client
  24. from baidubce.http import handler
  25. from baidubce.http import http_methods
  26. from baidubce.utils import required
  27. _logger = logging.getLogger(__name__)
  28. class MmsClient(BceBaseClient):
  29. """
  30. mms client
  31. """
  32. def __init__(self, config=None):
  33. """init"""
  34. BceBaseClient.__init__(self, config)
  35. @required(lib_name=(bytes, str), params=dict)
  36. def create_video_lib(self, lib_name, params=None, config=None):
  37. """
  38. create video lib.
  39. :param lib_name: video lib name
  40. :param params: description for this request
  41. :type params: dict
  42. :return: **dict**
  43. """
  44. body = {
  45. 'name': lib_name
  46. }
  47. if params is not None:
  48. if 'description' in params:
  49. body['description'] = params['description']
  50. if 'scoreThreshold' in params:
  51. body['scoreThreshold'] = params['scoreThreshold']
  52. if 'videoScoreThreshold' in params:
  53. body['videoScoreThreshold'] = params['videoScoreThreshold']
  54. if 'frameType' in params:
  55. body['frameType'] = params['frameType']
  56. if 'interval' in params:
  57. body['interval'] = params['interval']
  58. return self._send_request(http_methods.POST,
  59. b'/v2/videolib',
  60. headers={
  61. b'Content-Type': b'application/json'},
  62. body=json.dumps(body),
  63. config=config)
  64. @required(lib_id=(bytes, str))
  65. def delete_video_lib(self, lib_id, config=None):
  66. """
  67. delete video lib by lib_id.
  68. :param lib_id: video lib id
  69. :return: **BceResponse**
  70. """
  71. return self._send_request(http_methods.POST,
  72. b'/v2/videolib/%s' % compat.convert_to_bytes(
  73. lib_id),
  74. headers={b'Content-Type': b'application/json'},
  75. params={b'deleteLibById': b''},
  76. config=config)
  77. @required(lib_name=(bytes, str), params=dict)
  78. def create_image_lib(self, lib_name, params=None, config=None):
  79. """
  80. create image lib.
  81. :param lib_name: image lib name
  82. :param params: params for this request
  83. :type params: dict
  84. :return: **BceResponse**
  85. """
  86. body = {
  87. 'name': lib_name
  88. }
  89. if params is not None:
  90. if 'description' in params:
  91. body['description'] = params['description']
  92. if 'scoreThreshold' in params:
  93. body['scoreThreshold'] = params['scoreThreshold']
  94. if 'frameType' in params:
  95. body['frameType'] = params['frameType']
  96. if 'interval' in params:
  97. body['interval'] = params['interval']
  98. return self._send_request(http_methods.POST,
  99. b'/v2/imagelib',
  100. headers={
  101. b'Content-Type': b'application/json'},
  102. body=json.dumps(body),
  103. config=config)
  104. @required(lib_id=(bytes, str))
  105. def delete_image_lib(self, lib_id, config=None):
  106. """
  107. delete image lib by lib_id.
  108. :param lib_id: image lib id
  109. :return: **BceResponse**
  110. """
  111. return self._send_request(http_methods.POST,
  112. b'/v2/imagelib/%s' % compat.convert_to_bytes(
  113. lib_id),
  114. headers={b'Content-Type': b'application/json'},
  115. params={b'deleteLibById': b''},
  116. config=config)
  117. @required(params=dict)
  118. def list_lib(self, params=None, config=None):
  119. """
  120. list lib.
  121. :param params: params for this request
  122. :type params: dict
  123. :return: **dict**
  124. """
  125. body = {
  126. 'type': params['type']
  127. }
  128. return self._send_request(http_methods.POST,
  129. b'/v2/lib/list',
  130. headers={
  131. b'Content-Type': b'application/json'},
  132. body=json.dumps(body),
  133. config=config)
  134. @required(params=dict)
  135. def list_media(self, params=None, config=None):
  136. """
  137. list media.
  138. :param params: params for this request
  139. :type params: dict
  140. :return: **dict**
  141. """
  142. body = {
  143. 'type': params['type'],
  144. 'id': params['id']
  145. }
  146. if params is not None:
  147. if 'pageNo' in params:
  148. body['pageNo'] = params['pageNo']
  149. if 'pageSize' in params:
  150. body['pageSize'] = params['pageSize']
  151. return self._send_request(http_methods.POST,
  152. b'/v2/lib/item/list',
  153. headers={
  154. b'Content-Type': b'application/json'},
  155. body=json.dumps(body),
  156. config=config)
  157. @required(video_lib=(bytes, str), source=(bytes, str))
  158. def insert_video(self, video_lib, source, description=None, notification=None, config=None):
  159. """
  160. insert a video.
  161. :param video_lib: video lib
  162. :type video_lib: string
  163. :param source: video source
  164. :type source: string
  165. :param description: description for this request
  166. :type description: string
  167. :param notification: notification for this request
  168. :type notification: string
  169. :return: **BceResponse**
  170. """
  171. body = {
  172. 'source': source
  173. }
  174. if description is not None:
  175. body['description'] = description
  176. if notification is not None:
  177. body['notification'] = notification
  178. return self._send_request(http_methods.PUT,
  179. b'/v2/videolib/%s' % compat.convert_to_bytes(
  180. video_lib),
  181. headers={
  182. b'Content-Type': b'application/json'},
  183. body=json.dumps(body),
  184. config=config)
  185. @required(video_lib=(bytes, str), source=(bytes, str))
  186. def get_insert_video_task_result(self, video_lib, source, config=None):
  187. """
  188. get insert video task result.
  189. :param video_lib: video lib
  190. :type video_lib: string
  191. :param source: video source
  192. :type source: string
  193. :return: **BceResponse**
  194. """
  195. return self._send_request(http_methods.GET,
  196. b'/v2/videolib/%s' % compat.convert_to_bytes(
  197. video_lib),
  198. headers={
  199. b'Content-Type': b'application/json'},
  200. params={b'source': source},
  201. config=config)
  202. @required(video_lib_id=(bytes, str), media_id=(bytes, str))
  203. def get_insert_video_task_result_by_id(self, video_lib_id, media_id, config=None):
  204. """
  205. get insert video task result by id.
  206. :param video_lib_id: video lib id
  207. :type video_lib_id: string
  208. :param media_id: video id
  209. :type media_id: string
  210. :return: **BceResponse**
  211. """
  212. return self._send_request(http_methods.GET,
  213. b'/v2/videolib/%s' % compat.convert_to_bytes(
  214. video_lib_id),
  215. params={b'getInsertResponseById': b'',
  216. b'mediaId': media_id},
  217. headers={
  218. b'Content-Type': b'application/json'},
  219. config=config)
  220. @required(video_lib=(bytes, str), source=(bytes, str))
  221. def delete_video(self, video_lib, source, config=None):
  222. """
  223. delete a video.
  224. :param video_lib: video lib
  225. :type video_lib: string
  226. :param source: video source
  227. :type source: string
  228. :return: **BceResponse**
  229. """
  230. return self._send_request(http_methods.POST,
  231. b'/v2/videolib/%s' % compat.convert_to_bytes(
  232. video_lib),
  233. headers={
  234. b'Content-Type': b'application/json'},
  235. params={b'deleteVideo': b'',
  236. b'source': source},
  237. config=config)
  238. @required(video_lib_id=(bytes, str), media_id=(bytes, str))
  239. def delete_video_by_id(self, video_lib_id, media_id, config=None):
  240. """
  241. delete a video by id.
  242. :param video_lib_id: video lib id
  243. :type video_lib_id: string
  244. :param media_id: video id
  245. :type media_id: string
  246. :return: **BceResponse**
  247. """
  248. return self._send_request(http_methods.POST,
  249. b'/v2/videolib/%s' % compat.convert_to_bytes(
  250. video_lib_id),
  251. headers={
  252. b'Content-Type': b'application/json'},
  253. params={b'deleteVideoById': b'',
  254. b'mediaId': media_id},
  255. config=config)
  256. @required(video_lib=(bytes, str), source=(bytes, str))
  257. def create_search_video_by_video_task(self, video_lib, source, description=None, notification=None, config=None):
  258. """
  259. create search video by video task.
  260. :param video_lib: video lib
  261. :type video_lib: string
  262. :param source: video source
  263. :type source: string
  264. :param description: description for this request
  265. :type description: string
  266. :param notification: notification for this request
  267. :type notification: string
  268. :return: **BceResponse**
  269. """
  270. body = {
  271. 'source': source
  272. }
  273. if description is not None:
  274. body['description'] = description
  275. if notification is not None:
  276. body['notification'] = notification
  277. return self._send_request(http_methods.POST,
  278. b'/v2/videolib/%s' % compat.convert_to_bytes(
  279. video_lib),
  280. headers={
  281. b'Content-Type': b'application/json'},
  282. params={b'searchByVideo': b''},
  283. body=json.dumps(body),
  284. config=config)
  285. @required(video_lib=(bytes, str), source=(bytes, str))
  286. def get_search_video_by_video_task_result(self, video_lib, source, config=None):
  287. """
  288. get search video by video task result.
  289. :param video_lib: video lib
  290. :type video_lib: string
  291. :param source: video source
  292. :type source: string
  293. :return: **BceResponse**
  294. """
  295. return self._send_request(http_methods.GET,
  296. b'/v2/videolib/%s' % compat.convert_to_bytes(
  297. video_lib),
  298. headers={
  299. b'Content-Type': b'application/json'},
  300. params={b'searchByVideo': b'',
  301. b'source': source},
  302. config=config)
  303. @required(video_lib=(bytes, str), task_id=(bytes, str))
  304. def get_search_video_by_video_task_result_by_id(self, video_lib, task_id, config=None):
  305. """
  306. get search video by video task result by id.
  307. :param video_lib: video lib
  308. :type video_lib: string
  309. :param task_id: video search task id
  310. :type task_id: string
  311. :return: **BceResponse**
  312. """
  313. return self._send_request(http_methods.GET,
  314. b'/v2/videolib/%s' % compat.convert_to_bytes(
  315. video_lib),
  316. headers={
  317. b'Content-Type': b'application/json'},
  318. params={b'getSearchResponseByTaskId': b'',
  319. b'taskId': task_id},
  320. config=config)
  321. @required(video_lib=(bytes, str), source=(bytes, str))
  322. def search_video_by_image(self, video_lib, source, description=None, config=None):
  323. """
  324. search video by image.
  325. :param video_lib: video lib
  326. :type video_lib: string
  327. :param source: image source
  328. :type source: string
  329. :param description: description for this request
  330. :type description: string
  331. :return: **BceResponse**
  332. """
  333. body = {
  334. 'source': source
  335. }
  336. if description is not None:
  337. body['description'] = description
  338. return self._send_request(http_methods.POST,
  339. b'/v2/videolib/%s' % compat.convert_to_bytes(
  340. video_lib),
  341. headers={
  342. b'Content-Type': b'application/json'},
  343. params={b'searchByImage': b''},
  344. body=json.dumps(body),
  345. config=config)
  346. @required(image_lib=(bytes, str), source=(bytes, str))
  347. def insert_image(self, image_lib, source, description=None, config=None):
  348. """
  349. insert an image.
  350. :param image_lib: image lib
  351. :type image_lib: string
  352. :param source: image source
  353. :type source: string
  354. :param description: description for this request
  355. :type description: string
  356. :return: **BceResponse**
  357. """
  358. body = {
  359. 'source': source
  360. }
  361. if description is not None:
  362. body['description'] = description
  363. return self._send_request(http_methods.PUT,
  364. b'/v2/imagelib/%s' % compat.convert_to_bytes(
  365. image_lib),
  366. headers={
  367. b'Content-Type': b'application/json'},
  368. body=json.dumps(body),
  369. config=config)
  370. @required(image_lib=(bytes, str), source=(bytes, str))
  371. def delete_image(self, image_lib, source, config=None):
  372. """
  373. delete a video.
  374. :param image_lib: image lib
  375. :type image_lib: string
  376. :param source: image source
  377. :type source: string
  378. :return: **BceResponse**
  379. """
  380. return self._send_request(http_methods.POST,
  381. b'/v2/imagelib/%s' % compat.convert_to_bytes(
  382. image_lib),
  383. headers={
  384. b'Content-Type': b'application/json'},
  385. params={b'deleteImage': b'',
  386. b'source': source},
  387. config=config)
  388. @required(image_lib_id=(bytes, str), media_id=(bytes, str))
  389. def delete_image_by_id(self, image_lib_id, media_id, config=None):
  390. """
  391. delete a video.
  392. :param image_lib_id: image lib id
  393. :type image_lib_id: string
  394. :param media_id: image id
  395. :type media_id: string
  396. :return: **BceResponse**
  397. """
  398. return self._send_request(http_methods.POST,
  399. b'/v2/imagelib/%s' % compat.convert_to_bytes(
  400. image_lib_id),
  401. headers={
  402. b'Content-Type': b'application/json'},
  403. params={b'deleteImageById': b'',
  404. b'mediaId': media_id},
  405. config=config)
  406. @required(image_lib=(bytes, str), source=(bytes, str))
  407. def search_image_by_image(self, image_lib, source, description=None, config=None):
  408. """
  409. search image by image.
  410. :param image_lib: image lib
  411. :type image_lib: string
  412. :param source: image source
  413. :type source: string
  414. :param description: description for this request
  415. :type description: string
  416. :return: **BceResponse**
  417. """
  418. body = {
  419. 'source': source
  420. }
  421. if description is not None:
  422. body['description'] = description
  423. return self._send_request(http_methods.POST,
  424. b'/v2/imagelib/%s' % compat.convert_to_bytes(
  425. image_lib),
  426. headers={
  427. b'Content-Type': b'application/json'},
  428. params={b'searchByImage': b''},
  429. body=json.dumps(body),
  430. config=config)
  431. @staticmethod
  432. def _merge_config(self, config):
  433. if config is None:
  434. return self.config
  435. else:
  436. new_config = copy.copy(self.config)
  437. new_config.merge_non_none_values(config)
  438. return new_config
  439. def _send_request(
  440. self, http_method, path,
  441. body=None, headers=None, params=None,
  442. config=None,
  443. body_parser=None):
  444. config = self._merge_config(self, config)
  445. if body_parser is None:
  446. body_parser = handler.parse_json
  447. return bce_http_client.send_request(
  448. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  449. http_method, path, body, headers, params)