You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
4.3KB

  1. import cv2,os
  2. import time
  3. import subprocess as sp
  4. # 图片合并
  5. # def readImage():
  6. # p1 = cv2.imread("C:/Users/chenyukun/Pictures/Camera Roll/a.jpg")
  7. # p2 = cv2.imread("C:/Users/chenyukun/Pictures/Camera Roll/b.jpg")
  8. # ret = cv2.add(p1, p2)
  9. # cv2.imwrite(r"C:\Users\chenyukun\Pictures\Camera Roll\aa.jpg", ret)
  10. #
  11. # readImage()
  12. # https://opencv.apachecn.org/#/docs/4.0.0/2.1-tutorial_py_image_display
  13. if __name__ == "__main__":
  14. # print(cv2.__version__)
  15. # # 读取图像
  16. # p1 = cv2.imread("C:/Users/chenyukun/Pictures/Camera Roll/a.jpg", 0) # 以灰度模式加载图像
  17. # p2 = cv2.imread("C:/Users/chenyukun/Pictures/Camera Roll/b.jpg", 1) # **cv.IMREAD_COLOR**:加载彩色图像,任何图像的透明度都会被忽略,它是默认标志
  18. # p3 = cv2.imread("C:/Users/chenyukun/Pictures/Camera Roll/b.jpg", -1) # 加载图像,包括 alpha 通道
  19. # p4 = cv2.imread("C:/Users/chenyukun/Pictures/Camera Roll/a.jpg")
  20. # p5 = cv2.imread("C:/Users/chenyukun/Pictures/Camera Roll/c.jpg")
  21. # print(type(p5))
  22. # ret = cv2.add(p4, p5)
  23. # # 显示图像
  24. # cv2.imshow('frame', p1)
  25. # cv2.imshow('frame1', p2)
  26. # cv2.imshow('frame2', p3)
  27. # # 对显示窗口做调整,WINDOW_AUTOSIZE不可调整,WINDOW_NORMAL可调整窗口大小
  28. # cv2.namedWindow('frame3', cv2.WINDOW_AUTOSIZE)
  29. # cv2.imshow('frame3', ret)
  30. # # 等待时间 按下任务键
  31. # k = cv2.waitKey(1) & 0xFF
  32. # if k == 27: #ESC退出
  33. # cv2.destroyAllWindows()
  34. # elif k == ord('s'): # 's' 保存退出
  35. # # 保存图像
  36. # cv2.imwrite("C:/Users/chenyukun/Pictures/Camera Roll/d.jpg", ret)
  37. # cv2.destroyAllWindows()
  38. # 视频入门
  39. try:
  40. cap = cv2.VideoCapture("rtmp://live.play.t-aaron.com/live/THSAf_hd") # 0:表示连接一台摄像机
  41. print(cap.isOpened())
  42. print(cap)
  43. except Exception as e:
  44. print(e)
  45. raise e
  46. print("aaaa")
  47. # 有时,cap 可能没有初始化 capture。在这种情况下,此代码显示错误。你可以通过该方法 cap.isOpened() 检查它是否初始化。
  48. # 如果它是 True,那么是好的,否则用 cap.open() 打开在使用。
  49. fps = int(cap.get(cv2.CAP_PROP_FPS))
  50. width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
  51. height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
  52. print(fps,width,height)
  53. # # 设置宽
  54. # print(cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320))
  55. # # 设置高
  56. # print(cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240))
  57. # 声明编码器和创建 VideoWrite 对象
  58. fourcc = cv2.VideoWriter_fourcc(*'mp4v')
  59. out = cv2.VideoWriter('/home/DATA/dsp/ai/video/eee.mp4',fourcc, fps, (width, height))
  60. command=['/usr/bin/ffmpeg',
  61. '-y', # 覆盖输出文件
  62. '-f', 'rawvideo', # 强迫采用格式fmt
  63. '-vcodec','rawvideo',
  64. '-pix_fmt', 'bgr24',
  65. '-s', "{}x{}".format(640, 480),# 图片分辨率
  66. '-r', str(25.0),# 视频帧率
  67. '-i', '-',
  68. '-c:v', 'libx264',
  69. '-pix_fmt', 'yuv420p',
  70. '-preset', 'ultrafast',
  71. '-f', 'flv',
  72. "rtmp://live.push.t-aaron.com/live/THSAb"]
  73. # p = sp.Popen(command, stdin=sp.PIPE)
  74. start = time.time()
  75. while True:
  76. try:
  77. if not cap.isOpened():
  78. cap = cv2.VideoCapture("rtmp://live.play.t-aaron.com/live/THSAf_hd")
  79. continue
  80. # 一帧一帧捕捉
  81. ret, frame = cap.read() # 返回一个 bool 值(True/False)。如果加载成功,它会返回True
  82. if not ret:
  83. cap = cv2.VideoCapture("rtmp://live.play.t-aaron.com/live/THSAf_hd")
  84. continue
  85. # print(ret) #True
  86. # # 我们对帧的操作在这里
  87. # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  88. end = time.time()
  89. print("bbbbbbbbb")
  90. # p.stdin.write(frame.tostring())
  91. # print("aaaaa")
  92. out.write(frame)
  93. except Exception as e:
  94. raise e
  95. # # 显示返回的每帧
  96. # cv2.imshow('frame',gray)
  97. # if cv2.waitKey(1) & 0xFF == ord('q'):
  98. # break
  99. # 当所有事完成,释放 VideoCapture 对象
  100. cap.release()
  101. out.release()
  102. cv2.destroyAllWindows()