Monday, February 28, 2011

[Py] Using get_fps function of pygame

There are modules of pygame which provide time related functions. One of them is the get_fps which can be used to know the fps of executing program.

To use the get_fps function, we have to create a Clock object:
clock = pygame.time.Clock()
Then, use the tick method and get_fps:
clock.tick()
print "fps:", clock.get_fps()
Here is the testing code, which is almost the same with the one used in my old post:
import pygame
import Image
import ImageOps
from pygame.locals import *
import sys 

import opencv
from opencv import highgui

camera = highgui.cvCreateCameraCapture(0)
def get_image():
        im = highgui.cvQueryFrame(camera)
        return ImageOps.mirror(opencv.adaptors.Ipl2PIL(im))

pygame.init()
window = pygame.display.set_mode((640,480))
pygame.display.set_caption("WebCam Demo")
screen = pygame.display.get_surface()

clock = pygame.time.Clock()

while True:
        events = pygame.event.get()
        for event in events:
                if event.type == QUIT or event.type == KEYDOWN:
                        highgui.cvReleaseCapture(camera)
                        sys.exit(0)
        clock.tick()
        print "fps:", clock.get_fps()

        im = get_image()
        pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)

        screen.blit(pg_img, (0,0))
        pygame.display.flip()


Note that the print slow down the program significantly, so what we get is actually NOT the original fps but a slower one.

What I have shown in this post may be not a good implementation of getting real-time fps information. It is just a trial to utilize the get_fps function. If you know any better approach, please show me related information. Thank you.

---
Ref: Pygame module for monitoring time

No comments:

Post a Comment