From 7738b2fdb21b31fff9ba770e23f45fad18d8a1b6 Mon Sep 17 00:00:00 2001 From: sulv Date: Thu, 10 Oct 2024 23:35:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/ly/VideoInferenceApp.java | 35 ++++++++++--- .../java/com/ly/model_load/ModelManager.java | 6 +++ .../java/com/ly/play/opencv/VideoPlayer.java | 51 +++++++++++++++++++ 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/ly/VideoInferenceApp.java b/src/main/java/com/ly/VideoInferenceApp.java index 3ff8d95..98b03d4 100644 --- a/src/main/java/com/ly/VideoInferenceApp.java +++ b/src/main/java/com/ly/VideoInferenceApp.java @@ -106,11 +106,15 @@ public class VideoInferenceApp extends JFrame { controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 5)); + JButton recognizeButton = new JButton("识别图片"); + recognizeButton.setPreferredSize(new Dimension(120, 30)); + controlPanel.add(recognizeButton); + // 创建控制按钮 JButton playButton = new JButton("播放"); JButton pauseButton = new JButton("暂停"); JButton replayButton = new JButton("重播"); - JButton fastForward5sButton = new JButton("快进5秒"); + JButton fastForward5sButton = new JButton("快进1秒"); JButton rewind5sButton = new JButton("后退5秒"); // 设置按钮的统一高度 @@ -212,19 +216,32 @@ public class VideoInferenceApp extends JFrame { // 重播按钮 replayButton.addActionListener(e -> { try { -// videoPlayer.loadVideo(videoPlayer.getCurrentVideoPath()); - videoPlayer.playVideo(); + videoPlayer.replayVideo(); // 调用重播方法 } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "重播视频失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); } }); -// // 后退5秒 -// rewind5sButton.addActionListener(e -> videoPlayer.rewind(5000)); -// -// // 快进5秒 -// fastForward5sButton.addActionListener(e -> videoPlayer.fastForward(5000)); + // 后退5秒 + rewind5sButton.addActionListener(e -> { + try { + videoPlayer.rewind(5000); // 后退5000毫秒(5秒) + } catch (Exception ex) { + ex.printStackTrace(); + JOptionPane.showMessageDialog(this, "后退失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); + } + }); + + // 快进5秒 + fastForward5sButton.addActionListener(e -> { + try { + videoPlayer.fastForward(1000); // 快进5000毫秒(5秒) + } catch (Exception ex) { + ex.printStackTrace(); + JOptionPane.showMessageDialog(this, "快进失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); + } + }); // 开始播放按钮的行为 startPlayButton.addActionListener(e -> { @@ -264,6 +281,7 @@ public class VideoInferenceApp extends JFrame { } } + // 选择图片文件 // 选择图片文件 private void selectImageFile() { File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory(); @@ -279,6 +297,7 @@ public class VideoInferenceApp extends JFrame { File selectedFile = fileChooser.getSelectedFile(); try { videoPlayer.loadImage(selectedFile.getAbsolutePath()); + JOptionPane.showMessageDialog(this, "图片加载成功,请点击“识别图片”按钮进行识别。", "提示", JOptionPane.INFORMATION_MESSAGE); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "加载图片失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); diff --git a/src/main/java/com/ly/model_load/ModelManager.java b/src/main/java/com/ly/model_load/ModelManager.java index 3f36daa..d67b114 100644 --- a/src/main/java/com/ly/model_load/ModelManager.java +++ b/src/main/java/com/ly/model_load/ModelManager.java @@ -66,9 +66,15 @@ public class ModelManager extends JPanel { deleteMenuItem.addActionListener(e -> { int selectedIndex = modelList.getSelectedIndex(); if (selectedIndex != -1) { + ModelInfo modelInfo = modelListModel.getElementAt(selectedIndex); // 获取要删除的模型信息 int confirmation = JOptionPane.showConfirmDialog(null, "确定要删除此模型吗?", "确认删除", JOptionPane.YES_NO_OPTION); if (confirmation == JOptionPane.YES_OPTION) { modelListModel.remove(selectedIndex); // 删除选中的模型 + + // 从 VideoPlayer 中移除对应的推理引擎 + if (videoPlayer != null) { + videoPlayer.removeInferenceEngine(modelInfo.getModelFilePath()); + } } } }); diff --git a/src/main/java/com/ly/play/opencv/VideoPlayer.java b/src/main/java/com/ly/play/opencv/VideoPlayer.java index d8b6455..35e516c 100644 --- a/src/main/java/com/ly/play/opencv/VideoPlayer.java +++ b/src/main/java/com/ly/play/opencv/VideoPlayer.java @@ -59,6 +59,8 @@ public class VideoPlayer { this.modelManager = modelManager; } + + // 加载视频或流 public void loadVideo(String videoFilePathOrStreamUrl) throws Exception { stopVideo(); @@ -376,12 +378,61 @@ public class VideoPlayer { frameDataQueue.clear(); } + // 删除推理引擎 + public void removeInferenceEngine(String modelPath) { + inferenceEngines.removeIf(engine -> engine.getModelPath().equals(modelPath)); + } + public void addInferenceEngines(InferenceEngine inferenceEngine) { this.inferenceEngines.add(inferenceEngine); } + // 添加重播方法 + // 添加重播方法 + public void replayVideo() throws Exception { + if (videoCapture != null && videoCapture.isOpened()) { + boolean success = videoCapture.set(Videoio.CAP_PROP_POS_FRAMES, 0); + if (!success) { + throw new Exception("无法重置视频到起始位置。"); + } + currentTimestamp = 0; + } else { + throw new Exception("视频未加载或未打开。"); + } + } + + // 添加 rewind 和 fastForward 方法 + public void rewind(long millis) throws Exception { + if (videoCapture == null || !videoCapture.isOpened()) { + throw new Exception("视频未加载或未打开"); + } + long newTimestamp = currentTimestamp - millis; + if (newTimestamp < 0) newTimestamp = 0; + boolean success = videoCapture.set(Videoio.CAP_PROP_POS_MSEC, newTimestamp); + if (!success) { + throw new Exception("无法后退视频。"); + } + currentTimestamp = newTimestamp; + } + + public synchronized void fastForward(long millis) throws Exception { + if (videoCapture == null || !videoCapture.isOpened()) { + throw new Exception("视频未加载或未打开"); + } + long newTimestamp = currentTimestamp + millis; + if (videoDuration > 0 && newTimestamp > videoDuration) { + newTimestamp = videoDuration; + } + boolean success = videoCapture.set(Videoio.CAP_PROP_POS_MSEC, newTimestamp); + if (!success) { + throw new Exception("无法快进视频。"); + } + currentTimestamp = newTimestamp; + } + // 加载并处理图片 public void loadImage(String imagePath) throws Exception { + // 停止任何正在播放的视频 stopVideo();