Comments (5)
[Solved] -- Working code example at the end --
Hello everyone,
after I experienced exactly the same bug with Safari in iOS and got stuck for days as how I get it running on an iPhone, here is the solution:
-
Problem: Webcam Canvas freezes after one frame or stays black.
-
Solution tested on an iPhone 11 Pro, iPhone 8, iPad Pro 2, macOS Safari, Chrome, Firefox, Opera
-
Cause: The camera video stream generated in JavaScript is not added to the HTML DOM.
Or rather: It is added too late. For users who experience the same problem in a different application, here is a short explanation of the approach - the camera stream for iOS Safari must be set up this way:
(it will run on all other devices and operating systems as well, this is not a compromise, but only an extension of the current approach)
- initialize video stream object in JS:
let webcam = new tmImage.webcam(1800, 800, flip);
- add video object to the DOM:
document.getElementById('webcam-container').appendChild(webcam.webcam);
- set video attributes "muted" and "playsinline" (otherwise the autoplay will be stopped by iOS)
- start webcam video:
webcam.play();
In the example snippet you can copy on the TM-Page:
First the video is started and then the video object is added to the DOM. Also the two attributes are missing. Therefore Safari automatically blocks the video stream and blocks the "autoplay" of the stream. Result: Video is frozen or stays black.
Working code/ adaptation of the code snippet:
Since I've been looking for a solution for days and hate to read long explanations, tinker the solution together myself and then get error messages again - here the adapted version of the TM code snippet that makes the camera stream work:
Replace/adapt the init function with this:
async function init() {
const modelURL = URL + 'model.json';
const metadataURL = URL + 'metadata.json';
// load the model and metadata
// Refer to tmImage.loadFromFiles() in the API to support files from a file picker
// or files from your local hard drive
model = await tmImage.load(modelURL, metadataURL);
maxPredictions = model.getTotalClasses();
// convenience function to setup a webcam
const flip = true; // whether to flip the webcam
webcam = new tmImage.Webcam(1800, 800, flip); // width, height, flip
await webcam.setup({ facingMode: "environment" }); // use "user" to use front-cam on mobile phones
// append elements to the DOM --> **before starting the webcam**
// document.getElementById('webcam-container').appendChild(webcam.canvas); // just in case you want to use specifically the canvas
document.getElementById('webcam-container').appendChild(webcam.webcam); // webcam object needs to be added in any case to make this work on iOS
// grab video-object in any way you want and set the attributes --> **"muted" and "playsinline"**
let wc = document.getElementsByTagName('video')[0];
wc.setAttribute("playsinline", true); // written with "setAttribute" bc. iOS buggs otherwise :-)
wc.muted = "true"
wc.id = "webcamVideo";
// only now start the webcam --> **after video-object added to DOM and attributes are set**
webcam.play();
window.requestAnimationFrame(loop); // update canvas by loop-function
}
All right you beautiful people out there - I hope I could help you! This approach has been working for me personally beautifully since intensive testing! Let me know if you have any questions!
I wish you a great day - best greetings from Germany!
Oh: @HalfdanJ or @irealva - if the solution works for others as well - maybe you can change the code in the Github snippet and on the website to make it work out-of-the-box? Thanks! :-)
from teachablemachine-community.
Hi @ashique12009 can you provide a bit more detail?
- What browser are you using on iPhone? Can you try the Chrome browser for iOS?
- Are you using the code snippet we provided? Can you include your code?
- Which model are you running (image, pose)?
- Can you open the model using the directly link to the model you get once you export it? (this link is the one at https://teachablemachine.withgoogle.com/models/YOUR_MODEL_ID)
from teachablemachine-community.
[Solved] -- Working code example at the end --
Hello everyone,
after I experienced exactly the same bug with Safari in iOS and got stuck for days as how I get it running on an iPhone, here is the solution:
- Problem: Webcam Canvas freezes after one frame or stays black.
- Solution tested on an iPhone 11 Pro, iPhone 8, iPad Pro 2, macOS Safari, Chrome, Firefox, Opera
- Cause: The camera video stream generated in JavaScript is not added to the HTML DOM.
Or rather: It is added too late. For users who experience the same problem in a different application, here is a short explanation of the approach - the camera stream for iOS Safari must be set up this way:
(it will run on all other devices and operating systems as well, this is not a compromise, but only an extension of the current approach)
- initialize video stream object in JS:
let webcam = new tmImage.webcam(1800, 800, flip);
- add video object to the DOM:
document.getElementById('webcam-container').appendChild(webcam.webcam);
- set video attributes "muted" and "playsinline" (otherwise the autoplay will be stopped by iOS)
- start webcam video:
webcam.play();
In the example snippet you can copy on the TM-Page:
First the video is started and then the video object is added to the DOM. Also the two attributes are missing. Therefore Safari automatically blocks the video stream and blocks the "autoplay" of the stream. Result: Video is frozen or stays black.Working code/ adaptation of the code snippet:
Since I've been looking for a solution for days and hate to read long explanations, tinker the solution together myself and then get error messages again - here the adapted version of the TM code snippet that makes the camera stream work:
Replace/adapt the init function with this:
async function init() { const modelURL = URL + 'model.json'; const metadataURL = URL + 'metadata.json'; // load the model and metadata // Refer to tmImage.loadFromFiles() in the API to support files from a file picker // or files from your local hard drive model = await tmImage.load(modelURL, metadataURL); maxPredictions = model.getTotalClasses(); // convenience function to setup a webcam const flip = true; // whether to flip the webcam webcam = new tmImage.Webcam(1800, 800, flip); // width, height, flip await webcam.setup({ facingMode: "environment" }); // use "user" to use front-cam on mobile phones // append elements to the DOM --> **before starting the webcam** // document.getElementById('webcam-container').appendChild(webcam.canvas); // just in case you want to use specifically the canvas document.getElementById('webcam-container').appendChild(webcam.webcam); // webcam object needs to be added in any case to make this work on iOS // grab video-object in any way you want and set the attributes --> **"muted" and "playsinline"** let wc = document.getElementsByTagName('video')[0]; wc.setAttribute("playsinline", true); // written with "setAttribute" bc. iOS buggs otherwise :-) wc.muted = "true" wc.id = "webcamVideo"; // only now start the webcam --> **after video-object added to DOM and attributes are set** webcam.play(); window.requestAnimationFrame(loop); // update canvas by loop-function }
All right you beautiful people out there - I hope I could help you! This approach has been working for me personally beautifully since intensive testing! Let me know if you have any questions!
I wish you a great day - best greetings from Germany!Oh: @HalfdanJ or @irealva - if the solution works for others as well - maybe you can change the code in the Github snippet and on the website to make it work out-of-the-box? Thanks! :-)
[Solved] -- Working code example at the end --
Hello everyone,
after I experienced exactly the same bug with Safari in iOS and got stuck for days as how I get it running on an iPhone, here is the solution:
- Problem: Webcam Canvas freezes after one frame or stays black.
- Solution tested on an iPhone 11 Pro, iPhone 8, iPad Pro 2, macOS Safari, Chrome, Firefox, Opera
- Cause: The camera video stream generated in JavaScript is not added to the HTML DOM.
Or rather: It is added too late. For users who experience the same problem in a different application, here is a short explanation of the approach - the camera stream for iOS Safari must be set up this way:
(it will run on all other devices and operating systems as well, this is not a compromise, but only an extension of the current approach)
- initialize video stream object in JS:
let webcam = new tmImage.webcam(1800, 800, flip);
- add video object to the DOM:
document.getElementById('webcam-container').appendChild(webcam.webcam);
- set video attributes "muted" and "playsinline" (otherwise the autoplay will be stopped by iOS)
- start webcam video:
webcam.play();
In the example snippet you can copy on the TM-Page:
First the video is started and then the video object is added to the DOM. Also the two attributes are missing. Therefore Safari automatically blocks the video stream and blocks the "autoplay" of the stream. Result: Video is frozen or stays black.Working code/ adaptation of the code snippet:
Since I've been looking for a solution for days and hate to read long explanations, tinker the solution together myself and then get error messages again - here the adapted version of the TM code snippet that makes the camera stream work:
Replace/adapt the init function with this:
async function init() { const modelURL = URL + 'model.json'; const metadataURL = URL + 'metadata.json'; // load the model and metadata // Refer to tmImage.loadFromFiles() in the API to support files from a file picker // or files from your local hard drive model = await tmImage.load(modelURL, metadataURL); maxPredictions = model.getTotalClasses(); // convenience function to setup a webcam const flip = true; // whether to flip the webcam webcam = new tmImage.Webcam(1800, 800, flip); // width, height, flip await webcam.setup({ facingMode: "environment" }); // use "user" to use front-cam on mobile phones // append elements to the DOM --> **before starting the webcam** // document.getElementById('webcam-container').appendChild(webcam.canvas); // just in case you want to use specifically the canvas document.getElementById('webcam-container').appendChild(webcam.webcam); // webcam object needs to be added in any case to make this work on iOS // grab video-object in any way you want and set the attributes --> **"muted" and "playsinline"** let wc = document.getElementsByTagName('video')[0]; wc.setAttribute("playsinline", true); // written with "setAttribute" bc. iOS buggs otherwise :-) wc.muted = "true" wc.id = "webcamVideo"; // only now start the webcam --> **after video-object added to DOM and attributes are set** webcam.play(); window.requestAnimationFrame(loop); // update canvas by loop-function }
All right you beautiful people out there - I hope I could help you! This approach has been working for me personally beautifully since intensive testing! Let me know if you have any questions!
I wish you a great day - best greetings from Germany!Oh: @HalfdanJ or @irealva - if the solution works for others as well - maybe you can change the code in the Github snippet and on the website to make it work out-of-the-box? Thanks! :-)
@derm1ch1 Thx for your solution. Now,i can use this demo for my iphone safari. But i still cant run it with chrome.
The error message is
I dont know why chrome can not use WebGL2
My iphone version is 14.2
from teachablemachine-community.
Hey @thedb - that's odd. It works perfect for me in chrome.
Can you give me some more information about your setup/your browser version and you operating system?
I'm using chrome 87.0.4280.67
on macOS 10.15.6
.
But in any case I don't think this has something to do with my suggested Safari-iPhone fix - but glad it helped! :-)
from teachablemachine-community.
@derm1ch1
It's my fault, I didn't describe it clearly
This error is on the chrome of the iphone. And I know the limitation of iphone on webRTC. Thanks for your solution, I have already create pr too : )
from teachablemachine-community.
Related Issues (20)
- Trying to use the audio TensorFlow model on node and can't figure out how
- [BUG]: snippets/markdown/audio/tensorflowjs/p5js.mdAudio and video training without python code
- Is audio model using tflite_runtime-2.5.0 possible?
- [FEATURE REQUEST]: Title parquet processing
- [FEATURE REQUEST]: The ability to learn using the GPU
- Image processing and Compilation info HOT 2
- Cannot use the exported model HOT 2
- [FEATURE REQUEST]: Bounding Box
- [FEATURE REQUEST]: LLM Support
- snippets/markdown/image/edgetpu/python.md Opencv example code doesn't swap color to BGR2RGB
- Teachable Machine Project Help HOT 5
- How to run when there is no network in the local browser? HOT 1
- TeachableMachine.Calculate() always returns "Value cannot be null. (Parameter 's')" using C# .NET API
- [BUG]: TeachableMachine on local machine
- Is there an API that could obtain a trained .tflite model?
- Compilation error: tensorflow/lite/version.h: No such file or directory HOT 1
- linker_script.ld:138 cannot move location counter backwards (from 200532b0 to 2003fc00)
- Unable to build docker image
- [BUG]: 'keras.md' code refers erroneously to downloaded model file
- Error when doing inference in loading my model HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from teachablemachine-community.