Displaying Webcam in OpenCV

To capture a real-time video from webcam and display, OpenCV has a built in function/ object called “VideoCapture”. To use the object, we need to import cv2. I used the line:

camera = cv2.VideoCapture(0) — to display the webcam.

Since we want continuous webcam display, we need to run a loop afterwards.

Note: cv2.VideoCapture(0) is for a single webcam, cv2.VideoCapture(1) is for 2 webcams and so on.. (I am broke, so I used one, Haha)

Here is the code in PyCharm:

import cv2

camera = cv2.VideoCapture(0) # VideoCapture(0) is for using a single webcam
# VideoCapture(1) is for using double webcam... so on

while True: # We are running a loop here for continuous webcam operation
success, img = camera.read() # Reads from the camera and stores data in img
cv2.imshow("MyWebcam", img) # Showing the stored img data in MyWebcam

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


Displaying an Image

To display an image, we first need to define the path/ location of the image file that we want to see. I used the exact path of the file from the file properties and put it in a variable called “path” (you need to change the slash signs from “\” to “/”, as python can only read “/” this slash.

Then we need a fuction called “imread” to read the file from the specified path. So I put the whole path variable in the imread argument.

Grayscaling, Blurring and Edge Detection

Grayscaling an image:

Just put an extra ‘0’ in the imread argument; that converts your image to grayscale!

Blurring an image:

To blur an image, I used a function called “GaussianBlur (x,y,z)” which takes at least 3 argunments: the image file, the kernel size or “ksize” and sigmax. in the code, I used a line “img3 = cv2.GaussianBlur(img2, (5, 5), 0)”

Here, the arguments are:

img2: this is the image file that we will be blurring
(5, 5): this is the Kernel size, which means the blurring strength. This takes only odd number as values and as the values increase, the blur becomes stronger.
0: this is the sigmax value, I don’t have much idea about it yet.

Edge detection:

To detect the edges, I used the “Canny” function which takes 3 arguments, the image file and the Threshold1 & the Threshold2 values which determines the edge intensities.


Here is the full code:


import cv2

path = "C:/Users/mehra/OneDrive/Pictures/tomcat.png" # Path of the png file in computer
img = cv2.imread(path) # Read from path and save in img
img2 = cv2.imread(path, 0) # (path, 0) converts it in graysccale
img3 = cv2.GaussianBlur(img2,(5,5),0) # GaussianBlur function is used to blur
# the image. It takes at least 3 inputs,
# the file, the ksize and the sigmax

img4 = cv2.Canny(img2, 200, 200) # Edge detection command. Threshold1 = 200
# Threshold2 = 200. We have to figure out the
# Threshold values
cv2.waitKey(0) # waitKey defines how long the image should
# appear on the screen. waitKey(10000) means
# the image will be displayed on screen for 10
# seconds and then disappear


Hand detection

OpenCV has an amazing function called “HandDetector” to detect your hand and specify points. You can do specific works using the points, for example, you can use the points for a hand gesture dependent volume controller project.


Here is the code:

import cv2
from cvzone.HandTrackingModule import HandDetector
import math

# Read from Webcam
cap = cv2.VideoCapture(0)
cap.set(3, 1024) # 3 means width
cap.set(4, 720) # 4 means height

# Declaring HandDetector
detector = HandDetector(detectionCon= 0.7, minTrackCon= 0.7, maxHands= 2)

# Loop
while True:
success, img = cap.read()
hands, img = detector.findHands(img)

if hands:
lmList = hands[0]['lmList']
x1, y1 = lmList[5]
x2, y2 = lmList[17]
distance = int(math.sqrt((x1-x2)**2 + (y1-y2)**2))

cv2.imshow("webcam", img)