2013-05-29 21:46:38 +02:00
|
|
|
(* /*****************************************************************
|
|
|
|
// Delphi-OpenCV Demo
|
|
|
|
// Copyright (C) 2013 Project Delphi-OpenCV
|
|
|
|
// ****************************************************************
|
|
|
|
// Contributor:
|
|
|
|
// laentir Valetov
|
|
|
|
// email:laex@bk.ru
|
|
|
|
// ****************************************************************
|
|
|
|
// You may retrieve the latest version of this file at the GitHub,
|
|
|
|
// located at git://github.com/Laex/Delphi-OpenCV.git
|
|
|
|
// ****************************************************************
|
|
|
|
// The contents of this file are used with permission, subject to
|
|
|
|
// the Mozilla Public License Version 1.1 (the "License"); you may
|
|
|
|
// not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at
|
|
|
|
// http://www.mozilla.org/MPL/MPL-1_1Final.html
|
|
|
|
//
|
|
|
|
// Software distributed under the License is distributed on an
|
|
|
|
// "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
// implied. See the License for the specific language governing
|
|
|
|
// rights and limitations under the License.
|
|
|
|
******************************************************************* *)
|
|
|
|
|
2013-05-31 22:29:25 +02:00
|
|
|
unit uOCVCamera;
|
2013-05-29 21:46:38 +02:00
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils,
|
|
|
|
System.Classes,
|
|
|
|
core.types_c,
|
|
|
|
highgui_c,
|
2013-05-31 22:29:25 +02:00
|
|
|
uOCVTypes;
|
2013-05-29 21:46:38 +02:00
|
|
|
|
|
|
|
type
|
2013-05-31 22:29:25 +02:00
|
|
|
TocvCameraCaptureSource = //
|
2013-05-29 21:46:38 +02:00
|
|
|
(CAP_ANY { = 0 } , // autodetect
|
|
|
|
CAP_MIL { = 100 } , // MIL proprietary drivers
|
|
|
|
CAP_VFW { = 200 } , // platform native
|
|
|
|
CAP_V4L { = 200 } , //
|
|
|
|
CAP_V4L2 { = 200 } , //
|
|
|
|
CAP_FIREWARE { = 300 } , // IEEE 1394 drivers
|
|
|
|
CAP_FIREWIRE { = 300 } , //
|
|
|
|
CAP_IEEE1394 { = 300 } , //
|
|
|
|
CAP_DC1394 { = 300 } , //
|
|
|
|
CAP_CMU1394 { = 300 } , //
|
|
|
|
CAP_STEREO { = 400 } , // TYZX proprietary drivers
|
|
|
|
CAP_TYZX { = 400 } , //
|
|
|
|
TYZX_LEFT { = 400 } , //
|
|
|
|
TYZX_RIGHT { = 401 } , //
|
|
|
|
TYZX_COLOR { = 402 } , //
|
|
|
|
TYZX_Z { = 403 } , //
|
|
|
|
CAP_QT { = 500 } , // QuickTime
|
|
|
|
CAP_UNICAP { = 600 } , // Unicap drivers
|
|
|
|
CAP_DSHOW { = 700 } , // DirectShow (via videoInput)
|
|
|
|
CAP_PVAPI { = 800 } , // PvAPI, Prosilica GigE SDK
|
|
|
|
CAP_OPENNI { = 900 } , // OpenNI (for Kinect)
|
|
|
|
CAP_OPENNI_ASUS { = 910 } , // OpenNI (for Asus Xtion)
|
|
|
|
CAP_ANDROID { = 1000 } , // Android
|
|
|
|
CAP_XIAPI { = 1100 } , // XIMEA Camera API
|
|
|
|
CAP_AVFOUNDATION { = 1200 } );
|
|
|
|
|
|
|
|
type
|
|
|
|
|
2013-05-31 22:29:25 +02:00
|
|
|
TocvOnDataNotify = procedure(const IplImage: pIplImage) of object;
|
2013-05-29 21:46:38 +02:00
|
|
|
|
2013-05-31 22:29:25 +02:00
|
|
|
TocvCameraThread = class(TThread)
|
2013-05-29 21:46:38 +02:00
|
|
|
private
|
2013-05-31 22:29:25 +02:00
|
|
|
FOnNotifyData: TocvOnDataNotify;
|
2013-05-29 21:46:38 +02:00
|
|
|
protected
|
|
|
|
FCapture: pCvCapture;
|
|
|
|
procedure Execute; override;
|
|
|
|
public
|
2013-05-31 22:29:25 +02:00
|
|
|
property OnNotifyData: TocvOnDataNotify Read FOnNotifyData write FOnNotifyData;
|
2013-05-29 21:46:38 +02:00
|
|
|
end;
|
|
|
|
|
2013-05-31 22:29:25 +02:00
|
|
|
TocvCamera = class(TocvDataSource)
|
2013-05-29 21:46:38 +02:00
|
|
|
private
|
|
|
|
FEnabled: Boolean;
|
2013-05-31 22:29:25 +02:00
|
|
|
FCameraCaptureSource: TocvCameraCaptureSource;
|
2013-05-29 21:46:38 +02:00
|
|
|
procedure SetEnabled(const Value: Boolean);
|
2013-05-31 22:29:25 +02:00
|
|
|
procedure SetCameraCaptureSource(const Value: TocvCameraCaptureSource);
|
2013-05-29 21:46:38 +02:00
|
|
|
protected
|
|
|
|
FCapture: pCvCapture;
|
2013-05-31 22:29:25 +02:00
|
|
|
FOpenCVCameraThread: TocvCameraThread;
|
|
|
|
procedure OnNotifyData(const IplImage: pIplImage);
|
2013-05-29 21:46:38 +02:00
|
|
|
public
|
|
|
|
constructor Create(AOwner: TComponent); override;
|
|
|
|
destructor Destroy; override;
|
|
|
|
published
|
|
|
|
property Enabled: Boolean Read FEnabled write SetEnabled default False;
|
2013-05-31 22:29:25 +02:00
|
|
|
property CameraCaptureSource: TocvCameraCaptureSource read FCameraCaptureSource write SetCameraCaptureSource
|
2013-05-29 21:46:38 +02:00
|
|
|
default CAP_ANY;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
const
|
2013-05-31 22:29:25 +02:00
|
|
|
ocvCameraCaptureSource: array [TocvCameraCaptureSource] of Longint = //
|
2013-05-29 21:46:38 +02:00
|
|
|
(CV_CAP_ANY, // autodetect
|
|
|
|
CV_CAP_MIL, // MIL proprietary drivers
|
|
|
|
CV_CAP_VFW, // platform native
|
|
|
|
CV_CAP_V4L, //
|
|
|
|
CV_CAP_V4L2, //
|
|
|
|
CV_CAP_FIREWARE, // IEEE 1394 drivers
|
|
|
|
CV_CAP_FIREWIRE, //
|
|
|
|
CV_CAP_IEEE1394, //
|
|
|
|
CV_CAP_DC1394, //
|
|
|
|
CV_CAP_CMU1394, //
|
|
|
|
CV_CAP_STEREO, // TYZX proprietary drivers
|
|
|
|
CV_CAP_TYZX, //
|
|
|
|
CV_TYZX_LEFT, //
|
|
|
|
CV_TYZX_RIGHT, //
|
|
|
|
CV_TYZX_COLOR, //
|
|
|
|
CV_TYZX_Z, //
|
|
|
|
CV_CAP_QT, // QuickTime
|
|
|
|
CV_CAP_UNICAP, // Unicap drivers
|
|
|
|
CV_CAP_DSHOW, // DirectShow (via videoInput)
|
|
|
|
CV_CAP_PVAPI, // PvAPI; Prosilica GigE SDK
|
|
|
|
CV_CAP_OPENNI, // OpenNI (for Kinect)
|
|
|
|
CV_CAP_OPENNI_ASUS, // OpenNI (for Asus Xtion)
|
|
|
|
CV_CAP_ANDROID, // Android
|
|
|
|
CV_CAP_XIAPI, // XIMEA Camera API
|
|
|
|
CV_CAP_AVFOUNDATION);
|
|
|
|
|
|
|
|
ThreadSleepConst = 20;
|
|
|
|
|
|
|
|
{ TOpenCVCameraThread }
|
|
|
|
|
2013-05-31 22:29:25 +02:00
|
|
|
procedure TocvCameraThread.Execute;
|
2013-05-29 21:46:38 +02:00
|
|
|
Var
|
|
|
|
frame: pIplImage;
|
|
|
|
begin
|
|
|
|
while not Terminated do
|
|
|
|
if Assigned(FCapture) then
|
|
|
|
begin
|
2013-05-31 22:29:25 +02:00
|
|
|
try
|
|
|
|
frame := cvQueryFrame(FCapture);
|
|
|
|
if Assigned(frame) and Assigned(OnNotifyData) then
|
|
|
|
Synchronize(
|
|
|
|
procedure
|
|
|
|
begin
|
|
|
|
OnNotifyData(frame);
|
|
|
|
end)
|
|
|
|
except
|
2013-05-29 21:46:38 +02:00
|
|
|
end;
|
2013-05-31 22:29:25 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
Suspend;
|
2013-05-29 21:46:38 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ TOpenCVCamera }
|
|
|
|
|
2013-05-31 22:29:25 +02:00
|
|
|
constructor TocvCamera.Create(AOwner: TComponent);
|
2013-05-29 21:46:38 +02:00
|
|
|
begin
|
|
|
|
inherited;
|
2013-05-31 22:29:25 +02:00
|
|
|
if not(csDesigning in ComponentState) then
|
|
|
|
begin
|
|
|
|
FOpenCVCameraThread := TocvCameraThread.Create(True);
|
|
|
|
FOpenCVCameraThread.OnNotifyData := OnNotifyData;
|
|
|
|
end;
|
2013-05-29 21:46:38 +02:00
|
|
|
end;
|
|
|
|
|
2013-05-31 22:29:25 +02:00
|
|
|
destructor TocvCamera.Destroy;
|
2013-05-29 21:46:38 +02:00
|
|
|
begin
|
2013-05-31 22:29:25 +02:00
|
|
|
if Assigned(FOpenCVCameraThread) then
|
|
|
|
begin
|
|
|
|
FOpenCVCameraThread.FCapture := nil;
|
|
|
|
FOpenCVCameraThread.Terminate;
|
|
|
|
FOpenCVCameraThread.Resume;
|
|
|
|
FOpenCVCameraThread.WaitFor;
|
|
|
|
FOpenCVCameraThread.Free;
|
|
|
|
end;
|
2013-05-29 21:46:38 +02:00
|
|
|
if Assigned(FCapture) then
|
2013-05-31 22:29:25 +02:00
|
|
|
cvReleaseCapture(FCapture);
|
2013-05-29 21:46:38 +02:00
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2013-05-31 22:29:25 +02:00
|
|
|
procedure TocvCamera.OnNotifyData(const IplImage: pIplImage);
|
2013-05-29 21:46:38 +02:00
|
|
|
begin
|
2013-05-31 22:29:25 +02:00
|
|
|
NotifyRecipients(IplImage);
|
2013-05-29 21:46:38 +02:00
|
|
|
end;
|
|
|
|
|
2013-05-31 22:29:25 +02:00
|
|
|
procedure TocvCamera.SetCameraCaptureSource(const Value: TocvCameraCaptureSource);
|
2013-05-29 21:46:38 +02:00
|
|
|
Var
|
|
|
|
isEnabled: Boolean;
|
|
|
|
begin
|
|
|
|
if FCameraCaptureSource <> Value then
|
|
|
|
begin
|
|
|
|
isEnabled := Enabled;
|
|
|
|
if Assigned(FCapture) and FEnabled then
|
|
|
|
Enabled := False;
|
|
|
|
FCameraCaptureSource := Value;
|
|
|
|
Enabled := isEnabled;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2013-05-31 22:29:25 +02:00
|
|
|
procedure TocvCamera.SetEnabled(const Value: Boolean);
|
2013-05-29 21:46:38 +02:00
|
|
|
begin
|
|
|
|
if FEnabled <> Value then
|
|
|
|
begin
|
|
|
|
if not(csDesigning in ComponentState) then
|
|
|
|
begin
|
2013-05-31 22:29:25 +02:00
|
|
|
|
2013-05-29 21:46:38 +02:00
|
|
|
if Assigned(FCapture) and FEnabled then
|
|
|
|
begin
|
|
|
|
FOpenCVCameraThread.Suspend;
|
|
|
|
FOpenCVCameraThread.FCapture := nil;
|
|
|
|
cvReleaseCapture(FCapture);
|
|
|
|
FCapture := Nil;
|
|
|
|
end;
|
2013-05-31 22:29:25 +02:00
|
|
|
|
2013-05-29 21:46:38 +02:00
|
|
|
if Value then
|
|
|
|
begin
|
2013-05-31 22:29:25 +02:00
|
|
|
FCapture := cvCreateCameraCapture(ocvCameraCaptureSource[FCameraCaptureSource]);
|
2013-05-29 21:46:38 +02:00
|
|
|
FOpenCVCameraThread.FCapture := FCapture;
|
|
|
|
FOpenCVCameraThread.Resume;
|
|
|
|
end;
|
2013-05-31 22:29:25 +02:00
|
|
|
|
2013-05-29 21:46:38 +02:00
|
|
|
end;
|
|
|
|
FEnabled := Value;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|