0

This might be the very wrong way to do this, I'm looking to learn the correct way.

I have an application which in short replaces the background of a video stream. I'm looking to allow changing what the background is replaced with in real time via a post request. (Eventually i'll probably serve a webpage with some nice buttons or something but for now I just want to get this part working)

The relevant part of the app is structured like this (this pseudo code of the relevant parts)

class Image_Handler:
   def __init__(...):
      ...
      self.background = foo
      ...

   def change_background(self, new_foo, ...):
      ....
      self.background = new_foo
      ...

   def composite_background(self, capture, mask):
      return capture * mask + self.background * (1 - mask)
    
   # Below this point are a few helper functions for loading images
   def load_image():
      ...

class Camera:
   def get_frame():
      ...

   def schedule_frame():
      ...

class bodypix:
   def get_mask():
      ...

At one point I had this working as the code was much more flat (no classes) so I was able to call change background like this in flask.

def change_background(name):
    # Beautiful, I know
    global bg
    bg = name


@app.route('/', methods=['POST'])
def on_request():
    req = request.get_json()
    change_background(req['new_bg'])

The issue I'm running into is that to make this work I need to do a ton of round about horrible looking code to expose the change_background method to a flask app now.

ex:

app = Flask(__name__)
ih = Image_Handler(flask_app=app, ...)

ih.flask_app.add_url_rule('/', ..., ih.change_background)

This screams "This is the wrong way to do this"

My questions are:

  • Is flask even the correct thing to use for this? (This is not production, which seems to make it ok?)
  • How would I call the Image_Handlers change_background method in a way that makes sense?
  • Is the structure of this even correct?

Any resources you could link would be amazing, any thoughts on how you might change the structure of this would also be very welcome. I'm trying to learn the cleanest way to structure this.

Grey
  • 63
  • 1
  • 9

0 Answers0