| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package com.izouma.awesomeadmin.service.impl;
- import com.izouma.awesomeadmin.constant.AppConstant;
- import com.izouma.awesomeadmin.model.PlayerInfo;
- import com.izouma.awesomeadmin.service.PlayerInfoService;
- import com.izouma.awesomeadmin.service.VideoRecognitionService;
- import com.izouma.awesomeadmin.util.VideoProcessToolNew;
- import org.bytedeco.javacv.FrameGrabber;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.concurrent.LinkedBlockingQueue;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- @Service
- public class VideoRecognitionServiceImpl implements VideoRecognitionService {
- @Autowired
- private PlayerInfoService playerInfoService;
- private VideoProcessToolNew videoProcessTool = new VideoProcessToolNew();
- public static boolean DEBUG = false;
- private static Map<String, Map<String, String>> resultMap = new HashMap<>();
- private static Map<String, Long> lastStat = new HashMap<>();
- private static Map<String, Object> processing = new HashMap<>();
- 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 {
- Map<String, String> map = videoProcessTool.processVideo(playerInfo.getVideo(), 0);
- playerInfo.setImage(map.get("image"));
- playerInfo.setLiveTime(map.get("参赛时间"));
- int rank = Integer.valueOf(map.get("rank").replace("第", ""));
- if (rank <= 0 || rank > 100) {
- throw new Exception();
- }
- try {
- playerInfo.setRanking(Integer.valueOf(map.get("rank").replace("第", "")));
- } catch (Exception ignore) {
- }
- try {
- playerInfo.setScore(Float.parseFloat(map.get("评分")));
- } catch (Exception ignore) {
- }
- try {
- playerInfo.setKillNumber(Integer.parseInt(map.get("淘汰")));
- } catch (Exception ignore) {
- }
- try {
- playerInfo.setScore(Float.parseFloat(map.get("淘汰")));
- } catch (Exception ignore) {
- }
- playerInfo.setStatusFlag(AppConstant.PlayerStatus.PROCESSED);
- } catch (Exception e) {
- e.printStackTrace();
- playerInfo.setStatusFlag(AppConstant.PlayerStatus.PROCESSED_FAIL);
- }
- playerInfoService.updatePlayerInfo(playerInfo);
- });
- }
- @Override
- public Map<String, String> stat(String videoPath) {
- lastStat.put(videoPath, System.currentTimeMillis());
- return resultMap.get(videoPath);
- }
- }
|