Main Content

IP カメラからストリーミングされた JPEG イメージの表示

この例では、HTTP MultipartConsumer を使用して Web サイトからビデオをストリーミングする方法について説明します。ImageConsumer クラス CameraPlayer をカスタマイズして、IP アドレスから派生する架空の Web サイトから JPEG イメージを表示します。

実際に動作する例を作成するには、次が必要です。

  • 次のような IP アドレスベースの URL

    url = 'http://999.99.99.99/video/mjpg.cgi';
  • 次のような資格情報

    creds = matlab.net.http.Credentials('Scheme','Basic','User','admin','Pass','admin');
    creds.Username = 'yourName';
    creds.Password = 'yourPassword';
    

CameraPlayer クラス

IP カメラが、終了することのないマルチパート メッセージの一部として JPEG イメージを送信します。各パートは "image/jpeg" 型です。このメッセージを処理するには、"image/*" 型のパートを CameraPlayer で処理することを指定する MultipartConsumer を作成します。MATLAB® は受信したイメージの各フレームについて CameraPlayer.putData メソッドを呼び出し、このメソッドは自身のスーパークラスを呼び出してデータを変換します。その後、CameraPlayerimshow で Figure ウィンドウを作成してイメージを表示し、後続のイメージも同じウィンドウに表示します。ユーザーがウィンドウを閉じると、putDatastop=true を返し、それにより MATLAB が接続を閉じます。

classdef CameraPlayer < matlab.net.http.io.ImageConsumer
% CameraPlayer Player for IP camera
%   A ContentConsumer that displays image content types in a figure window.  If
%   specified directly in the RequestMessage.send() operation, it assumes the
%   entire contents of the ResponseMessage is a single image.  If the response
%   is a multipart message, and this is specified as a handler for image
%   types to a GenericConsumer or MultipartConsumer, then this assumes each
%   part is a frame of a video stream and displays them as they are received.
%
%   CameraPlayer properties:
%     CameraPlayer - constructor
%     Image        - the currently displayed Image object
%  
%   The following displays a received image, or a sequence of images received
%   in a multipart message and saves the last image received in the response
%   message Data.  The last image displayed remains visible until cp is
%   deleted.
% 
%     req = RequestMessage;
%     cp = CameraPlayer;
%     resp = req.send(url, [], GenericConsumer('image/*', cp));
%     image = cp.Image;
%     ...operate on image data...

% Copyright 2017 The MathWorks, Inc.

    properties
        % Image - the currently displayed Image object
        %   This image is in an Axes in a Figure.  It is deleted when this object is
        %   deleted.
        Image
        % Enough - control number of times to display message
        Enough
    end
    
    methods
        function obj = CameraPlayer()
            obj = obj@matlab.net.http.io.ImageConsumer();
        end
        
        function [len, stop] = putData(obj, data)
        % putData - called by MATLAB or parent Consumer to process image data
        
            % Pass the data to ImageConsumer, which buffers it until the end of data
            % and then converts it to a MATLAB image depending on the type of image.
            [len, stop] = obj.putData@matlab.net.http.io.ImageConsumer(data);
            if isempty(data)
                % end of image; display the result in Response.Body.Data
                imdata = obj.Response.Body.Data;
                if iscell(imdata) 
                    if ~isempty(imdata{2})
                        % If it was an indexed image if we get a cell array, so convert
                        imdata = ind2rgb(imdata{1},imdata{2});
                    else
                        imdata = imdata{1};
                    end
                end
                if isempty(obj.Image)
                    % First time we are called, create a figure containing the image
                    obj.Image = imshow(imdata);
                    obj.Enough = 0;
                else
                    % Subsequent times we are called, just change CData in an already-displayed
                    % image.

                    obj.Enough = obj.Enough+1;
                    if obj.Enough > 100 
                        disp 'To stop, close figure window.'
                        obj.Enough = 0;
                    end
                    try
                        obj.Image.CData = imdata;
                    catch e
                        % Fails if window closed or data was bad
                        if strcmp(e.identifier, 'MATLAB:class:InvalidHandle')
                            % User must have closed the window; terminate silently
                            stop = true;
                            disp 'Figure window closed.'
                            return
                        end
                    end
                end
                drawnow
            end
        end
        
        function delete(obj)
            delete(obj.Image);
        end
    end
end

CameraPlayer の呼び出し

次のコードは、イメージを取得するためのフレームワークを提供します。コードを実行するには、<> 文字に囲まれた内容の値を指定しなければなりません。Web サービスの URL には、ログイン情報やその他の情報を名前と値のペア引数で指定する追加パラメーターが含まれる場合があります。CameraPlayer を活用するには、これを send の呼び出しに追加します。要求メッセージ作成の詳細については、HTTP を使用した MATLAB からの Web サービスの呼び出しを参照してください。

url = '<YOUR_URL_CONTAINING_IP_ADDRESS>';
cp = CameraPlayer;
consumer = matlab.net.http.io.MultipartConsumer('image/*',cp);
creds = matlab.net.http.Credentials('Scheme','Basic','User','admin','Pass','admin');
creds.Username = '<YOURNAME>';
creds.Password = '<YOURPASSWORD>';
opts = matlab.net.http.HTTPOptions('Cred',creds,'SavePayload',true);
r = matlab.net.http.RequestMessage();
resp = r.send(url, opts, consumer);