Skip to main content

WebCamera Stream

⏱️ 2 min read
Kirill Vasin

In this example we will stream the data from the connected web-camera or any other camera-like device and perform real-time image processing

How can I take a photo in WL?

info

To get the best result make sure wljs-manupulate module is at least v1.2.8 or newer

Download original notebook
CurrentImage[]
(*VB[*)(FrontEndRef["00464a90-c955-4c55-8dfe-5f22dfb89cbd"])(*,*)(*"1:eJxTTMoPSmNkYGAoZgESHvk5KRCeEJBwK8rPK3HNS3GtSE0uLUlMykkNVgEKGxiYmJkkWhroJluamuqaJAMJi5S0VF3TNCOjlLQkC8vkpBQAedAV3g=="*)(*]VB*)

However, there is a better way if you need to stream the data from a camera continuously:

cam = DeviceOpen["Camera"]
cam["FrameRate"] = 30;
RefreshRate[cur = DeviceRead[cam], 1/30.0]

This should go without any lag. If your device support 60, change the code accordingly.

note

Refresh will not force Wolfram Kernel to work faster, than it can deliver even if you set 1/100.0 update interval.

Basic data-processing

Let's try binarize our images

Refresh[cur = DeviceRead[cam] // Binarize, 1/30.0]

One could also try applying neural network processing, for instance, to identify faces. There is a built-in function for it in Wolfram Language Standard Library:

cur // FindFaces 
{Rectangle[{190.5`,244.5`},{310.5`,397.5`}]}
HighlightImage[cur, {Green, Opacity[0.3], %}]
(*VB[*)(FrontEndRef["d667025f-ba56-4807-ae23-8a12a3dc386b"])(*,*)(*"1:eJxTTMoPSmNkYGAoZgESHvk5KRCeEJBwK8rPK3HNS3GtSE0uLUlMykkNVgEKp5iZmRsYmabpJiWamumaWBiY6yamGhnrWiQaGiUapyQbW5glAQB9axVt"*)(*]VB*)

Let's apply this in real-time:

Make sure to stop other widgets by cleaning the output cells

Refresh[HighlightImage[cur = DeviceRead[cam], With[{
  faces = FindFaces[cur]
}, 
  {Opacity[0.3], Green, If[Length[faces] > 0, First[faces], Rectangle[{0,0}, {0,0}]]}
]], 1/30.0]