7 lines of Python code to show your webcam in a GUI window using OpenCV

7 lines of Python code to show your webcam in a GUI window using OpenCV

7 lines of Python code to show your webcam in a GUI window using OpenCV

7 lines of Python code to show your webcam in a GUI window using OpenCV

import cv2, PySimpleGUI as sg

window = sg.Window('Demo Application - OpenCV Integration', [[sg.Image(filename='', key='image')],], location=(800,400))

cap = cv2.VideoCapture(0)       # Setup the camera as a capture device

while True:                     # The PSG "Event Loop"

    event, values = window.Read(timeout=20, timeout_key='timeout')      # get events for the window with 20ms max wait

    if event is None:  break                                            # if user closed window, quit

    window.FindElement('image').Update(data=cv2.imencode('.png', cap.read()[1])[1].tobytes()) # Update image in window

 

"""

Putting the comment at the bottom so that you can see that the code is indeed 7 lines long.  And, there is nothing

done out of the ordinary to make it 7 lines long.  There are no ; for example.  OK, so the if statement is on one line

but that's the only place that you would traditionally see one more line.  So, call it 8 if you want.

"""

The code creates this window:

https://user-images.githubusercontent.com/13696193/62974873-1b063f00-bde7-11e9-903e-d87a7def96b0.gif

(Some people having hard time seeing the GIF so pasting the link too)

 

python opencv

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

top 30 Python Tips and Tricks for Beginners

In this post, we'll learn top 30 Python Tips and Tricks for Beginners

Lambda, Map, Filter functions in python

You can learn how to use Lambda,Map,Filter function in python with Advance code examples. Please read this article

Create a Virtual Pen and Eraser with Python OpenCV - Genial Code

Learn Free how to create a virtual pen and eraser with python and OpenCV with source code and complete guide. This entire application is built fundamentally on contour detection. It can be thought of as something like closed color curves on compromises that have the same color or intensity, it's like a blob. In this project we use color masking to get the binary mask of our target color pen, then we use the counter detection to find the location of this pen and the contour to find it.

Video Chat App Python: How to Create a Video Chat App Using Python and OpenCV

In this tutorial, we'll learn Video Chat App Python: How to Create a Video Chat App Using Python and OpenCV. Very simple, in just a few steps.

Python Imread(): Different Ways to Load an Image using The OpenCV.imread() Method

In order to use the Python imread() method, we require the cv2 module of the opencv-python library. For that, we have to first install the opencv-python