Skip to content

Commit

Permalink
Added FPS counter
Browse files Browse the repository at this point in the history
  • Loading branch information
justinmerrell committed Nov 29, 2022
1 parent f6f8360 commit 2e7f9ba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div align="center">

# blokbot.io | OpenBlok
# OpenBlok | blokbot.io

[![CI | Pylint](https://github.com/blokbot-io/OpenBlok/actions/workflows/pylint.yml/badge.svg)](https://github.com/blokbot-io/OpenBlok/actions/workflows/pylint.yml)
&nbsp;
Expand All @@ -12,7 +12,7 @@

## Table of Contents

- [blokbot.io | OpenBlok](#blokbotio--openblok)
- [OpenBlok | blokbot.io](#openblok--blokbotio)
- [Table of Contents](#table-of-contents)
- [Hardware Requirements](#hardware-requirements)
- [Installation](#installation)
Expand Down
26 changes: 11 additions & 15 deletions bloks/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,12 @@
import numpy as np
from screeninfo import get_monitors # Required to get monitor info


from bloks import camera, serial # , upload


from bloks.utils import annotate, preprocess, bounding_boxes, crop_square

from bloks.utils import annotate, preprocess, stats, bounding_boxes, crop_square

from modeled import location, e2e


# if os.environ.get('DISPLAY', '') == '':
# os.environ.__setitem__('DISPLAY', ':0.0')
STATS = stats.Stats()


def predict_and_show():
Expand All @@ -48,6 +42,8 @@ def predict_and_show():
frame, frame_time = camera.grab_frame() # Grab frame
frame_time = Decimal(frame_time) # Copy frame & time

STATS.add_frame_time(frame_time) # Add frame time to stats

preprocessed_frame = preprocess.capture_regions(
frame) # Preprocess frame
# Frame to add annotations to
Expand Down Expand Up @@ -195,13 +191,13 @@ def predict_and_show():
cv2.FONT_HERSHEY_DUPLEX, 3, (255, 0, 0), 5
)

# Note: Extra?
# Display the resulting frame
# cv2.imshow('frame', combined_layers)

# Press Q on keyboard to exit
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# Display FPS in the upper right
combined_layers = cv2.putText(
combined_layers,
f"FPS: {STATS.fps}",
(combined_layers.shape[1]-300, 100),
cv2.FONT_HERSHEY_DUPLEX, 3, (255, 0, 0), 5
)

# ----------------------------- Display ----------------------------- #
# Resize image to fit monitor (does not maintain aspect ratio)
Expand Down
24 changes: 24 additions & 0 deletions bloks/utils/stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
''' OpenBlok | bloks | utils | stats.py '''


class Stats:
''' Running statistics '''

def __init__(self):
self.frame_time_log = []

def add_frame_time(self, frame_time):
'''
Stores time stamps for 1 second of frames
'''
time_difference = frame_time - self.frame_time_log[0]
if time_difference > 1:
self.frame_time_log.pop(0)

self.frame_time_log.append(frame_time)

def fps(self):
'''
Calculate frames per second
'''
return len(self.frame_time_log)

0 comments on commit 2e7f9ba

Please sign in to comment.