Skip to content

[Python] Using OpenCV package to call laptop camera

Last Updated on 2021-04-07 by Clay

The blog chapter is seem very like my other chapter —— Screening with Python —— Provided a sample code, but in one hand it's a screening tutorial, in other hand is camera.

# -*- coding: utf-8 -*-
import cv2

cam = cv2.VideoCapture(0)

while True:
    ret, img = cam.read()
    vis = img.copy()
    cv2.imshow('Camera', vis)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cam.release()
cv2.destroyAllWindows()


The method of calling is very simple. Basically, as long as the camera of the Notebook is not broken, the code "VideoCapture(0)" is read.

the loop part is the part of the shot. Finally, press the "q" button to leave the loop and the program will close automatically.

Although this is a fairly basic program, if you combine it with some other package, you can produce a lot of good features, you may wish to play.

Leave a Reply