I have a python program that tracks the position of a person in a room. This position is saved as a variable ("coordinates"), and constantly updated. Based on these coordinates (x and y) I would like a video player to zoom in and out. So if someone is in the front of the room, the movie clip is zoomed in. When he/she is in the back, the footage is zoomed out.
Now the problem is, I don't really find nice video players in python. I do however see a lot of nice video players in javascript. Now my question is: would it be possible for a javascript file to somehow "ask" the python file what the current value of the variable is? Is there a regular way to do this? If this is not the case I will search for a python video module, but really like the javascript one...
Thanks in advance!!!
2 Answers 2
Personally, the way I know of achieving this is writing to a file or a database from python and make Javascript make an ajax call to a handler python (or another format) script in the server, which would return the values in the file to which the variables have been written.
Although using a video player in python3 would be the best practice. Try looking into pyglet or moviepy. Here is post which focusses on zooming and panning with pyglet.
Also, you can use webSockets (or selenium) to drive the js from python itself. This should help.
Comments
If it's supposed to be vanilla JavaScript, then I'd suggest using sockets and updating the field with an onMessage event. This would mean that the Python side of the program would be "pushing" the information onto the JavaScript side which would only display the data.
let yourSocket = new WebSocket("ws://whatever.source");
const yourField = document.getElementById("your_element_id");
yourSocket.onMessage = function(event) {
yourField.innerHTML = event.data;
}
Substitute whatever.source for your message source (coming from python) and your_element_id with the ID of the element you want to fill with data.
Please refer to https://javascript.info/websocket for the JS part and for the Python side there's https://websockets.readthedocs.io/en/stable/intro.html..
WebSocketconcept for realtime communication.Websocketwill be more effecient and fast as you eliminate initiation of request. However, it will be costly for server.