x1ongzhu 6 лет назад
Родитель
Сommit
0fb03e79d6

+ 9 - 0
src/main/java/com/izouma/awesomeadmin/dto/VideoProcessResult.java

@@ -9,6 +9,7 @@ public class VideoProcessResult {
     private Double  liveTime;
     private Integer killNum;
     private String  image;
+    private Long    kill3time;
 
     public Integer getRank() {
         return rank;
@@ -62,6 +63,14 @@ public class VideoProcessResult {
         return rank != null && total != null && score != null && liveTime != null && image != null;
     }
 
+    public Long getKill3time() {
+        return kill3time;
+    }
+
+    public void setKill3time(Long kill3time) {
+        this.kill3time = kill3time;
+    }
+
     @Override
     public String toString() {
         return JSONObject.fromObject(this).toString(4);

+ 0 - 2
src/main/java/com/izouma/awesomeadmin/service/VideoRecognitionService.java

@@ -5,8 +5,6 @@ import com.izouma.awesomeadmin.model.PlayerInfo;
 import java.util.Map;
 
 public interface VideoRecognitionService {
-    boolean start(String videoPath);
-
     void start(PlayerInfo playerInfo);
 
     Map<String, String> stat(String videoPath);

+ 31 - 29
src/main/java/com/izouma/awesomeadmin/service/impl/VideoRecognitionServiceImpl.java

@@ -30,28 +30,34 @@ public class VideoRecognitionServiceImpl implements VideoRecognitionService {
 
     private ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
 
-    @Override
-    public boolean start(String videoPath) {
-        if (processing.get(videoPath) != null) {
-            return true;
-        }
-        processing.put(videoPath, videoPath);
-        lastStat.put(videoPath, System.currentTimeMillis());
-        resultMap.remove(videoPath);
-        executor.execute(() -> {
-            try {
-                Map<String, String> map = videoProcessTool.processVideo(videoPath, 0);
-                if (map != null) {
-                    resultMap.put(videoPath, map);
-                }
-            } catch (FrameGrabber.Exception e) {
-                e.printStackTrace();
-            }
-            processing.remove(videoPath);
-        });
-        return true;
-    }
-
+    // @Override
+    // public void start(PlayerInfo playerInfo) {
+    //     // if (playerInfo.getStatusFlag() > AppConstant.PlayerStatus.END) {
+    //     //     return;
+    //     // }
+    //     executor.execute(() -> {
+    //         try {
+    //             playerInfoService.updatePlayerInfo(playerInfo);
+    //             VideoProcessResult result = videoProcessTool.processVideo(playerInfo.getVideo(), 0);
+    //             if (result != null && result.isValid()) {
+    //                 playerInfo.setImage(result.getImage());
+    //                 playerInfo.setScore(Float.parseFloat(result.getScore().toString()));
+    //                 playerInfo.setLiveTime(Float.parseFloat(result.getLiveTime().toString()));
+    //                 playerInfo.setRanking(result.getRank());
+    //                 // playerInfo.setKillNumber(result.getKillNum());
+    //                 playerInfo.setStatusFlag(AppConstant.PlayerStatus.PROCESSED);
+    //             } else {
+    //                 playerInfo.setStatusFlag(AppConstant.PlayerStatus.PROCESSED_FAIL);
+    //                 playerInfo.setResultError(true);
+    //             }
+    //         } catch (FrameGrabber.Exception e) {
+    //             e.printStackTrace();
+    //             playerInfo.setStatusFlag(AppConstant.PlayerStatus.PROCESSED_FAIL);
+    //             playerInfo.setResultError(true);
+    //         }
+    //         playerInfoService.updatePlayerInfo(playerInfo);
+    //     });
+    // }
     @Override
     public void start(PlayerInfo playerInfo) {
         // if (playerInfo.getStatusFlag() > AppConstant.PlayerStatus.END) {
@@ -60,13 +66,10 @@ public class VideoRecognitionServiceImpl implements VideoRecognitionService {
         executor.execute(() -> {
             try {
                 playerInfoService.updatePlayerInfo(playerInfo);
-                VideoProcessResult result = videoProcessTool.processVideo1(playerInfo.getVideo(), 0);
-                if (result != null && result.isValid()) {
+                VideoProcessResult result = videoProcessTool.processVideo(playerInfo.getVideo(), 0);
+                if (result != null && result.getKill3time() != null) {
                     playerInfo.setImage(result.getImage());
-                    playerInfo.setScore(Float.parseFloat(result.getScore().toString()));
-                    playerInfo.setLiveTime(Float.parseFloat(result.getLiveTime().toString()));
-                    playerInfo.setRanking(result.getRank());
-                    // playerInfo.setKillNumber(result.getKillNum());
+                    // playerInfo.setKill3time(result.getKill3time());
                     playerInfo.setStatusFlag(AppConstant.PlayerStatus.PROCESSED);
                 } else {
                     playerInfo.setStatusFlag(AppConstant.PlayerStatus.PROCESSED_FAIL);
@@ -81,7 +84,6 @@ public class VideoRecognitionServiceImpl implements VideoRecognitionService {
         });
     }
 
-
     @Override
     public Map<String, String> stat(String videoPath) {
         lastStat.put(videoPath, System.currentTimeMillis());

+ 53 - 82
src/main/java/com/izouma/awesomeadmin/util/VideoProcessToolNew.java

@@ -39,88 +39,7 @@ public class VideoProcessToolNew {
     private OSSFileService ossFileService = new OSSFileService();
     public  String         imgPrefix      = "/tmp/";
 
-    public Map<String, String> processVideo(String path, int frameSkip) throws FrameGrabber.Exception {
-        SVM svm = SVM.load(GetResource.class.getClassLoader().getResource("trainneddata/pubg.xml").getPath());
-        Map<String, String> map = new HashMap<>();
-        OpenCVFrameConverter.ToMat converterToMat = new OpenCVFrameConverter.ToMat();
-        long frameCount = 0;
-        FrameGrabber videoGrabber = new FFmpegFrameGrabber(path);
-        videoGrabber.start();
-        System.out.println(videoGrabber.getLengthInFrames());
-        System.out.println(videoGrabber.getLengthInTime());
-        videoGrabber.setTimestamp(62500000L);
-        Frame vFrame;
-        do {
-            vFrame = videoGrabber.grabFrame();
-            if (vFrame != null) {
-                frameCount++;
-                if (!vFrame.keyFrame) {
-                    continue;
-                }
-                System.out.println(vFrame.timestamp);
-                if (frameCount % 200 == 0) {
-                    System.gc();
-                }
-                if (frameSkip > 0 && frameCount % frameSkip != 0) {
-                    continue;
-                }
-                Mat frame = removeBlackBarAndRotate(converterToMat.convert(vFrame));
-                if (matchGameOver(svm, frame)) {
-                    Mat filtered = new Mat();
-                    bilateralFilter(frame, filtered, 25, 25 * 2, 25 / 2f);
-                    if (map.get("rank") == null) {
-                        Integer rank = extractRank(filtered);
-                        if (rank != null)
-                            map.put("rank", rank.toString());
-                    }
-                    if (map.get("total") == null) {
-                        Integer total = extractTotalNum(filtered);
-                        if (total != null)
-                            map.put("total", total.toString());
-                    }
-                    if (map.get("参赛时间") == null) {
-                        Map<String, Double> statistics = getStatistics(filtered);
-                        if (statistics != null) {
-                            statistics.forEach((s, aDouble) -> map.put(s, aDouble.toString()));
-                        }
-                    }
-                    filtered.release();
-                    if (map.get("rank") != null
-                        && map.get("total") != null
-                        && map.get("参赛时间") != null
-                        && map.get("评分") != null) {
-                        double score = 0;
-                        double total = 0;
-                        double rank = 0;
-
-                        try {
-                            score = Double.parseDouble(map.get("评分"));
-                            total = Double.parseDouble(map.get("total"));
-                            rank = Double.parseDouble(map.get("rank").replace("第", ""));
-
-                            if (score > (total - rank) / total * 100 / 2) {
-                                String filename = "/var/samples/" + DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + RandomStringUtils.randomNumeric(8) + ".jpg";
-                                org.bytedeco.javacpp.opencv_imgcodecs.imwrite(filename, frame);
-                                map.put("image", uploadImage(frame));
-                                System.out.println(map);
-                                break;
-                            }
-                        } catch (Exception ignore) {
-                        }
-                    }
-                }
-                frame.release();
-                System.out.println(frameCount + " frame processed");
-            }
-        } while (vFrame != null);
-        videoGrabber.stop();
-        videoGrabber.release();
-        svm.deallocate();
-        System.gc();
-        return map;
-    }
-
-    public VideoProcessResult processVideo1(String path, int frameSkip) throws FrameGrabber.Exception {
+    public VideoProcessResult processVideo(String path, int frameSkip) throws FrameGrabber.Exception {
         SVM svm = SVM.load(GetResource.class.getClassLoader().getResource("trainneddata/pubg.xml").getPath());
         VideoProcessResult result = new VideoProcessResult();
         OpenCVFrameConverter.ToMat converterToMat = new OpenCVFrameConverter.ToMat();
@@ -199,6 +118,58 @@ public class VideoProcessToolNew {
         return result.isValid() ? result : null;
     }
 
+    public VideoProcessResult getKill3Time(String path, int frameSkip) throws FrameGrabber.Exception {
+        SVM svm = SVM.load(GetResource.class.getClassLoader().getResource("trainneddata/pubg.xml").getPath());
+        VideoProcessResult result = new VideoProcessResult();
+        OpenCVFrameConverter.ToMat converterToMat = new OpenCVFrameConverter.ToMat();
+        long frameCount = 0;
+        FrameGrabber videoGrabber = new FFmpegFrameGrabber(path);
+        videoGrabber.start();
+        Frame vFrame;
+        List<long[]> slices = toSlices(videoGrabber.getLengthInTime());
+        for (long[] slice : slices) {
+            long start = slice[0];
+            long end = slice[1];
+            long ts = start;
+            videoGrabber.setTimestamp(start);
+            while (ts <= end) {
+                vFrame = videoGrabber.grabFrame();
+                if (vFrame != null) {
+                    ts = vFrame.timestamp;
+                    frameCount++;
+                    if (frameCount % 200 == 0) {
+                        System.gc();
+                    }
+                    if (frameSkip > 0 && frameCount % frameSkip != 0) {
+                        continue;
+                    }
+                    Mat frame = removeBlackBarAndRotate(converterToMat.convert(vFrame));
+                    Mat filtered = new Mat();
+                    bilateralFilter(frame, filtered, 25, 25 * 2, 25 / 2f);
+                    if (getKillNum(filtered) >= 3) {
+                        String filename = "/var/samples/" + DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + RandomStringUtils.randomNumeric(8) + ".jpg";
+                        org.bytedeco.javacpp.opencv_imgcodecs.imwrite(filename, frame);
+                        result.setImage(uploadImage(frame));
+                        result.setKill3time(ts);
+                        break;
+                    }
+                    frame.release();
+                    System.out.println(frameCount + " frame processed");
+                } else {
+                    break;
+                }
+            }
+            if (result.getKill3time() != null) {
+                break;
+            }
+        }
+        videoGrabber.stop();
+        videoGrabber.release();
+        svm.deallocate();
+        System.gc();
+        return result;
+    }
+
     private List<long[]> toSlices(Long total) {
         List<long[]> list = new ArrayList<>();
         long end = total;

+ 0 - 7
src/main/java/com/izouma/awesomeadmin/web/VideoRecognitionController.java

@@ -17,13 +17,6 @@ public class VideoRecognitionController {
     @Autowired
     private VideoRecognitionService videoRecognitionService;
 
-    @RequestMapping("/start")
-    @ResponseBody
-    public Result start(@RequestParam("videoPath") String videoPath) {
-        boolean success = videoRecognitionService.start(videoPath);
-        return new Result(success, null);
-    }
-
     @RequestMapping("/stat")
     @ResponseBody
     public Result stat(@RequestParam("videoPath") String videoPath) {

+ 1 - 1
src/test/java/VideoProcessTest.java

@@ -28,7 +28,7 @@ public class VideoProcessTest {
     public void testVideoNew() {
         try {
             long ts = System.currentTimeMillis();
-            VideoProcessResult result = videoProcessToolNew.processVideo1("/Users/drew/Downloads/2019-07-09-04-29-14-hacic6mw.mp4", 0);
+            VideoProcessResult result = videoProcessToolNew.getKill3Time("/Users/drew/Downloads/2019-08-12-05-26-16-6j9l2tpm.mp4", 0);
             System.out.println(result);
         } catch (FrameGrabber.Exception e) {
             e.printStackTrace();