From 7b3b249a7f2320f97e21e94e26a65f4b4ead0b6e Mon Sep 17 00:00:00 2001
From: shikeying <shikeying@163.com>
Date: 星期六, 24 九月 2022 16:50:25 +0800
Subject: [PATCH] 视频相似度分析1

---
 recommend-video/src/main/java/com/iplatform/recvideo/SimilarExecutor.java |  107 ++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 93 insertions(+), 14 deletions(-)

diff --git a/recommend-video/src/main/java/com/iplatform/recvideo/SimilarExecutor.java b/recommend-video/src/main/java/com/iplatform/recvideo/SimilarExecutor.java
index 90d4868..1318e53 100644
--- a/recommend-video/src/main/java/com/iplatform/recvideo/SimilarExecutor.java
+++ b/recommend-video/src/main/java/com/iplatform/recvideo/SimilarExecutor.java
@@ -1,12 +1,15 @@
 package com.iplatform.recvideo;
 
 import com.iplatform.model.po.Rc_video_t1;
+import com.iplatform.recvideo.util.PythonInvokeUtils;
+import com.iplatform.recvideo.util.TestUtils;
 import com.iplatform.recvideo.util.VideoFileUtils;
 import com.walker.infrastructure.utils.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.File;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -25,7 +28,9 @@
 
     private String batchId = null;
 
+    // 褰撳墠鎵规瑕佸鐞嗙殑鍘熷瑙嗛闆嗗悎
     private List<VideoFolderInfo> videoFolderInfoList = null;
+    private List<String> videoIdList = new ArrayList<>();
 
     // 璁板綍褰撳墠鎵ц鍒�(璇ユ壒娆�)鍝釜瑙嗛鏂囦欢瀵瑰簲鐨勭鍑犱釜鍥剧墖
     private int currentVideoFolderIndex = -1;
@@ -33,15 +38,24 @@
 //    private VideoFolderInfo currentVideoFolderInfo = null;
 //    private ImageInfo currentImageInfo = null;
 
-    //
+    // 鏄惁宸插畬鎴愯棰戝姞杞借皟鐢紝瀹屾垚鍚庢墠鑳芥绱㈢浉浼艰棰�
     private boolean pythonLoadVideoDone = false;
+
+    private int topN = 40;
+
+    // 濡傛灉娴嬭瘯妯″紡锛屾暟鎹槸鍋囩殑!
+    private boolean testMode = false;
+
+    public int getTopN() {
+        return topN;
+    }
 
     /**
      * 鍒濆鍖栧璞¤皟鐢ㄤ竴娆�
      * @param videoDataFolder
      * @param batchId
      */
-    public void startup(String videoDataFolder, String batchId){
+    public void startup(String videoDataFolder, String batchId, int topN, boolean testMode){
         if(StringUtils.isEmpty(videoDataFolder)){
             throw new IllegalArgumentException("瑙嗛鏂囦欢澶规牴鐩綍蹇呴』璁剧疆!");
         }
@@ -50,17 +64,28 @@
         }
         this.videoDataFolder = videoDataFolder;
         this.batchId = batchId;
+        this.topN = topN;
+        this.testMode = testMode;
 //        this.videoFolderInfoList = VideoFileUtils.getBatchVideoFolderInfo(this.videoDataFolder, batchId);
+    }
+
+    public void destroy(){
+        if(this.videoFolderInfoList != null){
+            this.videoFolderInfoList.clear();
+        }
+        if(this.videoIdList != null){
+            this.videoIdList.clear();
+        }
     }
 
     /**
      * 鍦ㄦ瘡娆¤皟搴︽椂閽熷懆鏈熸墽琛屼竴娆°�備緥濡�: 10绉掍竴娆°��<p></p>
      * 娉ㄦ剰锛氳鏂规硶鑻辩‘淇濇瘡娆¤皟鐢ㄤ笉浼氶噸澶嶆暟鎹��
      */
-    public void execute(){
+    public int execute() throws Exception{
         if(!this.pythonLoadVideoDone){
             logger.debug("褰撳墠 pythonLoadVideoDone = false, 闇�瑕佹煡璇㈡暟鎹簱鏄惁宸插姞杞借棰�");
-            this.pythonLoadVideoDone = this.pythonLoadVideoDone(this.batchId, videoDataFolder + File.separator + batchId);
+            this.pythonLoadVideoDone = this.pythonLoadVideoDone(this.batchId, VideoFileUtils.combineBatchPath(videoDataFolder, batchId));
         }
 
         // 1: 濡傛灉瑙嗛杩樻湭鍔犺浇锛屽垯鍏堝姞杞借棰�
@@ -70,29 +95,42 @@
                 if(StringUtils.isNotEmpty(error)){
                     // 缁堟璋冪敤锛岀瓑寰呬笅娆¤皟搴︾户缁皾璇曟墽琛�
                     logger.error("python璋冪敤鍔犺浇瑙嗛杩斿洖閿欒:" + error);
-                    return;
+                    return -1;
                 }
                 this.pythonLoadVideoDone = true;
 
             } catch (Exception ex){
                 logger.error("python璋冪敤鍔犺浇瑙嗛寮傚父:" + this.batchId, ex);
-                return;
+                return -1;
             }
         }
 
         // 2: 鍔犺浇瀹岃棰戯紝闇�瑕佹煡璇㈡瘡涓浘鐗囩浉浼煎害缁撴灉锛屽苟瀛樺偍鍒版暟鎹簱
         //    杩欓噷娉ㄦ剰锛岀▼搴忓繀椤诲拰AI鏈嶅姟鍣ㄩ儴缃插湪涓�璧凤紝鏂逛究妫�绱㈣棰戝垎鏋愭枃浠跺す(鏈湴)
         if(this.videoFolderInfoList == null){
-            this.videoFolderInfoList = VideoFileUtils.getBatchVideoFolderInfo(this.videoDataFolder, batchId);
+            if(testMode){
+                this.videoFolderInfoList = TestUtils.getBatchVideoFolderInfo(this.videoDataFolder, batchId);
+            } else {
+                this.videoFolderInfoList = VideoFileUtils.getBatchVideoFolderInfo(this.videoDataFolder, batchId);
+            }
         }
         if(StringUtils.isEmptyList(this.videoFolderInfoList)){
             logger.warn("瑙嗛鍒嗘瀽鏂囦欢澶瑰唴瀹逛负绌猴紝鏃犳硶缁х画鏌ヨ鐩镐技搴︾粨鏋�! videoFolderInfoList = null");
-            return;
+            return -1;
+        }
+        for(VideoFolderInfo v : this.videoFolderInfoList){
+            this.videoIdList.add(v.getVideoId());
         }
 
         if(this.isSearchWriteDone()){
-            logger.info("宸茬粡瀹屾垚鎵规鐩镐技缁撴灉鍐欏叆鏁版嵁搴擄紝涓嶅啀寰�涓嬪鐞嗐�俠atch = " + this.batchId);
-            return;
+            logger.info("宸茬粡瀹屾垚鎵规鐩镐技缁撴灉鍐欏叆鏁版嵁搴擄紝澶勭悊鏈�鍚庝竴姝�:鍐欏叆鐢ㄦ埛鎺ㄨ崘琛ㄦ暟鎹�俠atch = " + this.batchId);
+            try {
+                this.writeRcVideoUser(this.batchId, this.videoIdList);
+                // 杩斿洖1琛ㄧず鏁翠釜娴佺▼鎵ц瀹屾瘯銆�
+                return 1;
+            } catch (Exception ex){
+                throw new Exception("writeRcVideoUser():" + ex.getMessage(), ex);
+            }
         }
 
         // 寮�濮嬫绱㈢浉浼煎害
@@ -100,6 +138,12 @@
             this.currentVideoFolderIndex ++;
         }
 
+        try {
+            this.processOneSearchAndWrite();
+            return 0;
+        } catch (Exception e) {
+            throw new Exception("processOneSearchAndWrite(): " + e.getMessage(), e);
+        }
     }
 
     private void processOneSearchAndWrite() throws Exception{
@@ -113,20 +157,34 @@
         }
         ImageInfo imageInfo = currentVideoFolderInfo.getImageInfoList().get(this.currentImageIndex);
 
-        this.acquirePythonSearchSimilarOnce(imageInfo.getImagePath(), "30");
+        List<Rc_video_t1> videoT1_list = this.acquirePythonSearchSimilarOnce(imageInfo.getVideoId()
+                , imageInfo.getImagePath(), String.valueOf(this.topN));
+        if(!StringUtils.isEmptyList(videoT1_list)){
+            try {
+                this.writeRcVideoT1(videoT1_list, PythonInvokeUtils.getFileNameWithoutSuffix(imageInfo.getImagePath(), Constants.IMAGE_SUFFIX));
+            } catch (Exception ex){
+                throw new Exception("writeRcVideoT1()鎵ц閿欒:" + ex.getMessage(), ex);
+            }
+        }
 
+        // 姣忎釜瑙嗛鐨勬渶鍚庝竴寮犲浘鐗�
         if((this.currentImageIndex + 1) >= currentVideoFolderInfo.getImageInfoSize()){
+            try {
+                this.writeRcVideoT2(currentVideoFolderInfo);
+            } catch (Exception ex){
+                throw new Exception("writeRcVideoT2()鎵ц閿欒:" + ex.getMessage(), ex);
+            }
             if((this.currentVideoFolderIndex + 1) < this.videoFolderInfoList.size()){
                 logger.debug("涓�涓棰戝浘鍍忛泦鍚堟绱㈠鐞嗗畬姣曪紝鍒囨崲鍒颁笅涓�涓紝currentImageIndex = " + this.currentImageIndex);
                 this.currentVideoFolderIndex ++;
                 this.currentImageIndex = -1;
             } else {
-                //
+                // 璁╂渶鍚庝竴涓浘鍍忛泦鍚堢储寮曞�艰秴杩囩晫闄愶紝琛ㄧず鏈�鍚庝竴涓棰戝凡澶勭悊瀹屾瘯銆�
+                this.currentImageIndex ++;
                 logger.debug("鎵�鏈夎棰戝寘鍚殑鎵�鏈夊浘鍍忓鐞嗗畬姣曪紝currentVideoFolderIndex = " + this.currentVideoFolderIndex);
             }
             return;
         }
-
         this.currentImageIndex ++;
     }
 
@@ -167,5 +225,26 @@
      * 璇锋眰AI鏈嶅姟锛屾绱㈢粰瀹氬浘鐗囩殑鐩镐技搴︾粨鏋滈泦鍚堛��
      * @return
      */
-    protected abstract List<Rc_video_t1> acquirePythonSearchSimilarOnce(String imagePath, String topN);
+    protected abstract List<Rc_video_t1> acquirePythonSearchSimilarOnce(String videoId
+            , String imagePath, String topN) throws Exception;
+
+    /**
+     * 绗竴涓复鏃惰〃'rc_video_t1'锛屾瘡寮犲浘鍍忓寘鍚涓浉浼艰棰戣褰曘��
+     * @param list
+     * @param srcImageId 鍘熷瑙嗛ID
+     */
+    protected abstract void writeRcVideoT1(List<Rc_video_t1> list, String srcImageId);
+
+    /**
+     * 鍒嗘瀽琛�'rc_video_t1'锛屽苟鎶婄粰瀹氳棰戠浉浼艰褰曞啓鍏ョ浜屼釜涓存椂琛�'rc_video_t2'
+     * @param videoFolderInfo
+     */
+    protected abstract void writeRcVideoT2(VideoFolderInfo videoFolderInfo);
+
+    /**
+     * 鍒嗘瀽缁欏畾鎵规鎵�鏈夎棰戠敤鎴锋帹鑽愮殑瑙嗛淇℃伅锛屽苟鍐欏叆琛�:'rc_video_user'
+     * @param batchId
+     * @param recVideoIdList 鎺ㄨ崘瑙嗛id闆嗗悎
+     */
+    protected abstract void writeRcVideoUser(String batchId, List<String> recVideoIdList);
 }

--
Gitblit v1.9.1