FPS with cv2.VideoCapture

# -*- coding: utf-8 -*-
"""
Created on Thu Nov 15 22:37:02 2018
#QQ群:476842922(欢迎加群讨论学习)
@author: Administrator
"""
# import the necessary packages
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import cv2
 
# construct the argument parse and parse the arguments解析参数
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", required=True,
	help="path to input video file")
args = vars(ap.parse_args())
 
# open a pointer to the video stream and start the FPS timer视频流帧
stream = cv2.VideoCapture(args["video"])
fps = FPS().start()

# loop over frames from the video file stream
while True:
	# grab the frame from the threaded video file stream
	(grabbed, frame) = stream.read()
 
	# if the frame was not grabbed, then we have reached the end
	# of the stream
	if not grabbed:
		break
 
	# resize the frame and convert it to grayscale (while still
	# retaining 3 channels)
	frame = imutils.resize(frame, width=450)
	frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
	frame = np.dstack([frame, frame, frame])
 
	# display a piece of text to the frame (so we can benchmark
	# fairly against the fast method)
	cv2.putText(frame, "Slow Method", (10, 30),
		cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)	
 
	# show the frame and update the FPS counter
	cv2.imshow("Frame", frame)
	cv2.waitKey(1)
	fps.update()

# stop the timer and display FPS information
fps.stop()
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
# do a bit of cleanup
stream.release()
cv2.destroyAllWindows()

(base) C:\Users\Administrator>python Videos.py --video aa.mp4
[INFO] elasped time: 72.97
[INFO] approx. FPS: 67.82

你可能感兴趣的:(代码,FPS,with)