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))
print(fpsInfo)
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'):
break
# Release handle to the webcam
camera.release()
cv2.destroyAllWindows()

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

References

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

3 responses to “#VSCode – 20 lines to display a webcam camera feed with #Python using #OpenCV”

  1. […] Source post with Python Code, VSCode – 20 lines to display a webcam camera feed with #Python using #OpenCV […]

    Like

  2. […] Visual Studio Code – 20 lines to display a webcam camera feed with Python using OpenCV […]

    Like

  3. […] Visual Studio Code – 20 lines to display a webcam camera feed with Python using OpenCV […]

    Like

Leave a comment

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading