3

I want to use OpenCV in my Django application. As OpenCV is a library, I thought we can use it like any other library.

When I try to import it using import cv2 in the views of Django, it works fine but when I try to make library function calls in Django view like cap = cv2.VideoCapture(0) and try to run the app on my browser, nothing happens: the template does not load and no traceback in the terminal and the application remains loading forever.

Don't know why but the cv2 function call is not executing as expected. Since there is no traceback, I am not able to understand what is the problem. If anyone can suggest what is wrong ? Is it the right way to use OpenCV with Django ?

Vipul
  • 4,038
  • 9
  • 32
  • 56

2 Answers2

2

Use a separate thread for the cv2 function call and the app should work like a charm. From what I figure..infinite loading is probably because the video never ceases recording and hence the code further up ahead is never taken into account, ergo an infinite loading page. Threads should probably do it. :) :)

Steve
  • 61
  • 8
1

Am I right that you dream about Django application able to capture video from your camera? This will not work (at least not in a way you expect).

Did you check any stack traces left by your web server (the one hosts Django app or the one started as Django built-in)?

I suggest you start playing with OpenCV a bit just from Python command line. If you're on Windows use IDLE. Observe behaviour of your calls from there.

Django application is running inside WSGI application server where there are several constraints what a module of particular type can and cannot do. I didn't try to repeat what you've done (I don't have camera I can access).

Proper way of handling camera in web application requires browser side handling in JavaScript.

Small disclaimer at the end: I'm not saying you cannot use OpenCV at all in Django application, but attempt to access the camera is not a way to go.

Michał Fita
  • 1,183
  • 1
  • 7
  • 24