JohnWhite
JohnWhite

Reputation: 13

Flask - image issue - cannot str and numpy.ndarray

I am trying to capture image from picamera(RaspberryPi) and show it using Flask (on web), but I am facing this issue:

b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

TypeError:cannot concatenate 'str' and 'numpy.ndarray' objects

this is the code:

stream = io.BytesIO()
with picamera.PiCamera() as camera2:
    camera2.start_preview()
    time.sleep(0.1)
    camera2.capture(stream, format='jpeg')

data = np.fromstring(stream.getvalue(), dtype=np.uint8)

frame = cv2.imdecode(data, 1)

yield (b'--frame\r\n'
       b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

Although I have read this Link, but it did not help me: https://blog.miguelgrinberg.com/post/video-streaming-with-flask

outputs:

data = np.fromstring(stream.getvalue(), dtype=np.uint8)

[255 216 255 ..., 175 255 217]

and also

frame = cv2.imdecode(data, 1)

[[[120 125 104] [120 125 104] [120 124 105] ...

any help would be highly appreciated...

Thanks

Upvotes: 1

Views: 508

Answers (1)

Mikhail Cherepanov
Mikhail Cherepanov

Reputation: 56

You have a trouble when you concatenate bytes string b'Content-Type: image/jpeg\r\n\r\n' with numpy array frames. In example as frames used bytes string, which read from jpeg file directly in binary mode. I think you must not do anything with stream.getvalue(). it's return you ready data structure for streaming(jpeg file in bytes representation). so just use it

stream = io.BytesIO()
with picamera.PiCamera() as camera2:
    camera2.start_preview()
    time.sleep(0.1)
    camera2.capture(stream, format='jpeg')

yield (b'--frame\r\n'
   b'Content-Type: image/jpeg\r\n\r\n' + stream.getvalue() + b'\r\n')

Upvotes: 1

Related Questions