前言
SuperMap iDesktopX 2024基于AI与AR技术,提供的视频分析功能,使得我们具备更强的视频感知能力,因而从视频中提取更为丰富的信息。视频分析模块提供了目标检测、车牌识别、围栏分析、轨迹提取,以及AR空间分析功能,包括缓冲区分析、空间查询、空气质量分析等,可将视频与GIS结合,在视频地图中提取更多的空间信息,并基于视频进行空间分析。以下是相关环境配置以及目标检测、地理围栏的示例代码。
一、视频分析环境配置
1.AI环境配置
在使用视频分析功能之前,需要先下载 SuperMap iDesktopX 的AI扩展包,并进行相关的环境配置,配置好环境后即可激活视频分析中的功能。具体环境配置可以参考:https://help.supermap.com/iDesktopX/zh/SpecialFeatures/ARMap/ARAnalysis/AREnvironmentConfiguration.html
2.开发环境配置
在iDea"Project Structure - Project Setting - Libraries"中引入以下依赖:
●%iDesktopX产品包%/bin/
●%iDesktopX产品包%/bundles/idesktop_bundles/
●%iDesktopX产品包%/bundles/require_bundles/
●%iDesktopX产品包%/lib/
二、视频分析扩展代码
1.加载视频分析环境
代码如下:
//加载OpenCV,用来解析视频
LoadDllManager.loadDlls();
//加载Python环境
PythonUtilities.setPythonExecutor(new PythonDefaultExecutor());
GlobalParameters.setHighPerformanceDetection(true);
GlobalParameters.setSupportStartPythonProcess(false);
VideoAnalystRegister.getInstance().register();
//加载视频分析环境
VideoPlayerPython videoPlayerPython = new VideoPlayerPython();
videoPlayerPython.getVideoAnalystEnvironment();
//打开空间化后的视频数据集
Workspace workspace = new Workspace();
String udbxPath = "F:\\Temp\\Video9SingleDatasetMultiVideo.udbx";
DatasourceConnectionInfo datasourceConnectionInfo = new DatasourceConnectionInfo();
datasourceConnectionInfo.setServer(udbxPath);
datasourceConnectionInfo.setEngineType(EngineType.UDBX);
datasourceConnectionInfo.setAlias(FileUtilities.getFileNameWithoutExtension(udbxPath));
Datasource datasource = workspace.getDatasources().open(datasourceConnectionInfo);
DatasetVector datasetVector = (DatasetVector) datasource.getDatasets().get("video9_1Video");
//遍历视频数据集并提交为视频分析任务
Recordset recordset = datasetVector.getRecordset(false,CursorType.STATIC);
while (!recordset.isEOF()){
VideoManager videoManager = new VideoManager(recordset);
videoManager.build();
recordset.moveNext();
VideoAnalystExecuteEngine.getInstance().addVideoTask(new VideoTask(videoManager));
}
RecordsetUtilities.releaseRecordset(recordset);
2.目标检测
代码如下:
/*
* 目标检测代码
* */
//开启目标检测
videoPlayerPython.getVideoAnalystEnvironment().startAnalyst(VideoAnalystFunctionKey.OBJECTION_DETECTION);
VideoAnalystObjectDetection videoAnalystObjectDetection = (VideoAnalystObjectDetection) videoPlayerPython.getVideoAnalystEnvironment().getVideoAnalyst(VideoAnalystFunctionKey.OBJECTION_DETECTION);
//设置房屋模型
AnalystModel analystModel = AnalystModelManager.getInstance().getAnalystModel("TieTaFangWu");
videoAnalystObjectDetection.getVideoAnalystParameter().setAnalystModel(analystModel);
videoAnalystObjectDetection.getVideoAnalystParameter().getSupportObjectDetection().clear();
//设置目标检测类型和结果绘制风格
for (DetectionType detectionType : analystModel.getDetectionTypes()){
videoAnalystObjectDetection.getVideoAnalystParameter().getSupportObjectDetection().put(detectionType.getStrType(), VideoAnalystUtilities.getObjectDetectionStyleByBackgroundColor(Color.GREEN));
}
3.地理围栏分析
代码如下:
/*
* 地理围栏分析代码
* */
//开启围栏分析
videoPlayerPython.getVideoAnalystEnvironment().startAnalyst(VideoAnalystFunctionKey.VIOLATION);
//获取围栏分析实例
VideoAnalystViolation videoAnalystViolation = (VideoAnalystViolation) videoPlayerPython.getVideoAnalystEnvironment().getVideoAnalyst(VideoAnalystFunctionKey.VIOLATION);
VideoAnalystViolationParameter videoAnalystViolationParameter = videoAnalystViolation.getVideoAnalystParameter();
//设置取证存过滤错误结果
videoAnalystViolationParameter.setViolationIntersectAreaRatio(0.5);
videoAnalystViolationParameter.setViolationOutputFolderPath("F:\\SuperMap\\result");
videoAnalystViolationParameter.setOutputImageTimeInterval(1);
//设置围栏分析规则
DatasetVector selectedDataset = (DatasetVector) datasource.getDatasets().get("GengDi");
videoAnalystViolationParameter.setViolationDatasetVector(selectedDataset);
Geometry geometryByViolationExpression = VideoAnalystUtilities.getGeometryByViolationExpression(selectedDataset,"SmID>0");
SpacialAnalystData spacialAnalystData = new SpacialAnalystData("耕地违建","SmID>0",geometryByViolationExpression,true);
spacialAnalystData.setViolationJudgmentRule(ViolationJudgmentRule.INCLUDED);
videoAnalystViolationParameter.getViolationData().add(spacialAnalystData);
4.获取视频分析结果
代码如下:
/*
* 获取视频分析结果
* */
//添加获取视频分析结果事件
videoPlayerPython.getVideoAnalystEnvironment().addVideoAnalystResultChangedListener(new VideoAnalystResultChangedListener() {
@Override
public void videoAnalystResultChanged(VideoAnalystResultChangedEvent videoAnalystResultChangedEvent) {
//获取视频分析结果
ArrayList<VideoAnalystResult> currentVideoAnalystResults = videoPlayerPython.getVideoAnalystEnvironment().getCurrentVideoAnalystResults();
for (VideoAnalystResult currentVideoAnalystResult:currentVideoAnalystResults) {
pushVideoAnalyst(currentVideoAnalystResult);
}
}
});
//执行分析任务
VideoAnalystExecuteEngine.getInstance().addVideoPlayerPython(videoPlayerPython);
VideoAnalystExecuteEngine.getInstance().run();
三、代码运行结果
完整工程代码以及视频下载链接:https://pan.baidu.com/s/1-8VOwKPWhMap6g_1HscdXw?pwd=8e64 运行工程请参考SuperMap iDesktopX二次开发项目搭建流程中1.2小节。
四、桌面功能
除了扩展代码外、SuperMap iDesktopX 2024桌面端也有视频检测功能,参考1.1AI环境配置,配置完成之后,打开SuperMap iDesktopX 2024桌面端,具体操作步骤可以查看帮助文档中目标检测。以下是桌面端运行结果: