Search code examples
python-3.xgtk3pygobjectwebcam-capture

How to show image from webcam


I never needed to work with camera before, so I do not know where to start. I need to display the image of a real-time camera, to capture and save to a file. I'm using python3 and gtk3.

Does gtk has any feature for this?


Solution

  • The easiest way to get the image from a webcam is to use OpenCV. It allows you to get the image with just 2 lines of code and 2 more to show it, like so:

    import numpy as np
    import cv2
    
    cap = cv2.VideoCapture(0)
    ret, frame = cap.read()
    
    cv2.imshow('frame',frame)
    cv2.waitKey(0)
    

    But there is a downside, namely that OpenCV for Python 3 has to be build from source. Most people don't like building from source so they say it is not available.

    Luckily there is the Unofficial Windows Binaries for Python Extension Packages by Gohlke (University of California) which also offers a precompiled version of OpenCV for Python 3. Installing that using pip should be easy.