0

I have install opencv on MacX(Lion) with ffmpeg support

import cv2
import cv
import base64
import time
import urllib2
import numpy as np

from cv2 import __version__

print(__version__)
video="http://xxx.223.91.91/mjpg/1/video.mjpg?dummy=param.avi"
vv = cv.CaptureFromFile(video)
cv2.namedWindow("preview")
vc = cv2.VideoCapture("http://xxx.223.91.91/mjpg/1/video.mjpg?dummy=param.mjpg")

but Show WARNING

2.4.2

WARNING: Couldn't read movie file http://xxx.223.91.91/mjpg/1/video.mjpg?dummy=param.avi

WARNING: Couldn't read movie file http://xxx.223.91.91/mjpg/1/video.mjpg?dummy=param.mjpg

I Google a lot of code, but could not find a solution to the problem. Please help me, thank you.

Sky
  • 71
  • 1
  • 2
  • 10

1 Answers1

4
import cv2
import urllib 
import numpy as np

stream=urllib.urlopen('http://localhost:8080/frame.mjpg')
bytes=''
while True:
    bytes+=stream.read(16384)
    a = bytes.find('\xff\xd8')
    b = bytes.find('\xff\xd9')
    if a!=-1 and b!=-1:
        jpg = bytes[a:b+2]
        bytes= bytes[b+2:]
        i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
        cv2.imshow('i',i)
        if cv2.waitKey(1) ==27:
            exit(0)    
Zaw Lin
  • 5,629
  • 1
  • 23
  • 41
  • Are you saying that OpenCV can not directly open MJPG stream? But I searched on Google code indicates that the code is using OpenCV open directly MJPG stream, but I experiment does not work. – Sky Aug 26 '13 at 04:07
  • It is my experience that a correctly configured OpenCV _can_ directly open an MJPG stream. However, there seems to be some configuration magic, which I don't fully understand. – Martin Haeberli Mar 17 '14 at 19:59
  • 1
    yes, opencv can open an mjpg stream with the following conditions. 1) it is compiled with ffmpeg. 2) url string must end in `mjpg` 3) stream will not start immediately due to max_analyze_duration not set when opencv call into ffmpeg. of course, fixing is trivial by editing the opencv code by enforcing `-f mjpeg` and setting a lower `max_analyze_duration`. but this method avoids the fat dependency and allows finer control over the stream. i have an explanation and a usage example @ http://stackoverflow.com/questions/21702477/how-to-parse-mjpeg-http-stream-from-ip-camera – Zaw Lin Mar 31 '14 at 04:22