|
- import cv2,os
- import time
- import subprocess as sp
- # 图片合并
- # def readImage():
- # p1 = cv2.imread("C:/Users/chenyukun/Pictures/Camera Roll/a.jpg")
- # p2 = cv2.imread("C:/Users/chenyukun/Pictures/Camera Roll/b.jpg")
- # ret = cv2.add(p1, p2)
- # cv2.imwrite(r"C:\Users\chenyukun\Pictures\Camera Roll\aa.jpg", ret)
- #
- # readImage()
- # https://opencv.apachecn.org/#/docs/4.0.0/2.1-tutorial_py_image_display
- if __name__ == "__main__":
- # print(cv2.__version__)
- # # 读取图像
- # p1 = cv2.imread("C:/Users/chenyukun/Pictures/Camera Roll/a.jpg", 0) # 以灰度模式加载图像
- # p2 = cv2.imread("C:/Users/chenyukun/Pictures/Camera Roll/b.jpg", 1) # **cv.IMREAD_COLOR**:加载彩色图像,任何图像的透明度都会被忽略,它是默认标志
- # p3 = cv2.imread("C:/Users/chenyukun/Pictures/Camera Roll/b.jpg", -1) # 加载图像,包括 alpha 通道
- # p4 = cv2.imread("C:/Users/chenyukun/Pictures/Camera Roll/a.jpg")
- # p5 = cv2.imread("C:/Users/chenyukun/Pictures/Camera Roll/c.jpg")
- # print(type(p5))
- # ret = cv2.add(p4, p5)
- # # 显示图像
- # cv2.imshow('frame', p1)
- # cv2.imshow('frame1', p2)
- # cv2.imshow('frame2', p3)
- # # 对显示窗口做调整,WINDOW_AUTOSIZE不可调整,WINDOW_NORMAL可调整窗口大小
- # cv2.namedWindow('frame3', cv2.WINDOW_AUTOSIZE)
- # cv2.imshow('frame3', ret)
- # # 等待时间 按下任务键
- # k = cv2.waitKey(1) & 0xFF
- # if k == 27: #ESC退出
- # cv2.destroyAllWindows()
- # elif k == ord('s'): # 's' 保存退出
- # # 保存图像
- # cv2.imwrite("C:/Users/chenyukun/Pictures/Camera Roll/d.jpg", ret)
- # cv2.destroyAllWindows()
-
-
-
- # 视频入门
- try:
- cap = cv2.VideoCapture("rtmp://live.play.t-aaron.com/live/THSAf_hd") # 0:表示连接一台摄像机
- print(cap.isOpened())
- print(cap)
- except Exception as e:
- print(e)
- raise e
- print("aaaa")
- # 有时,cap 可能没有初始化 capture。在这种情况下,此代码显示错误。你可以通过该方法 cap.isOpened() 检查它是否初始化。
- # 如果它是 True,那么是好的,否则用 cap.open() 打开在使用。
- fps = int(cap.get(cv2.CAP_PROP_FPS))
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
- print(fps,width,height)
- # # 设置宽
- # print(cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320))
- # # 设置高
- # print(cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240))
-
- # 声明编码器和创建 VideoWrite 对象
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
- out = cv2.VideoWriter('/home/DATA/dsp/ai/video/eee.mp4',fourcc, fps, (width, height))
- command=['/usr/bin/ffmpeg',
- '-y', # 覆盖输出文件
- '-f', 'rawvideo', # 强迫采用格式fmt
- '-vcodec','rawvideo',
- '-pix_fmt', 'bgr24',
- '-s', "{}x{}".format(640, 480),# 图片分辨率
- '-r', str(25.0),# 视频帧率
- '-i', '-',
- '-c:v', 'libx264',
- '-pix_fmt', 'yuv420p',
- '-preset', 'ultrafast',
- '-f', 'flv',
- "rtmp://live.push.t-aaron.com/live/THSAb"]
- # p = sp.Popen(command, stdin=sp.PIPE)
- start = time.time()
- while True:
-
- try:
- if not cap.isOpened():
- cap = cv2.VideoCapture("rtmp://live.play.t-aaron.com/live/THSAf_hd")
- continue
- # 一帧一帧捕捉
- ret, frame = cap.read() # 返回一个 bool 值(True/False)。如果加载成功,它会返回True
- if not ret:
- cap = cv2.VideoCapture("rtmp://live.play.t-aaron.com/live/THSAf_hd")
- continue
- # print(ret) #True
- # # 我们对帧的操作在这里
- # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- end = time.time()
- print("bbbbbbbbb")
- # p.stdin.write(frame.tostring())
- # print("aaaaa")
- out.write(frame)
- except Exception as e:
- raise e
- # # 显示返回的每帧
- # cv2.imshow('frame',gray)
- # if cv2.waitKey(1) & 0xFF == ord('q'):
- # break
- # 当所有事完成,释放 VideoCapture 对象
- cap.release()
- out.release()
- cv2.destroyAllWindows()
|