VideoRecognitionServiceImpl.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.izouma.awesomeadmin.service.impl;
  2. import com.izouma.awesomeadmin.constant.AppConstant;
  3. import com.izouma.awesomeadmin.model.PlayerInfo;
  4. import com.izouma.awesomeadmin.service.PlayerInfoService;
  5. import com.izouma.awesomeadmin.service.VideoRecognitionService;
  6. import com.izouma.awesomeadmin.util.VideoProcessToolNew;
  7. import org.bytedeco.javacv.FrameGrabber;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. import java.util.concurrent.LinkedBlockingQueue;
  13. import java.util.concurrent.ThreadPoolExecutor;
  14. import java.util.concurrent.TimeUnit;
  15. @Service
  16. public class VideoRecognitionServiceImpl implements VideoRecognitionService {
  17. @Autowired
  18. private PlayerInfoService playerInfoService;
  19. private VideoProcessToolNew videoProcessTool = new VideoProcessToolNew();
  20. public static boolean DEBUG = false;
  21. private static Map<String, Map<String, String>> resultMap = new HashMap<>();
  22. private static Map<String, Long> lastStat = new HashMap<>();
  23. private static Map<String, Object> processing = new HashMap<>();
  24. private ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
  25. @Override
  26. public boolean start(String videoPath) {
  27. if (processing.get(videoPath) != null) {
  28. return true;
  29. }
  30. processing.put(videoPath, videoPath);
  31. lastStat.put(videoPath, System.currentTimeMillis());
  32. resultMap.remove(videoPath);
  33. executor.execute(() -> {
  34. try {
  35. Map<String, String> map = videoProcessTool.processVideo(videoPath, 0);
  36. if (map != null) {
  37. resultMap.put(videoPath, map);
  38. }
  39. } catch (FrameGrabber.Exception e) {
  40. e.printStackTrace();
  41. }
  42. processing.remove(videoPath);
  43. });
  44. return true;
  45. }
  46. @Override
  47. public void start(PlayerInfo playerInfo) {
  48. // if (playerInfo.getStatusFlag() > AppConstant.PlayerStatus.END) {
  49. // return;
  50. // }
  51. executor.execute(() -> {
  52. try {
  53. Map<String, String> map = videoProcessTool.processVideo(playerInfo.getVideo(), 0);
  54. playerInfo.setImage(map.get("image"));
  55. playerInfo.setLiveTime(map.get("参赛时间"));
  56. int rank = Integer.valueOf(map.get("rank").replace("第", ""));
  57. if (rank <= 0 || rank > 100) {
  58. throw new Exception();
  59. }
  60. try {
  61. playerInfo.setRanking(Integer.valueOf(map.get("rank").replace("第", "")));
  62. } catch (Exception ignore) {
  63. }
  64. try {
  65. playerInfo.setScore(Float.parseFloat(map.get("评分")));
  66. } catch (Exception ignore) {
  67. }
  68. try {
  69. playerInfo.setKillNumber(Integer.parseInt(map.get("淘汰")));
  70. } catch (Exception ignore) {
  71. }
  72. try {
  73. playerInfo.setScore(Float.parseFloat(map.get("淘汰")));
  74. } catch (Exception ignore) {
  75. }
  76. playerInfo.setStatusFlag(AppConstant.PlayerStatus.PROCESSED);
  77. } catch (Exception e) {
  78. e.printStackTrace();
  79. playerInfo.setStatusFlag(AppConstant.PlayerStatus.PROCESSED_FAIL);
  80. }
  81. playerInfoService.updatePlayerInfo(playerInfo);
  82. });
  83. }
  84. @Override
  85. public Map<String, String> stat(String videoPath) {
  86. lastStat.put(videoPath, System.currentTimeMillis());
  87. return resultMap.get(videoPath);
  88. }
  89. }