Fetching external videos in browser (fetchImage for <video>)
See original GitHub issueI’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:
- Created 4 years ago
- Reactions:4
- Comments:11 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
is this code applicable to face-api.min.js and if so how can I implement it? Thank you!
You did do very good work, Thank you!