0

I have the goal of changing the file object (video uploaded by user) to bytes and then chunking it and then changing these chunks again to images or frames. the following code is a snippet from a django app.

def handle_uploaded_file(f):
    with open('./chunks_made.txt', 'wb+') as destination:
        for chunk in f.chunks():
            print(type(chunk))
            print(len(chunk))
            destination.write(chunk)
            chunk_length = len(chunk)
    read_batches(len(chunk))

def read_batches(chunk_size):
    with open('./chunks_made.txt', 'rb') as file:
        content = file.read(chunk_size)
        frame = cv2.imdecode(content, cv2.IMREAD_COLOR)
        plt.imshow(frame)
        plt.show()

The process view which calls these functions:

def process(request):
    
    video_file = request.FILES['video']
    handle_uploaded_file(video_file)
    data = 'some_data'
    return render(request, 'video/result.html', {'data':video_file})

I don't know how to decode the bytes into the frames as a real image.

RZAMarefat
  • 133
  • 8
  • `cv2.imdecode()` gives you `numpy.array` with `image` but you have to use `return` to send it back to `process`. And if you want it as `PNG` or `JPG` then you will have to convert `numpy.array` to file data - ie. `cv2.imencode(frame, ".png")`. And if you want it display in browser then you can save it in file and send ulr to this file. Or you can send it converted to `base64` and use `` – furas Apr 21 '21 at 22:29
  • today was similar question but in `Flask` [Send and receive back image by POST method in Python Flask](https://stackoverflow.com/questions/67196395/send-and-receive-back-image-by-post-method-in-python-flask/) – furas Apr 21 '21 at 22:31
  • I don't know what you want to do. And I don't know what you means `real image` - if you mean raw data (pixel by pixel) which you can change (resize, rotate, use in Machine Learning) then you have single frame with `frame = cv2.imdecode(...)`. If you mean MOV/MP4/AVI video then you have it in file `/chunks_made.txt`. If you mean single frame as PNG/JPG image (which you can send to browser to display it) then you will have to convert `frame` using `cv2.imencode(frame, ".png")` – furas Apr 21 '21 at 23:03

0 Answers0