0

I have some python code that uses opencv to play a video from a certain path and I've been reading about how to incorporate that python code with Django and I saw that the python code can be put into the django views.py file but my question is what am I supposed to put as a parameter for the piece of code that renders it, like return render(request, [what do I put here?]) because usually after request the location of the html file is put but if I want that video to play do I just specify the html page I want the video to play on, will that work or do I have to do something more? Also if you know any good tutorials that deal with this type of stuff I would appreciate any links. Thanks in advance.

Here's the python code that just plays a video

 filename = 'C:/Desktop/Videos/traffic2.mp4'
        vidcap = cv2.VideoCapture(filename)

        while(vidcap.isOpened()):
            success, frame_org = vidcap.read()

            cv2.imshow('frame',frame_org)  

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        vidcap.release()
        cv2.destroyAllWindows()
user3354383
  • 47
  • 1
  • 1
  • 9
  • Are you asking how to play a video in the client's web browser or do you want a video window to open and play on the server every time a request is made to a certain URL? – Simon Andrews Jul 20 '16 at 18:49
  • I want a video window to open and play on the server every time a request is made to a certain URL which would be when a button is pressed on the html page – user3354383 Jul 20 '16 at 18:55

1 Answers1

1

Quick answer: Don't bother with templates and render(), just use an HttpResponse. The video will play, then the response will be returned so it all works out in the end.

from django.http import HttpResponse

def index(request):
    play_video()
    return HttpResponse("OK")

Opinions answer:

So I've actually done something kinda similar to this.

I'd recommend having a main view with the button on it, that when clicked calls a JavaScript function that sends a GET request to another hidden view that actually plays the video on the server.

This hidden view would basically be the code snippet I posted above.

You may also want to consider putting your video playing code in a subprocess because Django or the webbrowser might time out or something.

Community
  • 1
  • 1