How to measure the latency of a WebCam with OpenCV – Hacker Noon

In this quick tutorial, you will learn how to measure the latency of your webcam to make the assessment whether it is capable to handle image capturing task with a higher real-time demand compared to doing a still image capturing.

Picture a beverage producing line filling bottles rapidly along a conveyor. A camera is deployed to validate the quality of each bottle like the volume of liquid and shape, every defected product must be sorted out. The real-time requirement for the image capturing is critical since any elongated latency could result in a different bottle being diverted in the wrong path.

Vision-based inspection

Where the latency comes from?

It might seem simple to read a frame from your webcam by calling camera.read() in Python cv2 library, however, lots of stuff happens behind the scene which could introduce the unpredictable latency not desirable the in real-time application.

Camera pipeline

As seen from the above diagram, the captured image traveled a “long” way to reach your computer then it can be decoded and displayed on the screen.

Measure the latency

To measure the latency between when the actual image is captured and displayed on your screen, we are going to whip up a simple Python script to read the image from the camera as fast as we can and flip the display color as soon as the previous color change is detected at the end of the pipeline.

Measure latency

The previous sequence demonstrates that the latency might be longer than several frames.

Here is the logic diagram for this experiment.

Flowchart

The complete script shown below.

By pointing your camera at the popup window and watch the color flips between black and white several times per second which reflects the latency.

You can point your camera lens as close to the screen as possible, it doesn’t need to have a clear focus, only the average intensity is necessary to make the color flip.

I have tested it on my Logitech C930e USB webcam and here is the result.

Demo

Two image resolutions are tested, and you can see the lower resolution produces slightly lower latency.

Latency distribution 640 x 480

Latency distribution 1280 x 720

The value is measured in milliseconds, with the average around 200ms. Given a conveyor traveling at a constant speed of 1m/s, the bottle has already moved 20cm away.

What is the solution?

If your application demands more predictive and lower latency image capturing, one simple and effective solution is to upgrade your camera to an industrial camera with external trigger signal where a photo eye sensor or digital controlled IO can be hooked up.

External trigger

In this case, an image can be captured at a more predictive timestamp either triggered by something blocking the photo eye sensor or a real-time signal from the computer.

Share on Twitter Share on Facebook

read original article here