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:
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:
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:
ReplyDeleteThanks admin for the wonderful post uploaded.
German Classes in Chennai
Java Training in Chennai
CCNA Training in Chennai
Python Training in Chennai
Best Python Training in Chennai
Python Training
Nice article! It is really gave an valuable information and it is easy to understand.
ReplyDeleteCyber Security Training Course in Chennai | Certification | Cyber Security Online Training Course | Ethical Hacking Training Course in Chennai | Certification | Ethical Hacking Online Training Course | CCNA Training Course in Chennai | Certification | CCNA Online Training Course | RPA Robotic Process Automation Training Course in Chennai | Certification | RPA Training Course Chennai | SEO Training in Chennai | Certification | SEO Online Training Course