python opencv 学习之视频输入之一

来源:互联网 发布:鞋子折痕淘宝不让退 编辑:程序博客网 时间:2024/06/11 17:31

学习简单的Python opencv视频读入。

#coding=utf-8import cv2.cv as cv#获取视频,capture capture = cv.CaptureFromFile('myvideo.mp4')#获取视频的帧集合对象个数frames = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT))#CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream 视频流帧的宽度#CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream 视频流帧的高度#获取帧率fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)#两帧间的间隔时间wait = int(1/fps * 1000/1)#视频的时间长度duration = (frames * fps) / 1000print 'Num. Frames = ', framesprint 'Frame Rate = ', fps, 'fps'print 'Duration = ', duration, 'sec'#遍历所有的帧for f in xrange( frames ):#抓取后,capture被指向下一帧frameImg = cv.QueryFrame(capture)#获取当前帧的位置,并且写入图片中num= cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_POS_FRAMES)#创建一个矩形,来让我们在图片上写文字,参数依次定义了文字类型,高,宽,字体厚度等。。font=cv.InitFont(cv.CV_FONT_HERSHEY_SCRIPT_SIMPLEX, 1, 1, 0, 3, 8)text='%d' %num;cv.PutText(frameImg, text, (30,30), font, (0,255,0))#显示当前帧    cv.ShowImage("The Video", frameImg)cv.WaitKey(wait)


0 0