Alex_raspberry
Posts: 10
Joined: Wed Dec 09, 2015 3:05 pm

picamera and Flask - numpy issue

Sat Sep 29, 2018 2:54 pm

Hi,

I am trying to capture image from picamera 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 what I am using:

Code: Select all

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')


The outputs of "data" and "frame" are like below:

Code: Select all

data = np.fromstring(stream.getvalue(), dtype=np.uint8)
output:
[255 216 255 ..., 175 255 217]

and for frame is:

Code: Select all

frame = cv2.imdecode(data, 1)
output:
[[[120 125 104]
[120 125 104]
[120 124 105]
...

any help would be highly appriciated...

Thanks

User avatar
paddyg
Posts: 2182
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: picamera and Flask - numpy issue

Mon Oct 01, 2018 11:51 am

Not really sure what you are intending to yield, or how it is to be consumed... but if you want to concatenate your b'Content...' with a numpy array you might have to convert it to raw bytes with something like tobytes()

Code: Select all

b'Content-Type: image/jpeg\r\n\r\n' + frame.tobytes() + b'\r\n')
It's probably fairly critical whether you are using python2 (I wouldn't) or python3
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

yfpiLiker
Posts: 32
Joined: Sat Mar 24, 2018 7:46 am

Re: picamera and Flask - numpy issue

Thu Oct 11, 2018 6:17 pm

But isn't frame a instance of type Mat?
Not English. But Pythish.

User avatar
paddyg
Posts: 2182
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: picamera and Flask - numpy issue

Fri Oct 12, 2018 9:12 am

I was really just going by the error message the OP quoted
But isn't frame a instance of type Mat?
You may well ask! I find the python opencv docs almost impenetrable; they unhelpfully show

Code: Select all

Python: cv2.imdecode(buf, flags) → retval
If you want to find out what a 'retval' is the only way seems to be to print(type(..)) in which case I think it turns out to be <class 'numpy.ndarray'>

IMHO opencv is a bit of a snare and delusion in situations like this as picamera has an array module that can be used to efficiently stream frames directly to numpy arrays https://picamera.readthedocs.io/en/rele ... pirgbarray. So it should be possible to do something along the lines of https://github.com/pi3d/pi3d_demos/blob ... _2d.py#L16
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

yfpiLiker
Posts: 32
Joined: Sat Mar 24, 2018 7:46 am

Re: picamera and Flask - numpy issue

Sat Oct 13, 2018 5:32 pm

That looks much better documented and easier to use!
Not English. But Pythish.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 20 guests