1.我们在监控视频的时候,往往想做到监控视频发生的事件,才会有意义,要监控视频发生的事件,我们必须借助于视频分析处理器来进行算法的分析。以某个视频分析处理器为列。
单独的录像机监控如下所示:
类似在监控室一样看到的录像机或者摄像头实时监控的视频,如果要进行视频事件分析,我们得进行事件监控
2.视频智能分析处理器
指计算机图像视觉分析技术,通过将场景中背景和目标分离进而分析并追踪在摄像机场景内出现的目标。用户可以根据的视频内容分析功能,通过在不同摄像机的场景中预设不同的报警规则,一旦目标在场景中出现了违反预定义规则的行为,系统会自动发出报**控工作站自动弹出报警信息并发出警示音,用户可以通过点击报警信息,实现报警的场景重组并采取相关措施。
咱们用的是某个厂家的智能分析处理器
这边我就不过多介绍了,涉及到其他的厂家的一些信息,反正是使用Ubuntu的系统
3.配置摄像头或者录像机到智能分析处理器内部
首先我们录像机或者摄像头会有个账号密码,自己也可以修改的。我们登录到摄像头的网络地址,或者录像机的网络地址,会在里面看到相关的rtsp协议的流地址,使用这个流地址,我们可以配置到智能分析处理器,基本上使用的摄像头和录像机都是支持的,我们这边用到的是海康的摄像头或者录像机,当然您也可以用大华的。
从来录像机和摄像头的网络地址可以看到rtsp的地址如上图所示差不多的。
复制出来可以在智能分析系统的平台进行配置
设置监测的事件
保存之后可以进行视频预览,看看是否在监控中,如下图就成功监测中了
咱们可以看到设置的事件会在上面显示,这样就可以监测相关事件了
4.我们程序需要采集相关的事件,保存后续做展示用的
可以声明一些枚举类相关进行处理事件,因为和其他产品相关我这边不作过多的解释
以下代码可以进行视频录像机,摄像头的是否接入的状态,是否在线状态,事件相关的同步都是可以做到的
package com.mp.core.http;
import cn.hutool.core.net.NetUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mp.core.model.dto.business.CameraEventDataHttpDTO;
import com.mp.core.model.dto.business.CameraEventLoginDTO;
import com.mp.core.model.dto.business.CameraInfoDataDTO;
import com.mp.core.model.dto.business.CameraInfoDataHttpDTO;
import com.mp.core.model.entity.safety.SafetyCamera;
import com.mp.core.model.entity.safety.SafetyCameraEventType;
import com.mp.core.model.enums.EventTypeEnum;
import com.mp.core.service.safety.SafetyCameraEventTypeService;
import com.mp.core.service.safety.SafetyCameraService;
import com.mp.core.utils.HttpCameraEventUtil;
import com.mp.core.utils.SpringUtilsAuTo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author lrx
* @description: TODO 摄像头事件http
* @date 2022/10/12 11:56
*/
@AllArgsConstructor
@Slf4j
public class CameraRelevantHttp {
public static String appKey = "appKey";
public static String appSecret = "appSecret ";
private static SafetyCameraService safetyCameraService = SpringUtilsAuTo.getBean(SafetyCameraService.class);
private static SafetyCameraEventTypeService safetyCameraEventTypeService = SpringUtilsAuTo.getBean(SafetyCameraEventTypeService.class);
//登录操作获取相关验证信息
public static CameraEventLoginDTO cameraEventLoginAuthInfo() throws UnknownHostException {
log.info("登录操作获取相关验证信息");
InetAddress inetAddress = InetAddress.getLocalHost();
//组装参数
Map<String, String> loginToCameraEventMap = new HashMap<>();
loginToCameraEventMap.put("appkey", appKey);
loginToCameraEventMap.put("appsecret", appSecret);
loginToCameraEventMap.put("deviceUniqueID", NetUtil.getMacAddress(inetAddress));
loginToCameraEventMap.put("timestamp", Long.toString(System.currentTimeMillis() / 1000L));
return HttpCameraEventUtil.cameraEventLoginPostForm("http://ip:8063/openapi/JinYeeAPIN/loginInfo", loginToCameraEventMap);
}
//根据条件获取摄像头事件数据
public static CameraEventDataHttpDTO cameraEventDataInfo(CameraEventLoginDTO cameraEventLoginDTO, String startTime, String endTime) {
String pagenum = "0";
String perpagenum = "10000";
log.info("根据条件获取摄像头事件数据");
//组装参数
Map<String, String> cameraEventDataMap = new HashMap<>();
cameraEventDataMap.put("token", cameraEventLoginDTO.getToken());
cameraEventDataMap.put("UUID", cameraEventLoginDTO.getUUID());
cameraEventDataMap.put("pagenum", pagenum);
cameraEventDataMap.put("perpagenum", perpagenum);
cameraEventDataMap.put("starttime", startTime);
cameraEventDataMap.put("endtime", endTime);
return HttpCameraEventUtil.cameraEventDataPostForm("http://ip:8063/openapi/JinYeeAPIN/getEventInfo", cameraEventDataMap);
}
//根据条件获取摄像头信息数据
public static void syncCameraInfo(CameraEventLoginDTO cameraEventLoginDTO, String type) {
log.info("根据条件获取摄像头信息数据");
//组装参数
Map<String, String> cameraInfoDataMap = new HashMap<>();
cameraInfoDataMap.put("token", cameraEventLoginDTO.getToken());
cameraInfoDataMap.put("UUID", cameraEventLoginDTO.getUUID());
cameraInfoDataMap.put("type", type);
CameraInfoDataHttpDTO cameraInfoDataHttpDTO = HttpCameraEventUtil.syncCameraInfoPostForm("http://ip:8063/openapi/JinYeeAPIN/getDevices", cameraInfoDataMap);
//判断摄像头信息不为空
if (cameraInfoDataHttpDTO.getList() != null && cameraInfoDataHttpDTO.getList().size() > 0) {
for (CameraInfoDataDTO cameraInfoDataDTO : cameraInfoDataHttpDTO.getList()) {
SafetyCamera safetyCameraByCameraNumber = safetyCameraService.getOne(Wrappers.<SafetyCamera>lambdaQuery().eq(SafetyCamera::getValid, 1).eq(SafetyCamera::getCameraNumber, cameraInfoDataDTO.getCameranumber()));
if (safetyCameraByCameraNumber != null) {
//更新视频播放地址
safetyCameraByCameraNumber.setPlaybackAddress(cameraInfoDataDTO.getIPAddress() + ":" + cameraInfoDataDTO.getVideoport() + "/" + cameraInfoDataDTO.getVideotype());
safetyCameraService.saveOrUpdate(safetyCameraByCameraNumber);
toUpdateSafetyCameraByCamera(safetyCameraByCameraNumber, cameraInfoDataDTO);
}
}
}
}
//更新摄像头关联的事件类型
public static void toUpdateSafetyCameraByCamera(SafetyCamera safetyCameraByCameraNumber, CameraInfoDataDTO cameraInfoDataDTO) {
//根据安防摄像头id删除摄像头对应的事件类型信息
safetyCameraEventTypeService.deleteSafetyCameraEventTypeBySafetyCameraId(safetyCameraByCameraNumber.getId());
List<SafetyCameraEventType> safetyCameraEventTypeList = new ArrayList<>();
if (cameraInfoDataDTO.getChase() == 1) {
SafetyCameraEventType safetyCameraEventType = new SafetyCameraEventType();
safetyCameraEventType.setSafetyCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEventType.setEventType(EventTypeEnum.CHASE);
safetyCameraEventTypeList.add(safetyCameraEventType);
}
if (cameraInfoDataDTO.getFace() == 1) {
SafetyCameraEventType safetyCameraEventType = new SafetyCameraEventType();
safetyCameraEventType.setSafetyCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEventType.setEventType(EventTypeEnum.FACE);
safetyCameraEventTypeList.add(safetyCameraEventType);
}
if (cameraInfoDataDTO.getAgeFlag() == 1) {
SafetyCameraEventType safetyCameraEventType = new SafetyCameraEventType();
safetyCameraEventType.setSafetyCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEventType.setEventType(EventTypeEnum.AGEFLAG);
safetyCameraEventTypeList.add(safetyCameraEventType);
}
if (cameraInfoDataDTO.getDensity() == 1) {
SafetyCameraEventType safetyCameraEventType = new SafetyCameraEventType();
safetyCameraEventType.setSafetyCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEventType.setEventType(EventTypeEnum.DENSITY);
safetyCameraEventTypeList.add(safetyCameraEventType);
}
if (cameraInfoDataDTO.getFight() == 1) {
SafetyCameraEventType safetyCameraEventType = new SafetyCameraEventType();
safetyCameraEventType.setSafetyCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEventType.setEventType(EventTypeEnum.FIGHT);
safetyCameraEventTypeList.add(safetyCameraEventType);
}
if (cameraInfoDataDTO.getFire() == 1) {
SafetyCameraEventType safetyCameraEventType = new SafetyCameraEventType();
safetyCameraEventType.setSafetyCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEventType.setEventType(EventTypeEnum.FIRE);
safetyCameraEventTypeList.add(safetyCameraEventType);
}
if (cameraInfoDataDTO.getHelmet() == 1) {
SafetyCameraEventType safetyCameraEventType = new SafetyCameraEventType();
safetyCameraEventType.setSafetyCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEventType.setEventType(EventTypeEnum.HELMET);
safetyCameraEventTypeList.add(safetyCameraEventType);
}
if (cameraInfoDataDTO.getHover() == 1) {
SafetyCameraEventType safetyCameraEventType = new SafetyCameraEventType();
safetyCameraEventType.setSafetyCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEventType.setEventType(EventTypeEnum.HOVER);
safetyCameraEventTypeList.add(safetyCameraEventType);
}
if (cameraInfoDataDTO.getKnife() == 1) {
SafetyCameraEventType safetyCameraEventType = new SafetyCameraEventType();
safetyCameraEventType.setSafetyCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEventType.setEventType(EventTypeEnum.KNIFE);
safetyCameraEventTypeList.add(safetyCameraEventType);
}
if (cameraInfoDataDTO.getMask() == 1) {
SafetyCameraEventType safetyCameraEventType = new SafetyCameraEventType();
safetyCameraEventType.setSafetyCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEventType.setEventType(EventTypeEnum.MASK);
safetyCameraEventTypeList.add(safetyCameraEventType);
}
if (cameraInfoDataDTO.getOil() == 1) {
SafetyCameraEventType safetyCameraEventType = new SafetyCameraEventType();
safetyCameraEventType.setSafetyCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEventType.setEventType(EventTypeEnum.OIL);
safetyCameraEventTypeList.add(safetyCameraEventType);
}
if (cameraInfoDataDTO.getOverstep() == 1) {
SafetyCameraEventType safetyCameraEventType = new SafetyCameraEventType();
safetyCameraEventType.setSafetyCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEventType.setEventType(EventTypeEnum.OVERSTEP);
safetyCameraEventTypeList.add(safetyCameraEventType);
}
if (cameraInfoDataDTO.getReflective() == 1) {
SafetyCameraEventType safetyCameraEventType = new SafetyCameraEventType();
safetyCameraEventType.setSafetyCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEventType.setEventType(EventTypeEnum.REFLECTIVE);
safetyCameraEventTypeList.add(safetyCameraEventType);
}
if (cameraInfoDataDTO.getLeavePosition() == 1) {
SafetyCameraEventType safetyCameraEventType = new SafetyCameraEventType();
safetyCameraEventType.setSafetyCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEventType.setEventType(EventTypeEnum.LEAVEPOSITION);
safetyCameraEventTypeList.add(safetyCameraEventType);
}
safetyCameraEventTypeService.saveBatch(safetyCameraEventTypeList);
}
}
也可以通过websocket进行实时的自动获取事件
我这里有些自己项目的代码,可以自行去除
package com.mp.core.websocket;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mp.common.core.utils.DateUtils;
import com.mp.common.core.utils.ResponseUtil;
import com.mp.core.model.consts.CacheKeyConst;
import com.mp.core.model.consts.OnlineStatusDataConst;
import com.mp.core.model.dto.business.CameraEventDataDTO;
import com.mp.core.model.dto.business.CameraStateDataDTO;
import com.mp.core.model.entity.safety.SafetyCamera;
import com.mp.core.model.entity.safety.SafetyCameraEvent;
import com.mp.core.model.enums.EventStatusEnum;
import com.mp.core.model.enums.OnlineStatusEnum;
import com.mp.core.model.query.safety.SafetyEventInfoQuery;
import com.mp.core.service.safety.SafetyCameraEventService;
import com.mp.core.service.safety.SafetyCameraService;
import com.mp.core.utils.SpringUtilsAuTo;
import com.mp.core.utils.VideoOrPictureDownloadUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import javax.annotation.PostConstruct;
import javax.websocket.*;
import java.io.IOException;
import java.net.URI;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
/**
* @author lrx
* @description: TODO 摄像头事件socket控制台
* @date 2022/10/12 10:13
*/
@Slf4j
/*@Component*/
@ClientEndpoint
public class CameraEventSocketClient {
private static SafetyCameraService safetyCameraService = SpringUtilsAuTo.getBean(SafetyCameraService.class);
private static SafetyCameraEventService safetyCameraEventService = SpringUtilsAuTo.getBean(SafetyCameraEventService.class);
private static SimpMessagingTemplate simpMessagingTemplate = SpringUtilsAuTo.getBean(SimpMessagingTemplate.class);
private static RedissonClient redissonClient = SpringUtilsAuTo.getBean(RedissonClient.class);
private String serverUrl = "ws://ip:8063/snapPush";
private Session session;
@PostConstruct
void init() {
try {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
URI uri = URI.create(serverUrl);
session = container.connectToServer(CameraEventSocketClient.class, uri);
} catch (DeploymentException | IOException e) {
e.printStackTrace();
}
}
/**
* 打开连接
*
* @param session
*/
@OnOpen
public void onOpen(Session session) {
log.info("打开");
this.session = session;
}
/**
* 接收消息
*
* @param text
*/
@OnMessage
public void onMessage(String text) throws ParseException {
Map<String, Object> dataMap = JSONObject.parseObject(text);
CameraEventDataDTO cameraEventDateDTO = null;
CameraStateDataDTO cameraStateDataDTO = null;
if (dataMap.containsKey("event_path")) {
cameraEventDateDTO = CameraEventDataDTO.builder().eventPath(dataMap.get("event_path").toString()).devID(dataMap.get("devID").toString())
.code(Integer.parseInt(dataMap.get("code").toString())).cameraNumber(dataMap.get("cameranumber").toString())
.cameraNote(dataMap.get("cameranote").toString()).time(DateUtils.toDateTime(dataMap.get("time").toString()))
.type(dataMap.get("type").toString()).build();
}
if (dataMap.containsKey("cameraNumber")) {
cameraStateDataDTO = CameraStateDataDTO.builder().cameraNumber(dataMap.get("cameraNumber").toString())
.code(Integer.parseInt(dataMap.get("code").toString())).type(dataMap.get("type").toString())
.status(dataMap.get("status").toString()).build();
}
//后面离线,或者在线通过判断{"cameraNumber":"1","code":0,"type":"refresh","status":"fail"}
if (cameraEventDateDTO != null) {
log.info("接收消息:" + cameraEventDateDTO);
SafetyCamera safetyCameraByCameraNumber = safetyCameraService.getOne(Wrappers.<SafetyCamera>lambdaQuery().eq(SafetyCamera::getValid, 1).eq(SafetyCamera::getCameraNumber, cameraEventDateDTO.getCameraNumber()));
if (safetyCameraByCameraNumber != null) {
SafetyCameraEvent safetyCameraEvent = new SafetyCameraEvent();
safetyCameraEvent.setCameraId(safetyCameraByCameraNumber.getId());
safetyCameraEvent.setEventDate(cameraEventDateDTO.getTime());
safetyCameraEvent.setEventStatus(EventStatusEnum.WAITING_PROCESS);
safetyCameraEvent.setEventContent(cameraEventDateDTO.getType());
safetyCameraEvent.setEventNumber(getEventNumber());
safetyCameraEventService.save(safetyCameraEvent);
String videoOrPictureUrl = "ip:8063/openapi" + cameraEventDateDTO.getEventPath();
//下载视频到本地保存
VideoOrPictureDownloadUtils.videoOrPictureDownload(safetyCameraEvent, videoOrPictureUrl);
//从缓存中获取Token
RBucket<String> bucket = redissonClient.getBucket(CacheKeyConst.USER_PUSH);
if (!StringUtils.isBlank(bucket.get())) {
List<Long> userPushList = JSONObject.parseArray(bucket.get(), Long.class);
for (Long userId : userPushList) {
log.info("----------------------推送" + simpMessagingTemplate + "-----------------------------");
SafetyEventInfoQuery safetyEventInfoQuery = new SafetyEventInfoQuery();
simpMessagingTemplate.convertAndSend("/topic/safetyEventInfoDetailsByPush/" + userId, ResponseUtil.responseSuccess(safetyCameraEventService.safetyEventInfoDetails(safetyEventInfoQuery)));
}
}
}
}
//处理摄像头状态数据
if (cameraStateDataDTO != null) {
log.info("接收消息:" + cameraStateDataDTO);
SafetyCamera safetyCameraByCameraNumber = safetyCameraService.getOne(Wrappers.<SafetyCamera>lambdaQuery().eq(SafetyCamera::getValid, 1).eq(SafetyCamera::getCameraNumber, cameraStateDataDTO.getCameraNumber()));
if (safetyCameraByCameraNumber != null) {
switch (cameraStateDataDTO.getStatus()) {
case OnlineStatusDataConst.ON_LINE:
safetyCameraByCameraNumber.setOnlineStatus(OnlineStatusEnum.ON_LINE);
break;
case OnlineStatusDataConst.OFF_LINE:
safetyCameraByCameraNumber.setOnlineStatus(OnlineStatusEnum.OFF_LINE);
break;
}
}
safetyCameraService.saveOrUpdate(safetyCameraByCameraNumber);
}
}
/**
* 获取事件编号
*
* @return
*/
private String getEventNumber() {
//设置事件编号(递增)
String eventNumber = safetyCameraEventService.getObj(new QueryWrapper<SafetyCameraEvent>().select("event_number").orderByDesc("event_number")
.last("limit 1"), item -> (String) item);
if (eventNumber == null) {
eventNumber = String.format("%08d", Integer.valueOf(1));
} else {
eventNumber = String.format("%08d", Integer.valueOf(Integer.parseInt(eventNumber) + 1));
}
return eventNumber;
}
/**
* 异常处理
*
* @param throwable
*/
@OnError
public void onError(Throwable throwable) {
throwable.printStackTrace();
}
/**
* 关闭连接
*/
@OnClose
public void onClosing() throws IOException {
log.info("关闭");
session.close();
}
/**
* 主动发送消息
*/
public void send(String message) {
this.session.getAsyncRemote().sendText(message);
}
public void close() throws IOException {
if (this.session.isOpen()) {
this.session.close();
}
}
}
后面我们可以看到数据库表中有相关的摄像头状态更新,和事件的实时存储
摄像头表的数据
摄像头关联的事件类型
监控的事件数据