Video KYC — Part I

Capturing a video stream from a webcam

Elina Maliarsky
5 min readJun 19, 2022

“Know Your Customer” or KYC refers to the process of verifying the identity of the customers either before or during the start of the business relationship based on documentary evidence from an authoritative source. Video-based KYC means that users can complete remote KYC from anywhere via a video call. The process starts when the customer is displaying their face in front of the smartphone’s camera or a webcam on the computer, while the system performs identity checks that include extracting facial biometrics, doing a liveliness check, authenticating the person is a person and simultaneously authenticating the documents (that are also presented in front of the camera later) to automatically perform and complete the verification KYC requirements.

In this series of posts, I’ll show you how different components of a generic video-KYC system can be implemented. Let’s start from the very beginning — accessing a webcam and capturing the video stream from it.

Captured from “Doctor Who” episode named “Asylum of Daleks”

Capture webcam video stream in Python with OpenCV

Python provides various packages for image and video processing. One of them is OpenCV; it is an open-source computer vision and machine learning software library that was built to provide various functionality for image and video operations. With this library, one can detect and recognize faces, identify objects, track movement, and a lot more. Here we’ll see how to capture and record webcam videos with just a few lines of code.

First, let's import the library:

import cv2

If you haven’t installed it yet, visit this page in order to get instructions on how to do it.

After that, VideoCapture object should be created: it will capture the video from the webcam. The constructor of VideoCapture class gets the device index argument that defines the camera, 0 is the index of a PC’s webcam. If you have more than one camera the second will be named 1, the third with 2, and so on.

cap = cv2.VideoCapture(0)

Video can be considered as a collection of images (frames). The webcam video data is captured frame by frame; the frames, for now, are saved in the list of objects.

frames = []
#set up infinite loop
while True:
#read a frame
result, frame = cap.read()
frames.append(frame)
#This will open an independent window
cv2.imshow(“frame”, frame)

The “q” key closes the webcam.

if cv2.waitKey(1) & 0xFF==ord(‘q’): # quit when ‘q’ is pressed 
break

In the end, we just have to release the camera and close all the related windows.

# close the already opened camera
cap.release()
# Destroy all the windows
cv2.destroyAllWindows()

If you work in Jupyter notebook, just run the cells with the code above. It will open a webcam window that will be closed when “q” key is pressed. All the video frames will be saved in the list called frames. The code below prints the length of this list and displays one of the frames.

print(len(frames))
plt.imshow(frames[100])
Image captured from my webcam

But wait, why do I look like the Corpse Bride?

The reason is I use imshow method of matplotlib in order to show the image in Jupyter notebook and not imshow method of OpenCV. When the image file is read with the OpenCV functions (imread(), cap.read()) the order of colors is BGR (blue, green, red), while in matplotlib the order of colors is assumed to be RGB (red, green, blue). Therefore, if you want to use both the matplotlib and the OpenCV functions, you need to convert BGR and RGB, function cvtColor does the job.

plt.imshow(cv2.cvtColor(frames[100], cv2.COLOR_BGR2RGB))
Image captured from my webcam

How to Capture Video in Google Colab

If you work in Google Colab (or any other remote notebook that runs in your browser), the approach is different: cv2.VideoCapture(0) will not work because the code runs on remote servers that have no physical access to your device. You need to use web APIs to access local hardware like a camera.

Google provided code to capture an image inside Google Colab, you just need to find an appropriate snippet:

Screenshots from Goggle Colab Menu

The code from the snippet uses web APIs to access a webcam and allows to take a single frame from a stream. With a slight modification, the code can be used to capture the whole video stream. The Colab notebook below contains both the original and modified code.

Once we have a video file, we can process it with OpenCV.

import cv2
from matplotlib import pyplot as plt
import numpy as np
frames = []
result= True
cap = cv2.VideoCapture(filename)
while result:
result, frame = cap.read()
frames.append(frame)
cap.release()

Please pay attention to cv2_imshow function: this specific one is provided by Google: the original openCV’s function crushes in Google Colab.

from google.colab.patches import cv2_imshow
cv2_imshow(frames[10])

Summary

In this post, we learned how to capture video from a webcam in Python with OpenCV. In addition, we’ve seen some tricks that allow working with OpenCV in Jupiter notebook and Google Colab (or any other remote) notebook. The next post in the series will be about face detection.

Actimize

NICE Actimize leverages machine learning and AI to detect and prevent financial crimes across the financial services industry, including some of the largest global FIs. Our AI and analytics teams create models to detect anomalous activities associated with AML, Fraud, and market abuse.

The NICE Actimize KYC/CDD solution uses the latest technological innovations to provide complete customer lifecycle risk coverage — accounting for customer onboarding, ongoing due diligence, and enhanced due diligence (EDD) processes.

--

--