Reputation: 21
I wrote some code in Python which utilizes OpenCV (among other packages) to go through a selected video frame by frame, check whether the frame has a person or not (see here), and then return all the frames where a person was found.
The goal (for now) is to dynamically display these images in a web browser. I used this answer for guidance: How to stream an HttpResponse with Django
My code is below:
def stream_response(request):
return StreamingHttpResponse(detect())
def detect():
frame_count=0
while(frame_count < num_frames):
ret, frame = capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
image_pil=Image.fromarray(gray)
response = HttpResponse(content_type="image/jpeg")
image_pil.save(response, "JPEG")
yield response
yield ""*1024
time.sleep(1)
frame_count+=1
However, when I use the StreamingHTTPResponse feature in Django to yield a response (a method to implement a generator), the response I see in the browser is gibberish (see image below).
When I use HTTPResponse to return a response, however, the image displays fine in the browser when directed to the appropriate port.
I've tried various tests, and still can't figure it out. My theory is that it might be due to the browser being unable to render the image in the given time, but I'm not sure...
Update: Ended up yielding HTML directly from Python for the prototype app. That did the trick for current purposes, but will re-explore this down the line.
Upvotes: 1
Views: 642
Reputation: 5184
You need distributed task queue system like Celery. You can make your application in the way that
You main page (from where execution flow starts)
Ask the user to upload a video
Save this video file on server
Add an entry against video in the database
On the success page
In the status web service
Get task id using video id
Check task status
Get images from database using video id (images that your asynchronous task is putting in the database in the background)
Return the status (whether completed or in progress) and list of images
Upvotes: 2