| 
 | 1 | +#-------------------------------------------------------------------------------  | 
 | 2 | +# SPDX-License-Identifier: MIT  | 
 | 3 | +#   | 
 | 4 | +# Copyright (c) 2025 SparkFun Electronics  | 
 | 5 | +#-------------------------------------------------------------------------------  | 
 | 6 | +# ex05_performance.py  | 
 | 7 | +#   | 
 | 8 | +# This example  | 
 | 9 | +#-------------------------------------------------------------------------------  | 
 | 10 | + | 
 | 11 | +# Import OpenCV and hardware initialization module  | 
 | 12 | +import cv2 as cv  | 
 | 13 | +from cv2_hardware_init import *  | 
 | 14 | + | 
 | 15 | +# Import NumPy  | 
 | 16 | +from ulab import numpy as np  | 
 | 17 | + | 
 | 18 | +# Import time for frame rate calculation  | 
 | 19 | +import time  | 
 | 20 | + | 
 | 21 | +# Initialize a loop timer to calculate processing speed in FPS  | 
 | 22 | +loop_time = time.ticks_us()  | 
 | 23 | + | 
 | 24 | +# Open the camera  | 
 | 25 | +camera.open()  | 
 | 26 | + | 
 | 27 | +# The `read()` method of OpenCV camera drivers can optionally take an output  | 
 | 28 | +# image as an argument. When it's not provided, the camera driver must allocate  | 
 | 29 | +# a whole new image for the frame, which can be slow and waste memory. If the  | 
 | 30 | +# image argument is provided, then the camera driver will write the data to the  | 
 | 31 | +# provided image. The image must be a NumPy array with the same shape and data  | 
 | 32 | +# type as the camera's  | 
 | 33 | +success, frame = camera.read()  | 
 | 34 | + | 
 | 35 | +# Prompt the user to press a key to continue  | 
 | 36 | +print("Press any key to continue")  | 
 | 37 | + | 
 | 38 | +# Loop to continuously read frames from the camera and display them  | 
 | 39 | +while True:  | 
 | 40 | +    # Read a frame from the camera  | 
 | 41 | +    success, frame = camera.read()  | 
 | 42 | +    if not success:  | 
 | 43 | +        print("Failed to read frame from camera")  | 
 | 44 | +        break  | 
 | 45 | + | 
 | 46 | +    # Now we'll   | 
 | 47 | + | 
 | 48 | +    # It's a good idea to measure the frame rate of the main loop to see how  | 
 | 49 | +    # fast the entire pipeline is running. This will include not only the  | 
 | 50 | +    # processing steps, but also any overhead from the hardware drivers and  | 
 | 51 | +    # other code. We can calculate the FPS with the loop timer and draw it on  | 
 | 52 | +    # the frame for visualization  | 
 | 53 | +    current_time = time.ticks_us()  | 
 | 54 | +    fps = 1_000_000 / (current_time - loop_time)  | 
 | 55 | +    loop_time = current_time  | 
 | 56 | +    frame = cv.putText(frame, f"FPS: {fps:.2f}", (10, 30), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)  | 
 | 57 | + | 
 | 58 | +    # Display the frame  | 
 | 59 | +    cv.imshow(display, frame)  | 
 | 60 | + | 
 | 61 | +    # Check for key presses  | 
 | 62 | +    key = cv.waitKey(1)  | 
 | 63 | + | 
 | 64 | +    # If any key is pressed, exit the loop  | 
 | 65 | +    if key != -1:  | 
 | 66 | +        break  | 
 | 67 | + | 
 | 68 | +# Release the camera  | 
 | 69 | +camera.release()  | 
0 commit comments