Skip to content

Instantly share code, notes, and snippets.

@Nannigalaxy
Created September 7, 2020 03:44
Show Gist options
  • Save Nannigalaxy/0fbe211b11950d49c2085c8d8e79eb3f to your computer and use it in GitHub Desktop.
Save Nannigalaxy/0fbe211b11950d49c2085c8d8e79eb3f to your computer and use it in GitHub Desktop.
Simple Webcam Video Streaming with FPS count using OpenCV Python 3
'''
Date: 07/09/2020
Written by Nandan M
'''
import cv2
import time
# 0 is default webcam. Change to switch between multiple cameras or IP address.
vc = cv2.VideoCapture(0)
start_time = time.time()
# FPS update time in seconds
display_time = 2
fc = 0
FPS = 0
while True:
_, frame = vc.read()
# OpenCV reads images in BGR color format by default.
#frame_RGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
fc+=1
TIME = time.time() - start_time
if (TIME) >= display_time :
FPS = fc / (TIME)
fc = 0
start_time = time.time()
fps_disp = "FPS: "+str(FPS)[:5]
# Add FPS count on frame
image = cv2.putText(frame, fps_disp, (10, 25),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
# imshow converts BGR to RGB while saving or displaying.
cv2.imshow('Video Stream w/ FPS', image)
key = cv2.waitKey(1) & 0xFF
# press q to quit streaming
if key == ord("q"):
break
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment