recommend-video/src/main/java/com/iplatform/recvideo/SimilarExecutor.java
@@ -4,6 +4,7 @@ import com.iplatform.recvideo.util.PythonInvokeUtils; import com.iplatform.recvideo.util.TestUtils; import com.iplatform.recvideo.util.VideoFileUtils; import com.walker.infrastructure.utils.FileUtils; import com.walker.infrastructure.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -84,7 +85,14 @@ public int execute() throws Exception{ if(!this.pythonLoadVideoDone){ logger.debug("当前 pythonLoadVideoDone = false, 需要查询数据库是否已加载视频"); this.pythonLoadVideoDone = this.pythonLoadVideoDone(this.batchId, VideoFileUtils.combineBatchPath(videoDataFolder, batchId)); String batchPath = VideoFileUtils.combineBatchPath(videoDataFolder, batchId); if(!FileUtils.isExist(batchPath)){ // 文件夹不存在,或略该状态记录,2022-10-11 this.ignoreTaskStatus(batchId); logger.warn("文件夹不存在,忽略该批次记录,自动更新为'已完成', batchPath = " + batchPath); return 1; } this.pythonLoadVideoDone = this.pythonLoadVideoDone(this.batchId, batchPath); } // 1: 如果视频还未加载,则先加载视频 @@ -114,9 +122,13 @@ } } if(StringUtils.isEmptyList(this.videoFolderInfoList)){ logger.warn("视频分析文件夹内容为空,无法继续查询相似度结果! videoFolderInfoList = null"); return -1; logger.warn("视频分析文件夹内容为空,无法继续查询相似度结果!忽略该批次。 videoFolderInfoList = null"); // return -1; // 文件夹不没有任何视频文件,或略该状态记录,2022-10-11 this.ignoreTaskStatus(batchId); return 1; } for(VideoFolderInfo v : this.videoFolderInfoList){ this.videoIdList.add(v.getVideoId()); } @@ -246,4 +258,11 @@ * @param recVideoIdList 本批次处理原始视频id集合 */ protected abstract void writeRcVideoUser(String batchId, List<String> recVideoIdList); /** * 忽略该状态批次,由于文件夹不存在等原因,不带处理,不要在这里打伴。 * @param batchId * @date 2022-10-11 */ protected abstract void ignoreTaskStatus(String batchId); } recommend-video/src/main/java/com/iplatform/recvideo/service/VideoExecutorServiceImpl.java
@@ -21,6 +21,7 @@ private static final String SQL_GET_BATCH_VIDEO = "select user_id, src_video_id from rc_video_batch where batch_id=?"; private static final String SQL_UPDATE_TASK_STATUS_LOAD = "update rc_task_status set status='1', end_time=? where last_value=? and status='0'"; private static final String SQL_IGNORE_TASK_STATUS_LOAD = "update rc_task_status set status='1', msg='ignore' where last_value=? and status='0' and task_type='video_load'"; /** * 写入视频相似度第一级临时数据,每个图像包含多个相似视频记录。 @@ -56,6 +57,9 @@ this.execute(SQL_UPDATE_TASK_STATUS_LOAD, param); } public void execIgnoreTaskStatus(String batchId){ this.execute(SQL_IGNORE_TASK_STATUS_LOAD, new Object[]{Long.parseLong(batchId)}); } /** * 返回一个批次用户对应视频记录集合,用于最后更新用户推荐视频数据。 recommend-video/src/main/java/com/iplatform/recvideo/support/DefaultSimilarExecutor.java
@@ -52,7 +52,9 @@ @Override protected String requestStartPythonLoadVideo(String batchId) throws Exception { logger.info("开始请求python服务:" + Constants.AI_SERVICE_VIDEO_LOAD); return null; String url = this.remoteUrl + Constants.AI_SERVICE_VIDEO_LOAD; boolean success = PythonInvokeUtils.postLoadVideoGather(batchId, url, this.restTemplate); return success? null : "python 加载视频调用失败"; } @Override @@ -138,6 +140,11 @@ logger.debug("批次任务状态已更新成功! " + batchId); } @Override protected void ignoreTaskStatus(String batchId) { this.videoExecutorService.execIgnoreTaskStatus(batchId); } private List<Rc_video_user> toRcVideoUserList(List<SimilarVideoUser> similarVideoUserList){ if(StringUtils.isEmptyList(similarVideoUserList)){ return null; recommend-video/src/main/java/com/iplatform/recvideo/util/LoadRequest.java
New file @@ -0,0 +1,32 @@ package com.iplatform.recvideo.util; import java.io.Serializable; /** * 请求python加载视频(一个批次) * @date 2022-10-11 */ public class LoadRequest implements Serializable { private String date; private String frame_rate = "30"; public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getFrame_rate() { return frame_rate; } public void setFrame_rate(String frame_rate) { this.frame_rate = frame_rate; } } recommend-video/src/main/java/com/iplatform/recvideo/util/PythonInvokeUtils.java
@@ -18,6 +18,36 @@ private static final transient Logger logger = LoggerFactory.getLogger(PythonInvokeUtils.class); public static boolean postLoadVideoGather(String batchId, String url, RestTemplate restTemplate) throws Exception{ LoadRequest request = new LoadRequest(); request.setDate(batchId); request.setFrame_rate("30"); ResponseEntity<String> entity = restTemplate.postForEntity(url, request, String.class); if(entity == null){ return false; } String jsonData = entity.getBody(); try { Map<String, Object> map = JsonUtils.jsonStringToObject(jsonData, Map.class); if(map == null || !map.containsKey("code")){ logger.error("python返回结果为空,或者没有code标志,无法判断调用加载视频成功!"); return false; } String code = map.get("code").toString(); if(code.equals("0")){ logger.info("python notify_gather_once() 加载视频处理成功! batchId = " + batchId); return true; } logger.warn("python notify_gather_once() 加载视频处理失败, batchId = " + batchId); return false; } catch (Exception e) { logger.error("解析json结果错误:" + jsonData, e); throw e; } } public static List<Rc_video_t1> acquireImageSearchResult(String videoId, String imgPath , String topN, String url, RestTemplate restTemplate) throws Exception{ SearchRequest request = new SearchRequest(); @@ -55,6 +85,8 @@ * @return */ public static final String getFileNameWithoutSuffix(String videoPath, String suffix){ // 如果存在windows反斜杠,先转换成正斜杠。2022-10-11 videoPath = videoPath.replaceAll("\\\\", StringUtils.FOLDER_SEPARATOR); String[] array = videoPath.split("/"); if(array == null || array.length == 0){ logger.error("视频名称截取id错误:" + videoPath); recommend-video/src/test/java/com/iplatform/recvideo/VideoSimilarTest.java
@@ -34,9 +34,10 @@ } } // @Test @Test public void testGetVideoId(){ String id = PythonInvokeUtils.getFileNameWithoutSuffix("/opt/ai/video/20220921/landscape_01.mp4", Constants.VIDEO_SUFFIX); // String id = PythonInvokeUtils.getFileNameWithoutSuffix("/opt/ai/video/20220921/landscape_01.mp4", Constants.VIDEO_SUFFIX); String id = PythonInvokeUtils.getFileNameWithoutSuffix("D:\\dev_tools\\ai\\20220520144630\\1527541255435849728\\1527541255435849728_0.jpg", Constants.IMAGE_SUFFIX); System.out.println(id); }