[译]使用Kinect传感器进行自制监控
By robot-v1.0
本文链接 https://www.kyfws.com/applications/homemade-surveillance-using-kinect-sensor-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 8 分钟阅读 - 3819 个词 阅读量 0[译]使用Kinect传感器进行自制监控
原文地址:https://www.codeproject.com/Articles/1235037/Homemade-Surveillance-using-Kinect-Sensor
原文作者:Miguel Diaz Kusztrich
译文由本站 robot-v1.0 翻译
前言
Adding Support for Microsoft Kinect to ThiefWatcher application
向ThiefWatcher应用程序添加对Microsoft Kinect的支持
- 下载KinectProtocol可执行文件-45.2 KB(Download KinectProtocol executable files - 45.2 KB)
- 下载KinectProtocol项目源代码-3.8 MB(Download KinectProtocol project source code - 3.8 MB)
介绍(Introduction)
几个月前,我在该网站上发布了一篇标题为(Some months ago, I posted in this site an article titled) 完善的防盗自制监控系统(A complete anti-theft homemade surveillance system) .如标题所示,它是用于家庭安全的软件,可以使用一个或多个摄像头,入侵检测器触发警报,通信设备来通知警报,以及存储系统可以即时访问由摄像头拍摄的照片.闹钟.(. As the title indicates, it is a software intended to home security, which can use one or more cameras, intruder detectors to trigger the alarm, communication devices to notify alarms, and storage systems to access the photos taken by the cameras in the instant of the alarm.)
该系统是可扩展的,因此您可以轻松地为每种协议编写自己的驱动程序并将其添加到应用程序中,而无需付出任何努力.(The system is extensible, so that you can easily write your own drivers for each of these protocols and add them to the application almost without effort.)
微软(Microsoft)Kinect(Kinect)传感器特别适合此应用.它提供了可以用作监视摄像机的彩色摄像机和红外传感器,以及人体检测系统和深度传感器,可以使用两种不同方法实现存在检测协议以触发警报.(sensor is especially appropriate for this application. It provides a color camera and an infrared sensor, which can be used as surveillance cameras, along with a human body detection system and a depth sensor, which allow implementing the presence detection protocol to trigger the alarm using two different approaches.)
存在不同版本的传感器,以及不同版本的Kinect SDK.本文提供的代码使用SDK的2.0版,该版本可与Kinect Sensor配合使用,(There exist different versions of the sensor, along with different versions of Kinect SDK. The code provided with this article uses the version 2.0 of the SDK, which works with the Kinect Sensor for)Xbox One(Xbox One),但不适用于(, but not for the)Xbox 360(Xbox 360)版本(至少就我而言),因此,如果您想要或需要使用另一个SDK版本,则必须修改项目的一个类,以使其适应这些版本.(version (at least in my case), so that, if you want or need to use another SDK version, you will have to modify one of the classes of the project in order to adapt it to these version.)
您可以下载(You can download the source code of the) KinectProtocol
项目,也可以仅下载必要的可执行文件,然后将归档文件的内容解压缩到(project, or you can download the necessary executable files only, and unzip the content of the archive into the path where the) ThiefWatcher
应用程序已安装.的(application is installed. The) ThiefWatcher
可以从上面引用的文章中下载该应用程序.(application can be downloaded from the article referenced above.)
编译(To compile the) KinectProtocol
项目,您必须添加对(project, you have to add a reference to the) WatcherCommons
在的项目(project in the) ThiefWatcher
主要解决方案.(main solution.)
代码写在(The code is written in)C#(C#)使用(using)视觉工作室(Visual Studio)2015年.(2015.)
安装协议(Installing the Protocols)
一旦(Once the)Microsoft.Kinect.dll(Microsoft.Kinect.dll),(,)**KinectProtocol.dll(KinectProtocol.dll)**文件和(files and the)**es(es)**具有西班牙文资源的目录被复制到(directory, with the Spanish resources, are copied in the)**盗贼守望者(ThiefWatcher)**目录,您只需要使用(directory, you only have to use the)安装协议(Install Protocol)中的选项(option in the)文件(File)菜单选择文件(menu to select the file)**KinectProtocol.dll(KinectProtocol.dll)**和(, and the) KinectCamera and KinectTrigger p
协议将添加到配置文件中.(rotocols will be added to the configuration files.)
由于传感器可以一次提供红外和彩色图像,因此您可以在单个设备上添加每种类型的一个摄像头,从而可以在明亮或黑暗的环境下拍摄图像.要配置Kinect相机,请使用(As the sensor can provide infrared and color images at once, you can add one camera of each type with a single device, so that you can take images both with light or in the dark. To configure a Kinect camera, use the)新相机(New Camera)中的选项(option in the)文件菜单(File menu),然后选择(, and select)Kinect相机(Kinect Camera)在下拉列表中:(in the drop down list:)
将所有数据保留为空白(Leave blank all the data in the)摄像头访问(Camera Access)对话框,因为在这种情况下不需要.(dialog box, as there is no need in this case.)
在(In)相机设定(Camera Settings),您只有两个选项,彩色或红外图像,选择其中一个并关闭对话框.(, you only have two options, color or infrared images, select one of them and close the dialog box.)
最后一步是为摄像机提供名称,然后按保存按钮将其存储在配置文件中.(The last step is to provide a name for the camera and to press the save button to store it in the configuration files.)
您可以使用同一窗口中的播放按钮测试相机.(You can test the camera by using the play button in this same window.)
关于触发协议,它在(Regarding the trigger protocol, it is configured in the)控制面板(control panel):(:)
在下拉列表中,选择(In the drop down list, select)Kinect触发器(Kinect Trigger)作为触发协议.然后,您必须提供一个连接字符串来设置操作模式和设置.(as the trigger protocol. Then, you have to provide a connection string to set up the operation mode and settings.)
在这种情况下,您有两个选择.如果您在家中养宠物,则使用身体检测功能非常理想,因为只会检测到人类的存在.在这种情况下,字符串连接很简单(You have two options in this case. Using the body detection is perfect if you have pets at home, as only the presence of human beings will be detected. The string connection in this case is simply) source=body
.(.)
另一方面,您也可以使用深度传感器检测环境中的各种变化.使用此选项,可以从(On the other hand, you can also use the depth sensor to detect a wide range of changes in the environment. With this option, the data obtained from)Kinect(Kinect)对于图像的每个像素,它们到传感器的距离以毫米为单位.用于检测变化的算法是计算两个连续帧之间的差异,从另一个图像中的对应像素减去一个图像的每个像素.如果距离大于某个阈值,则考虑差异.当差异计数超过图像总像素的给定百分比时,将触发触发器.(are, for each pixel of the image, their distance in millimetres to the sensor. The algorithm used to detect changes is to calculate the difference between two consecutive frames, subtracting each pixel of one image from the corresponding one in the other. If the distance if greater than some threshold, the difference is taken into account. When the count of differences exceeds a given percentage of the total pixels of the image, the trigger is fired.)
在里面(In the) string
连接,您必须指出(connection, you have to indicate) source=depth
,以及阈值((, and the threshold () thres
参数)和差异百分比((parameter) and the percentage of differences () sens
参数),例如:(parameter), for instance:) source=depth;thres=100;sens=15
.(.)
使用代码(Using the Code)
有关以下内容的主要文章介绍了摄像头和触发协议的实现:(The implementation of the camera and trigger protocol is explained in the main article about) ThiefWatcher
应用.因此,让我们专注于(application. So let’s focus on the) Sensor static
类,封装了与Kinect Sensor交互所需的所有代码,并且是您唯一需要修改以使其适合其他Kinect SDK版本的代码.(class, which encapsulates all the code necessary to interact with the Kinect Sensor and which is the only one you have to modify to adapt the code to other Kinect SDK versions.)
的(The) SensorCaps enum
用于指示必须处理的输入类型,以优化工作:(is used to indicate the type of inputs that must be processed, in order to optimize the work:)
[Flags]
public enum SensorCaps
{
None = 0,
Color = 1,
Infrared = 2,
Depth = 4,
Body = 8
}
在里面(In the) Sensor.Caps
属性,则可以指示这些值的适当组合.(property, you can indicate the appropriate combination of these values.)
彩色图像有两个属性,(You have two properties for color images,) Sensor.ColorFrameSize
,它是只读的,它将图像的大小返回为(, which is read-only, which returns the size of the image as a) Size struct
和(, and the) Sensor.ColorFrame
属性,它返回一个(property, which returns a) Bitmap
最后拍摄的图像的对象.(object with the last image captured.)
红外图像具有两个同源属性:(Infrared images are handled with two homologous properties:) Sensor.InfraredFrameSize
和(and) Sensor.InfraredFrame
.(.)
人体检测是通过(The body detection is performed through the) Sensor.BodyTracked
布尔属性,并以(Boolean property, and the depth sensor data is obtained, in the form of an) ushort
数组,从(array, from the) Sensor.DepthFrame
属性.(property.)
传感器通过以下方式投入使用(The sensor is put into operation using the) OpenSensor
方法,该方法接收参数(method, which receives a parameter of) SensorCaps
类型.由于此方法由每个摄像机和触发协议调用,并且只需要启动一次传感器,因此我们将在全局变量中保留已调用该方法的实例的账目(type. As this method is called by each camera and by the trigger protocol and it is only necessary to start the sensor once, we keep an account of instances that have called the method in the global variable) _instances
:(:)
public static void OpenSensor(SensorCaps caps)
{
Caps |= caps;
if (_sensor == null)
{
_instances = 1;
_sensor = KinectSensor.GetDefault();
if (!_sensor.IsOpen)
{
_sensor.Open();
}
Initialize();
_reader = _sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color
| FrameSourceTypes.Depth
| FrameSourceTypes.Infrared
| FrameSourceTypes.Body);
_reader.MultiSourceFrameArrived +=
new EventHandler<MultiSourceFrameArrivedEventArgs>(OnNewFrame);
}
else
{
_instances++;
}
}
全局变量(The global variable) _sensor
,共个(, of) KinectSensor
类型,将(type, will be) null
第一次调用此方法.在这种情况下,我们还创建了一个(the first time this method is called. In this case, we also create a) MultiSourceFrameReader
框架阅读器(frame reader, in the) _reader
变量,它使我们可以配置要同时读取的几种数据类型.在这种情况下,我们将始终读取彩色和红外图像,深度数据和检测到的物体列表.(variable, which allows us to configure several types of data to be read at the same time. In this case, we will always read color and infrared images, depth data and the list of detected bodies.)
阅读它们的方式将是通过(The way to read them will be through the) MultiSourceFrameArrived
事件,每次有新框架可用时都会触发.(event, which will trigger every time a new frame is available.)
停止使用打开实例的方法(The method to stop an instance opened with) OpenSensor
是(is) CloseSensor
,这将减少实例数量,直到达到最后一个实例为止,在这种情况下,系统保持初始状态并释放资源:(, which will reduce the count of instances until it reaches the last one, leaving in this case the system in its initial state and releasing resources:)
public static void CloseSensor()
{
_instances--;
if (_instances <= 0)
{
if (_sensor != null)
{
if (_sensor.IsOpen)
{
_sensor.Close();
_reader.Dispose();
_reader = null;
_sensor = null;
}
}
_instances = 0;
Caps = SensorCaps.None;
}
}
就这样,谢谢您的阅读!!(And that’s all, thanks for reading!!)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# .NET VS2013 Visual-Studio Dev 新闻 翻译