Skip to main content

FPS counter with OpenCV

Showing FPS on the screen is an important part of benchmarking your app. In this post we're going to look at how to calculate the number of frames per second and show it on screen.

The theory is that we keep track of the last 5 or so timestamps and average over to get the FPS. We keep deleting the last item so the list only has 5 times in it. Lets have a look at the code:

1:  import time  
2:    
3:  times = []  
4:  count = 5  
5:    
6:  times.append(time.time())  
7:  if len(times) >= count:  
8:    times = times[-count:]  
9:    fps = "%.2f FPS" % (count/(times[-1] - times[0]))  
10:   cv2.putText(frame, fps, (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1, cv2.LINE_AA) 

First import time, which is required for this to work. Then I define two constants which will be used in the function. Remember this code will run every time the videoloop runs, it adds the time to the list. If the list gets too long, get the last bit using indexing. Then work out the average time by dividing by the length. I use string formatting to get rid of all the decimal places, apart from the last two. Then I show it on the image. The arguments are, respectively, the image to draw on, the text, the coordinates to draw on, the font, the size, the colour. Normally I keep the last two the same (the thickness and the anti-aliasing to use).

Here is the result with my GoT cast facial recognition program:


Comments

Post a Comment

Popular posts from this blog

Getting the icon association for any file or folder with the Python Windows API

I wanted to make a file browser which has the correct file icons for each file type, instead of just getting them from the file name. This is a bad solution, since it has to call images from disc and may be wrong if the name is different. This method uses the Windows API to get the exact icon. Tested in Python 3.6 in Windows 10. from win32com.shell import shell, shellcon from PIL import Image, ImageTk import win32api import win32con import win32ui import win32gui def get_icon(PATH, size): SHGFI_ICON = 0x000000100 SHGFI_ICONLOCATION = 0x000001000 if size == "small": SHIL_SIZE= 0x00001 elif size == "large": SHIL_SIZE= 0x00002 else: raise TypeError("Invalid argument for 'size'. Must be equal to 'small' or 'large'") ret, info = shell.SHGetFileInfo(PATH, 0, SHGFI_ICONLOCATION | SHGFI_ICON | SHIL_SIZE) hIcon, iIcon, dwAttr, name, typeName = info ico_x =

Using inno to package your Python application

So you have just compiled your Windows Python application. You want to make an installer for your users to easily get the application onto their PCs. You have tried using cx_Freeze's bdist_msi option but it is not advanced enough. Perhaps you want to run a script on installation or on uninstall, or want an easy way of adding an icon to the user's desktop. Now the easiest tool to use is inno. In this post we're going to take a look at how to use inno's setup wizard to package your application, as well as editing the script file. So the first thing you are going to want to do is to jump onto  inno's download page  and download inno setup compiler. Once it's done, open the application and select 'Create a new script file using the Script Wizard'. You will then be greeted with the following window: Leave the box unchecked. We're going to use the Wizard to make the base of our install script. Fill out all the information in the entry bo