camera_by_cv2.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # update: 2022-2-23
  2. """
  3. image.shape: 图片尺寸 (height, width, number)
  4. """
  5. import cv2 as cv
  6. import time
  7. def get_capture_once(capture_path):
  8. """获取capture"""
  9. # --- check ---
  10. cap = cv.VideoCapture(capture_path)
  11. if not cap.isOpened():
  12. return None
  13. # --- check ---
  14. fps = int(cap.get(cv.CAP_PROP_FPS))
  15. if not 4 < fps < 100:
  16. return None
  17. return cap
  18. def get_capture(capture_path):
  19. """获取capture"""
  20. while True:
  21. # --- check ---
  22. cap = cv.VideoCapture(capture_path)
  23. if not cap.isOpened():
  24. print('m1: sleep 3s.')
  25. time.sleep(3)
  26. continue
  27. # --- check ---
  28. fps = int(cap.get(cv.CAP_PROP_FPS))
  29. if not 4 < fps < 100:
  30. print('m2: sleep 3s.')
  31. time.sleep(3)
  32. continue
  33. return cap
  34. def rtsp_is_live(capture_path):
  35. """"""
  36. # --- check ---
  37. cap = cv.VideoCapture(capture_path)
  38. if not cap.isOpened():
  39. return False
  40. # --- check ---
  41. fps = int(cap.get(cv.CAP_PROP_FPS))
  42. if not 4 < fps < 100:
  43. return False
  44. return True
  45. if __name__ == '__main__':
  46. print(get_capture('rtsp://admin:DEVdev123@192.168.30.235:554/h264/ch1/sub/av_stream'))
  47. print(rtsp_is_live('rtsp://admin:DEVdev123@192.168.30.235:554/h264/ch1/sub/av_stream'))