I'm using a windows build of gphoto2 to generate a byte stream. Take the byte stream and look for the jpeg headers (ff d8) and footer (ff d9) and display a single image from the stream. Whenever I pass the parsed byte string into imdecode it returns None. I pass all of the data including the ff d8/ ff d9 into imdecode.
pipe = sp.Popen('gphoto2 --stdout-size --capture-movie', stdout = sp.PIPE)
founda=False
foundb=False
bytesl=b''
while True:
bytesl=bytesl+pipe.stdout.readline()
if ~founda:
a = bytesl.find(b'\xff\xd8') # JPEG start
bytesl = bytesl[a:]
if a!=-1:
founda=True
if founda and ~foundb:
b = bytesl.find(b'\xff\xd9') # JPEG end
if a!=-1 and b!=-1:
foundb=True
if founda and foundb:
jpgi = bytesl[:b+2]
imfbuffh = cv2.imdecode(np.frombuffer(jpgi, dtype=np.uint8),cv2.IMREAD_COLOR)
I keep getting nothing from imdecode and I'm not sure why. The byte string appears to correctly parse the data. Any help would be greatly appreciated.
Edit: Something else I've noticed is if I just read a JPG from a file and I do a np.shape on the object from np.buffer I report something like (140000,1) versus when i do the np.shape when I'm reading it from the byte string I get (140000,). I've tried expanding the dimensions but that didn't work.
Edit2: Well I realized that the header for the mjpeg is not just a standard jpeg header. I'm not sure how to convert it to the standard format. If anyone has any tips that would be great.
Edit3: I simplified the output and write to file code to just read the pipe data.
I have two test cases one where I use --capture-movie 2 and one where I use --capture-image-and-download so that in the first case I capture 2 frames of MJPEG data and another where I capture 1 frame of jpeg data. I tried to display the data for both cases with my previous code and they failed to display the image even if I just wait for the stdout to finish rather than reading the data in real time.
Here is the code I used to just to write the bytes to a byte file. In my previous comment I was just recording the byte string from a print statement (stupid I know I'm not very good at this). Should be noted I think these byte strings need to be decoded.
pipe = sp.Popen('gphoto2 --stdout-size --capture-movie 2', stdout = sp.PIPE)
pipedata=pipe.stdout.read()
f = open('C:\\Users\\Work\\frame2out.txt', 'wb')
f.write(pipedata)
Attached are links to the two cases. 2 Frames from --capture-movie https://www.dropbox.com/s/3wvyg8s1tflzwaa/frame2out.txt?dl=0
Bytes from --capture-image-and-download https://www.dropbox.com/s/3arozhvfz6a77lr/imageout.txt?dl=0