哪一种比较适合计算fps?

时间:2019-06-09 06:54:53

标签: python-2.7 frame-rate

两个问题:

第一个问题:

这是我的代码,我使用两种方法计算fps,但我需要使用最好的一种。

这是第一行:print 'frame_NO',frame_NO

第二行此行:print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))

第二种方式总是在变化,而不是恒定数,但是第一种方式大约是恒定数

第二个问题: 两个如何计算fps,开始:1,结束:10000或打开??

我使用了以下行:print 'counter', counter 每秒帧数是否正确?

import cv2
import numpy as np
from imutils.video import FPS
# capturing video through webcam
import time
cap = cv2.VideoCapture(0)
#video dimension in python-opencv
width = cap.get(3)  # float
height = cap.get(4) # float
print width,height
time.sleep(2.0)
fps = FPS().start()
start_time = time.time()
counter = 0
while(1):
    counter+=1
    a =(time.time() - start_time)
    frame_NO = counter / a
    print  'frame_NO',frame_NO
    print 'counter', counter
    _, img = cap.read()

    # converting frame(img == BGR) to HSV(hue-saturation-value)

    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    # red color

    red_lower = np.array([136,87,111],np.uint8)
    red_upper = np.array([180,255,255],np.uint8)



    # all color together

    red = cv2.inRange(hsv, red_lower, red_upper)


    # Morphological Transform, Dilation

    kernal = np.ones((5, 5), "uint8")

    red = cv2.dilate(red, kernal)
    res_red = cv2.bitwise_and(img, img, mask = red)



    # Tracking red
    (_, contours, hierarchy)=cv2.findContours(red, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for pic, contour in enumerate(contours):
        area = cv2.contourArea(contour)
        if(area > 300):
            x, y, w, h = cv2.boundingRect(contour)
            img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
            cv2.putText(img, "Red Colour", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255))    



    cv2.imshow("Color Tracking", img)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break
    fps.update()


# stop the timer and display FPS information
    fps.stop()
#    print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
    print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))

请帮助我

先谢谢您

0 个答案:

没有答案