Madhav Malik
Madhav Malik

Reputation: 68

How do i access my webcam with python (Django) from the browser with the help of HTML and Javascript?

I am trying to create a web-app and I need to get video input from the webcam into Javascript or HTML and pass it to Python (Django) where I can use OpenCV with each frame.

Upvotes: 4

Views: 5043

Answers (2)

Adamu Abdulkarim Dee
Adamu Abdulkarim Dee

Reputation: 196

To access the camera in your Django template, you'll need to use JavaScript along with HTML to capture the camera feed. Here's how I did it:

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" 
    crossorigin="anonymous">
    
<div class="container">
        <div class="row justify-content-center">
            <div class="col">
                <div class="card">
                    <div class="card-body">
                        <video id="camerafeed" autoplay controls=""></video>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        navigator.mediaDevices.getUserMedia({ video: true })
            .then(function (stream) {
                document.getElementById('camerafeed').srcObject = stream;
            })
            .catch(function (error) {
                console.log('Camera access error:', error);
            });
    </script>

Upvotes: 1

fabrv
fabrv

Reputation: 382

You can use the WebRTC APIs to capture or stream video from the camera. You can check this documentation on it https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API.

And also this WebRTC implementation for Python https://github.com/aiortc/aiortc

I hope the links are of use.

Upvotes: 2

Related Questions