0

I have created a django web app and written code to get video feed from my laptop camera using opencv.

def start(request):
    print("HI")
    def testDevice():
        cap = cv2.VideoCapture(0)
        if cap is None or not cap.isOpened():
            print('Warning: unable to open video source: ', source)

    testDevice(0) # no printout
    testDevice(1) # prints message
    global video_thread
    if video_thread is None or not video_thread.is_alive():
        stop_event.clear()
        video_thread = threading.Thread(target=run_video_feed)
        video_thread.start()
        return HttpResponse("Video feed started successfully.")
    else:
        return HttpResponse("Video feed is already running.")

The above code is fetching video feed while running in localhost. But when I uploaded(hosted) it in the pythonanywhere, it is not fetching the video feed and it prints Warning: unable to open video source.

  • Oh yes you open a camera... the webcam of the server machine... Most servers don't tend to have cameras. – Willem Van Onsem Mar 19 '23 at 17:55
  • Yes....I thought it would request for my laptop camera access. Is there any way to capture my laptop camera feed on a hosted web app ? – GOPI KRISHNA Mar 19 '23 at 18:08
  • you will need some JavaScript for this: the Python code only runs at the server side. – Willem Van Onsem Mar 19 '23 at 18:12
  • 2
    A bit of context (which is probably obvious to to the OP but might not be for everyone) -- it worked when running locally because the browser and the server were on the same machine, so the server-side code was running on the machine with the camera. When deployed, as @WillemVanOnsem indicated, the server-side code was running on a server. The JavaScript libraries to look into, BTW, are documented here: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia – Giles Thomas Mar 21 '23 at 09:04

1 Answers1

0

I have made an example to solve this by using Django Channels (for asgi support) to set up a websocket stream between client and server.

Note: As I understand, Pythonanywhere only supports wsgi architecture so you may choose another hosting provider if follow this approach.

You can check it out at this repo: https://github.com/snowby666/Django-OpenCV-Video-Streaming

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34833370) – doneforaiur Aug 17 '23 at 08:08