#VSCode – 20 lines to display a webcam camera feed with #Python using #OpenCV

Hi !

I always write this from scratch, so it seems that I’ll drop this one here. So next time I search for this, I’ll find myself.

import os
import cv2
import time
# init camera
execution_path = os.getcwd()
camera = cv2.VideoCapture(0)
while True:
# Init and FPS process
start_time = time.time()
# Grab a single frame of video
ret, frame = camera.read()
# calculate FPS >> FPS = 1 / time to process loop
fpsInfo = "FPS: " + str(1.0 / (time.time() start_time))
cv2.putText(frame, fpsInfo, (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1)
# Display the resulting image
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
# Release handle to the webcam
view raw PythonWebCamFeed.py hosted with ❤ by GitHub

And with some extra lines, we can even detect faces and display some face landmarks:

This is the base of some many image recognition scenarios, so I hope this will save me some local search time 😀

Happy coding!

Greetings @ Toronto

El Bruno


My posts on Face Recognition using Python

  1. Detecting Faces with 20 lines in Python
  2. Face Recognition with 20 lines in Python
  3. Detecting Facial Features with 20 lines in Python
  4. Facial Features and Face Recognition with 20 lines in Python
  5. Performance improvements with code
  6. More performance improvements, lowering the camera resolution

And some general Python posts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.