Replies: 4 comments 4 replies
-
Do I understand correctly, that you want to capture an image of the user's webcam and send it to Python? |
Beta Was this translation helpful? Give feedback.
-
Yako the Developer demostrated on How To Access Webcam In HTML Using JavaScript. The simple and short code works on my laptop, but asks twice for authoration for accessing webcam. <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>camera</title>
<script type="text/javascript" src="https://unpkg.com/webcam-easy/dist/webcam-easy.min.js"></script>
<style>
a {
padding: 10px;
background-color: orange;
color: white;
text-decoration: none;
}
</style>
</head>
<body>
<video id="webCam" autoplay playsinlne width="800" height="600"></video>
<canvas id="canvas"></canvas>
<a download onClick="takeAPciture()">SNAP</a>
<a download onClick="stopCam()">STOP</a>
<script>
const webCamElement = document.getElementById( "webCam");
const canvasElement = document.getElementById("canvas");
const webcam = new Webcam(webCamElement, "user", canvasElement);
webcam.start();
function takeAPciture(){
let pic = webcam.snap();
document.querySelector("a").href = pic;
}
function stopCam(){
webcam.stop();
}
</script>
</body>
</html> then I try to use the code in HTML to embed a live camera view in my NiceGUI application. However the code does not render any camera view. In fact, it does not ask for permission to use camera at all. So how to fix my buggy code? Thanks from nicegui import ui, Client
@ui.page('/')
async def fn(client):
ui.label('hello')
await client.connected()
ui.add_body_html('<script type="text/javascript" src="https://unpkg.com/webcam-easy/dist/webcam-easy.min.js"></script>')
ui.element("video").props(' id="webCam" autoplay playsinlne width="800" height="600" ')
ui.element("canvas").props(' id="canvas" ')
webCamElement = ui.run_javascript('document.getElementById( "webCam");')
canvasElement = ui.run_javascript('document.getElementById("canvas");')
webcam = ui.run_javascript(f'new Webcam({webCamElement}, "user", {canvasElement});')
ui.run_javascript(f'{webcam}.start();')
ui.run() |
Beta Was this translation helpful? Give feedback.
-
Thanks, @falkoschindler, your code works. But the bad news is the script I mentioned above can use webcam on windows( no matter I use edge, firefox or chrome) only. It does not support
I have not tested in Linux yet. It seems too hard to make webcam work in different client webbrowser :( |
Beta Was this translation helpful? Give feedback.
-
There also seem to be quite a few vue solutions which might be possible to adapt similar to our custom vue component example:
Or pure javaScript like this minimal proof-of-concept: @ui.page('/')
async def page():
v = ui.element('video')
await ui.context.client.connected()
ui.run_javascript('''
function compressToJpeg(canvas, quality) {
return new Promise((resolve) => {
canvas.toBlob((blob) => {
const reader = new FileReader();
reader.onloadend = () => {
resolve(reader.result);
};
reader.readAsDataURL(blob);
}, 'image/jpeg', quality);
});
}
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => {
const video = getElement(''' + str(v.id) + ''');
video.srcObject = stream;
video.play();
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
video.addEventListener('play', () => {
setInterval(async () => {
context.drawImage(video, 0, 0, canvas.width, canvas.height);
const jpeg = await compressToJpeg(canvas, 0.5);
emitEvent('frame', jpeg);
}, 1000);
});
})
.catch(err => console.error('Error accessing the camera:', err));
''')
ui.on('frame', lambda f: print('frame', f.args[:100], flush=True)) # data received in python |
Beta Was this translation helpful? Give feedback.
-
Question
Everyone can see the image captured by camera on PC/laptop which runs
nicegui-main\examples\opencv_webcam\main.py
But what if
well, this is maybe a very complex situation, because the user can use windows/linux/Mac OSx/Android/iOS.
thanks
Beta Was this translation helpful? Give feedback.
All reactions