赞
踩
本项目最具亮点之处——简单:
如果有需求,也可以进行对接消息中间件实现告警推送的扩展。
定时抓图
@Override
public BaseResult faceDetection(String streamId) {
ScheduledFuture scheduledFuture = ThreadPoolTaskConfig.getScheduledFuture(streamId);
if(Objects.nonNull(scheduledFuture)){
log.warn("[FaceServiceImpl] Face detection: {} is running.", streamId);
return BaseResult.success("success");
}
List<String> playingStreams = redisUtil.getPlayingStreams();
if(!playingStreams.contains(streamId)){
log.error("[FaceDetection] Stream ID: {} inexistence.", streamId);
return BaseResult.success("success");
}
MediaServerDTO mediaServerDTO = redisUtil.getMediaServerDTO();
String fileName = streamId + ".jpg";
schedulerManager.startPeriod(streamId, () -> {
List<String> streams = redisUtil.getPlayingStreams();
if(!streams.contains(streamId)){
schedulerManager.stopSchedule(streamId);
TaskContainer.remove(captureFaceSavePath, fileName);
}
// 抓图
// rtsp://10.192.33.34:554/rtp/34020000001320000001_34020000001320000001
// String streamUrl = String.format("rtsp://%s:%s/%s/%s/", mediaServerDTO.getIp(), "554", "rtp", streamId);
// http://10.192.33.34:80/rtp/34020000001320000001_34020000001320000001.live.flv
String streamUrl = String.format("http://%s:%s/%s/%s.live.flv", mediaServerDTO.getIp(), mediaServerDTO.getHttpPort(), "rtp", streamId);
// 请求截图
log.info("[CaptureTask]: " + fileName);
// zlmRestfulUtils.getSnap(mediaServerDTO, streamUrl, 15, 1, captureFaceSavePath, fileName);
FaceUtil.capture(ffmpegBinPath, streamUrl, captureFaceSavePath + fileName);
// try {
// FaceUtil.capture(streamUrl, captureFaceSavePath + fileName);
// } catch (IOException e) {
// e.printStackTrace();
// }
// FaceUtil.capture();
}, 8);
Optional<TaskPO> taskDaoById = taskDao.findById(streamId);
if(taskDaoById.isPresent()){
TaskPO taskPO = taskDaoById.get();
taskPO.setFaceDetection(true);
taskDao.save(taskPO);
}
TaskContainer.addStream(captureFaceSavePath, fileName);
return BaseResult.success("success");
}
package com.example.mediasiphttpserver.config;
import com.example.mediasiphttpserver.server.face.task.CaptureTask;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
/**
* @author yrz
* @create 2023/12/20
*/
@Configuration
public class SchedulerConfig {
@Autowired
private Scheduler scheduler;
@PostConstruct
public void scheduleJobs() throws SchedulerException {
// 创建第一个JobDetail和Trigger
JobDetail jobDetail1 = JobBuilder.newJob(CaptureTask.class)
.withIdentity("job1", "group1")
.build();
CronTrigger trigger1 = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ?"))
.build();
// 将JobDetail和Trigger关联到Scheduler
scheduler.scheduleJob(jobDetail1, trigger1);
}
}
package com.example.mediasiphttpserver.server.face.task;
import com.example.mediasiphttpserver.server.face.utils.FaceUtil;
import com.example.mediasiphttpserver.server.face.utils.TaskContainer;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author yrz
* @create 2023/12/19
*/
@Slf4j
public class CaptureTask implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
Set<Map.Entry<String, HashSet<String>>> entries = TaskContainer.getEntries();
for(Map.Entry<String, HashSet<String>> entry : entries){
String captureFaceSavePath = entry.getKey();
HashSet<String> fileNames = entry.getValue();
for(String fileName : fileNames){
FaceUtil.faceDetection(captureFaceSavePath, fileName);
}
}
}
}
flv预览后点击人脸任务
开启/停止任务
查看结果(无人脸被识别到展示默认图片)
有想了解该项目或想要源码的私信我(づ。◕ᴗᴗ◕。)づ
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。