Capturing Video Stream Using OpenCV in Python: A Guide

The simplest method to display video streaming in a tkinter frame is through the PIL library. Are there any specific types of video streams that OpenCV can capture? Is there a solution that doesn’t require a change of scheme or transcoding? Thank you!

Question:

I am interested in utilizing Python’s OpenCV to handle an mms video stream originating from an external source (
IP camera
), specifically a traffic monitor. The stream can be accessed through either mms or mmst schemes.

mms://194.90.203.111/cam2

It is compatible with both VLC and Windows Media Player.

mmst://194.90.203.111/cam2

The functionality is limited to VLC only. Despite my attempts to re-stream with FFmpeg and VLC and alter the scheme to HTTP, the issue persisted.

From my understanding, the stream is encoded using
Windows Media Video
by mms. Unfortunately, appending ‘.mjpeg’ to the URI did not work. I am still searching for the supported streaming formats for OpenCV.

Here’s my code –

import cv2, platform
#import numpy as np
cam = "mms://194.90.203.111/cam2"
#cam = 0 # Use  local webcam.
cap = cv2.VideoCapture(cam)
if not cap:
    print("!!! Failed VideoCapture: invalid parameter!")
while(True):
    # Capture frame-by-frame
    ret, current_frame = cap.read()
    if type(current_frame) == type(None):
        print("!!! Couldn't read frame!")
        break
    # Display the resulting frame
    cv2.imshow('frame',current_frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# release the capture
cap.release()
cv2.destroyAllWindows()

What type of video streams can be captured by OpenCV? Is there a way to solve this without altering the scheme or transcoding? Essentially, what am I missing?

Thanks!

The system comprises Python version 2.7.8 and OpenCV version 2.4.9, both designed for x86 architecture and compatible with Windows 7 operating system running on a 64-bit platform.


Solution:

The issue was resolved by utilizing FFmpeg and FFserver, with the caveat that FFserver is solely compatible with Linux. Ryan provided a recommendation to incorporate Python code from this source, which was implemented in the solution.

Flow is as follows –

  • Begin the desired configuration’s FFserver background process, which in this case is mjpeg.
  • The input for FFmpeg is the mmst stream which is then directed to the localhost as the output stream.
  • Execute the
    python script
    command to initiate the localhost stream and then proceed with decoding each frame individually.

Run FFserver

ffserver -d -f /etc/ffserver.conf

On a second terminal run FFmpeg

ffmpeg -i mmst://194.90.203.111/cam2 http://localhost:8090/cam2.ffm

In this instance, the Python code will initiate a video stream by launching a window.

import cv2, platform
import numpy as np
import urllib
import os
cam2 = "http://localhost:8090/cam2.mjpeg"
stream=urllib.urlopen(cam2)
bytes=''
while True:
    # to read mjpeg frame -
    bytes+=stream.read(1024)
    a = bytes.find('xffxd8')
    b = bytes.find('xffxd9')
    if a!=-1 and b!=-1:
        jpg = bytes[a:b+2]
        bytes= bytes[b+2:]
    frame = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
    # we now have frame stored in frame.
    cv2.imshow('cam2',frame)
    # Press 'q' to quit 
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cv2.destroyAllWindows()

ffserver.config –

Port 8090
BindAddress 0.0.0.0
MaxClients 10
MaxBandWidth 50000
CustomLog -
#NoDaemon

    File /tmp/cam2.ffm
    FileMaxSize 1G
    ACL allow 127.0.0.1
    ACL allow localhost


    Feed cam2.ffm
    Format mpjpeg
    VideoFrameRate 25
    VideoBitRate 10240
    VideoBufferSize 20480
    VideoSize 320x240
    VideoQMin 3
    VideoQMax 31
    NoAudio
    Strict -1


    Format status
    # Only allow local people to get the status
    ACL allow localhost
    ACL allow 192.168.0.0 192.168.255.255


    URL http://www.ffmpeg.org/

Please be aware that the configuration of ffserver could benefit from further refinement, however, it is currently performing quite satisfactorily as it produces a nearly identical frame to the source with only a slight
frame freeze
impact.

Frequently Asked Questions