In colab, I want to do a facial expression model on live streaming video. But I don't know how can I do this. I can get a source about video record and the link is here. But I want to work on live video streaming in my project.
In colab, I want to do a facial expression model on live streaming video. But I don't know how can I do this. I can get a source about video record and the link is here. But I want to work on live video streaming in my project.
Unfortunately, colab cannot access your web cam in real time. However, if you have already recorded video, then it is easy.
#!pip install deepface
from deepface import DeepFace
DeepFace.stream(db_path = "C:/facial_database", source = "my_video.mp4")
It will apply face recognition and facial attribute analysis (age, gender and emotion). If you store facial images in the path C:/facial_database, then it will look for the identity here for detected faces.
At this moment Google Colab has included in it's Code snippets section (Insert/Code snippets) the Camera Capture snippet:
Camera Capture
Using a webcam to capture images for processing on the runtime.
from IPython.display import display, Javascript
from google.colab.output import eval_js
from base64 import b64decode
def take_photo(filename='photo.jpg', quality=0.8):
js = Javascript('''
async function takePhoto(quality) {
const div = document.createElement('div');
const capture = document.createElement('button');
capture.textContent = 'Capture';
div.appendChild(capture);
const video = document.createElement('video');
video.style.display = 'block';
const stream = await navigator.mediaDevices.getUserMedia({video: true});
document.body.appendChild(div);
div.appendChild(video);
video.srcObject = stream;
await video.play();
// Resize the output to fit the video element.
google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);
// Wait for Capture to be clicked.
await new Promise((resolve) => capture.onclick = resolve);
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
stream.getVideoTracks()[0].stop();
div.remove();
return canvas.toDataURL('image/jpeg', quality);
}
''')
display(js)
data = eval_js('takePhoto({})'.format(quality))
binary = b64decode(data.split(',')[1])
with open(filename, 'wb') as f:
f.write(binary)
return filename
from IPython.display import Image
try:
filename = take_photo()
print('Saved to {}'.format(filename))
# Show the image which was just taken.
display(Image(filename))
except Exception as err:
# Errors will be thrown if the user does not have a webcam or if they do not
# grant the page permission to access it.
print(str(err))