Car Collision Prevention

Car Collision Prevention Project – Overlaying FPS on Top of the Camera Stream

Since performance is critical to the application of our project, I thought it’d be important to log our current sampling frame rate by writing on top of the window.

So I went ahead and kept track of the initial time and the number of frames that have passed to find the average frame rate for every sample displayed.

I also went ahead and added some lines of code to reduce the resolution in an attempt to boost performance. This worked on my mac but not on the Raspberry Pi. I’m still getting around 14 FPS on the Raspberry Pi.

My final code looks something like below. As you can see I commented out the lower resolution settings for both cameras because I didn’t see a performance improvement on the Raspberry Pi.

import cv2
import time

cv2.namedWindow("Left Camera") # New window for previewing the left camera

cv2.namedWindow("Right Camera") # New window for previewing the right camera

vcLeft = cv2.VideoCapture(0) # Load video campture for the left camera

vcRight = cv2.VideoCapture(1) # Load video capture for the right camera

firstTime = time.time() # First time log

totalFramesPassed = 0 # Number of frames passed

if vcLeft.isOpened() and vcRight.isOpened():
    rvalLeft, frameLeft =
    rvalRight, frameRight =

    rvalLeft = False
    rvalRight = False

while rvalLeft and rvalRight: # If the cameras are opened

    cv2.putText(frameLeft, "FPS : " + str(totalFramesPassed / (time.time() - firstTime)),(40, 40),cv2.FONT_HERSHEY_SIMPLEX,1,(225,255,0))

    cv2.imshow("Left Camera", frameLeft)

    cv2.putText(frameRight, "FPS : " + str(totalFramesPassed / (time.time() - firstTime)),(40, 40),cv2.FONT_HERSHEY_SIMPLEX,1,(225,255,0))

    cv2.imshow("Right Camera", frameRight)

    rvalLeft, frameLeft =

    rvalRight, frameRight =

    key = cv2.waitKey(20)

    totalFramesPassed = totalFramesPassed + 1 # One frame passed, increment

    if key == 27:


# When the user hits escape, the windows will get destroyed

cv2.destroyWindow("Left Camera")

cv2.destroyWindow("Right Camera")