Reputation: 1259
I'm trying to open an mjpeg stream with VideoCapture in OpenCV2
But whenever I try to read a frame I get the following error thrown:
[mjpeg @ 0x10f4d20] unable to decode APP fields: Invalid data found when processing input
I can watch the stream without issues in the browser. I also tried the typical suggestion of adding a dummy parameter like ?type=.mjpg
but no luck.
This is how I open the stream:
cap = cv2.VideoCapture("http://localhost:8000/camera/mjpeg?type=.mjpg")
while cap.isOpened():
ret, image = cap.read()
if not ret:
break
cv2.imshow("Result", image)
Upvotes: 1
Views: 7076
Reputation: 1
If you are using mjpeg streamer, to access to the stream use
stream = opencv.VideoCapture('http://localhost:8080/?action=stream')
or
stream = opencv.VideoCapture('http://XX.XX.XX.XX:8080/?action=stream')
Upvotes: 0
Reputation:
I am doing exactly this (Python 3.7) and it is working. I have a Raspberry Pi 4 sending out a stream. On the same network, my MacBook is running the following code.
# Open a URL stream
stream = cv2.VideoCapture('http://192.168.50.1:8080/stream.mjpg')
while ( stream.isOpened() ):
# Read a frame from the stream
ret, img = stream.read()
if ret: # ret == True if stream.read() was successful
cv2.imshow('Video Stream Monitor', img)
So I'm not sure what you were doing wrong.
Upvotes: 1
Reputation: 1298
you need to use urllib for reading that
import cv2
import urllib.request
import numpy as np
stream = urllib.request.urlopen('http://localhost:8000/camera/mjpeg?type=.mjpg')
bytes = b''
while True:
bytes += stream.read(1024)
a = bytes.find(b'\xff\xd8') #frame starting
b = bytes.find(b'\xff\xd9') #frame ending
if a != -1 and b != -1:
jpg = bytes[a:b+2]
bytes = bytes[b+2:]
img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.CV_LOAD_IMAGE_COLOR)
cv2.imshow('image', img)
if cv2.waitKey(1) == 27:
cv2.destroyAllWindows()
break
Upvotes: 0