question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fetching external videos in browser (fetchImage for <video>)

See original GitHub issue

I’ve ran into this issue for a couple hours and I ended up editing the dist library adding two new functions called fetchVideo and bufferToVideo that works pretty much like the fetchImage and bufferToImage functions.

I’ll leave it here to help somebody else with the same issue and in case someone wants to include it on future releases.

face-api.js

...
exports.fetchVideo = fetchVideo;
...
function fetchVideo(uri) {
        return __awaiter(this, void 0, void 0, function () {
            var res, blob;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, fetchOrThrow(uri)];
                    case 1:
                        res = _a.sent();
                        return [4 /*yield*/, (res).blob()];
                    case 2:
                        blob = _a.sent();
                        if (!blob.type.startsWith('video/')) {
                            throw new Error("fetchVideo - expected blob type to be of type video/*, instead have: " + blob.type + ", for url: " + res.url);
                        }
                        return [2 /*return*/, bufferToVideo(blob)];
                }
            });
        });
    }

function bufferToVideo(buf) {
        return new Promise(function (resolve, reject) {
            if (!(buf instanceof Blob)) {
                return reject('bufferToVideo - expected buf to be of type: Blob');
            }
            var reader = new FileReader();
            reader.onload = function () {
                if (typeof reader.result !== 'string') {
                    return reject('bufferToVideo - expected reader.result to be a string, in onload');
                }
                var video = env.getEnv().createVideoElement();
                video.onloadstart = function () {
                    setTimeout(() => {
                        return resolve(video);
                    }, 100)
                };
                video.onerror = reject;
                video.type = "video/mp4";
                video.autoplay = true;
                video.src = reader.result;
            };
            reader.onerror = reject;
            reader.readAsDataURL(buf);
        });
    }

Usage example:

const videoElement = document.querySelector('video');
    const detections = await faceapi
      .detectAllFaces(await faceapi.fetchVideo(videoElement.src))
      .withFaceLandmarks()
      .withFaceDescriptors();

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Bea98commented, Oct 28, 2019

is this code applicable to face-api.min.js and if so how can I implement it? Thank you!

1reaction
devofficercommented, Nov 2, 2020

You did do very good work, Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deliver remote media files - Cloudinary
Retrieve remote media files from any URL, apply transformations, and then deliver the automatically optimized images and videos via CDN.
Read more >
Flutter: Fetch Image from the Back-End through Rest API
Show your support and subscribe to the channel -: https://devstack.page.link/eNh4In this video -:1) We will work on the Profile Screen2) ...
Read more >
Pre-fetching / caching videos for later playing - Stack Overflow
Specify the preload attribute in the <video> element to indicate that the browser should start downloading the file as soon as the page ......
Read more >
Display images from the internet - Flutter documentation
Displaying images is fundamental for most mobile apps. Flutter provides the Image widget to display different types of images. To work with images...
Read more >
Images - React Native
A caveat is that videos must use absolute positioning instead of ... Sometimes, you might be getting encoded image data from a REST...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found