close
The Wayback Machine - https://web.archive.org/web/20220709022056/https://github.com/justadudewhohacks/face-api.js/issues/392
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pt-br opened this issue Aug 24, 2019 · 11 comments
Open

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

pt-br opened this issue Aug 24, 2019 · 11 comments
Labels
enhancement good first issue help wanted

Comments

@pt-br
Copy link

@pt-br pt-br commented Aug 24, 2019

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();
@pt-br
Copy link
Author

@pt-br pt-br commented Aug 24, 2019

@justadudewhohacks Feel free to close this issue if you want (idk if it should stay open or not)

@justadudewhohacks
Copy link
Owner

@justadudewhohacks justadudewhohacks commented Sep 13, 2019

Thanks for your solution, if you want to open a PR contributing this method feel free :)

@justadudewhohacks justadudewhohacks added enhancement good first issue help wanted labels Sep 13, 2019
@Bea98
Copy link

@Bea98 Bea98 commented Oct 28, 2019

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

@rajkishoreandia
Copy link

@rajkishoreandia rajkishoreandia commented Jan 31, 2020

@pt-br could you please tell me where to place the code properly.if i write it as stand alone methods.i am getting following error in react.Failed to compile.
I changed the face-api.js still getting the below error
Attempted import error: 'fetchVideo' is not exported from 'face-api.js' (imported as 'faceapi').

@msvargas
Copy link

@msvargas msvargas commented Jan 31, 2020

Please add this feature, thanks!

@SathishSaminathan
Copy link

@SathishSaminathan SathishSaminathan commented Feb 12, 2020

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

have u found the solution bro?

@pt-br
Copy link
Author

@pt-br pt-br commented Jun 19, 2020

@Bea98 @rajkishoreandia you guys need to apply this to the dist file directly, not the min. Then you can compress it if you need a .min version.

@devofficer
Copy link

@devofficer devofficer commented Nov 2, 2020

You did do very good work, Thank you!

@bettysteger
Copy link

@bettysteger bettysteger commented May 21, 2021

@pt-br how can you get results by frame or second?

@Ntambwe1
Copy link

@Ntambwe1 Ntambwe1 commented Jan 21, 2022

hello guys, pls can u explain to step by step how to apply and run this code on python app ??,
pls guys , i am a new baby in python coding pls guide me, no harsh comments pls.

@bettysteger
Copy link

@bettysteger bettysteger commented Jan 21, 2022

@Ntambwe1 hey, this is a JS plugin, so i don't know if you can run it in python?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement good first issue help wanted
Projects
None yet
Development

No branches or pull requests

9 participants