0

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!!!

  • 3
    You may want to look at `WebSocket` concept for realtime communication. – Harun Yilmaz Aug 04 '20 at 08:43
  • If the updates are not that much, and refresh does not need to be very frequent, you may also create a pooling mechanism where every second/ few seconds, you will lookup to server for update. `Websocket` will be more effecient and fast as you eliminate initiation of request. However, it will be costly for server. – Rajesh Aug 04 '20 at 08:48
  • Python is not a front-end language. It probably doesn't make sense to talk about Python WRT front-end concepts like video players. At first I thought you might be talking about selenium but that doesn't seem to be the case. – pguardiario Aug 04 '20 at 08:53

2 Answers2

0

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..

Dropout
  • 13,653
  • 10
  • 56
  • 109
  • Advantage of this is that you don't need to constantly query your backend for data from the JS side, but it depends if you're able to implement it like this. Basically the Python backend would constantly "broadcast" the data while the JS part is sort of a reciever and just displays it. – Dropout Aug 04 '20 at 09:02
  • 1
    Thank you a lot for your quick help, will check this out!! – JasperVaugnman Aug 04 '20 at 12:48
0

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.

Sarwagya
  • 166
  • 11