作者 钟来

模块整理

for /L %i IN (1,1,254) DO ping -w 2 -n 1 192.168.0.%i
arp -a
\ No newline at end of file
... ...
... ... @@ -42,13 +42,11 @@
<artifactId>lh-common-swagger</artifactId>
</dependency>
<!-- OpenCV -->
<!-- https://mvnrepository.com/artifact/org.bytedeco/opencv -->
<dependency>
<groupId>org</groupId>
<groupId>org.bytedeco</groupId>
<artifactId>opencv</artifactId>
<version>4.5.1</version>
<scope>system</scope>
<systemPath>${project.basedir}\src\main\resources\lib\opencv-451.jar</systemPath>
<version>4.5.1-1.5.5</version>
</dependency>
<!-- 视频处理库 -->
... ... @@ -56,7 +54,7 @@
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.4</version>
<version>1.5.5</version>
</dependency>
<!-- WebSocket -->
... ...
... ... @@ -14,6 +14,16 @@ public class VeiwDto implements Serializable {
private Boolean isText;
private Boolean displaySrc;
public Boolean getDisplaySrc() {
return displaySrc;
}
public void setDisplaySrc(Boolean displaySrc) {
this.displaySrc = displaySrc;
}
public Boolean getText() {
return isText;
}
... ...
package com.zhonglai.luhui.smart.feeder.opencv;
import com.ruoyi.common.utils.DESUtil;
import com.ruoyi.common.utils.StringUtils;
import com.zhonglai.luhui.smart.feeder.config.OpenCVConfig;
import com.zhonglai.luhui.smart.feeder.dto.VeiwDto;
import com.zhonglai.luhui.smart.feeder.service.CameraService;
import com.zhonglai.luhui.smart.feeder.service.FFmCameraService;
import com.zhonglai.luhui.smart.feeder.service.impl.HtmllVeiwServiceImpl;
import com.zhonglai.luhui.smart.feeder.service.impl.JFrameVeiwServiceImpl;
import org.bytedeco.javacv.FrameGrabber;
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class OpenCVUtil {
... ... @@ -29,13 +21,14 @@ public class OpenCVUtil {
private static final Logger logger = LoggerFactory.getLogger(OpenCVUtil.class);
public static void main(String[] args) {
System.out.println(DESUtil.decode("5F06AAC657B2E2B287289D25D950A829", "EXU5RUhI1"));;
}
public static VideoCapture readVideoCaptureForRtsp()
{
FFmCameraService fFmCameraService = new FFmCameraService();
String ip = fFmCameraService.findCameraIp();
// FFmCameraService fFmCameraService = new FFmCameraService();
// String ip = fFmCameraService.findCameraIp();
String ip = "192.168.0.198";
if(StringUtils.isEmpty(ip))
{
logger.info("未检测到摄像头{},尝试打开本地视频",MP4_FILE_PATH);
... ...
... ... @@ -71,13 +71,14 @@ public class CameraService {
if(null == scheduledFuture || scheduledFuture.isDone())
{
scheduledFuture = scheduledExecutorService.scheduleWithFixedDelay(() -> {
logger.info("摄像头状态{}",videoIsOpen);
if(!videoIsOpen)
{
openCapture();
}
},0,1, TimeUnit.SECONDS);
}
logger.info("启动摄像头");
logger.info("启动摄像头{}");
}
/**
... ...
package com.zhonglai.luhui.smart.feeder.service;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.RuntimeUtil;
import com.ruoyi.common.utils.StringUtils;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.*;
import org.bytedeco.javacv.Frame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.awt.*;
... ... @@ -14,35 +14,27 @@ import java.awt.image.BufferedImage;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
@Service
public class FFmCameraService {
private static final Logger logger = LoggerFactory.getLogger(FFmCameraService.class);
private FFmpegFrameGrabber grabber;
private OpenCVFrameConverter.ToOrgOpenCvCoreMat converter2 = new OpenCVFrameConverter.ToOrgOpenCvCoreMat();
private static String MAC = "78-a6-a0-d2-bd-e1";
/**
* 查找摄像头
* @return
*/
public String findCameraIp()
{
List<String> list = RuntimeUtil.execForLines("arp", "-a");
for (String str:list)
{
if(str.indexOf(MAC)>=0)
{
// 使用Hutool提取字符串中的IP地址
List<String> ips = ReUtil.findAll("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}", str, 0, new ArrayList<>());
if(null != ips && ips.size()!=0)
public boolean getVideoIsOpen()
{
return ips.get(0);
}
}
return !grabber.isDeinterlace();
}
return null;
public void start()
{
loadCamera();
}
/**
... ... @@ -50,7 +42,7 @@ public class FFmCameraService {
*/
public void loadCamera()
{
String ip = findCameraIp();
String ip = "192.168.0.198";
if(StringUtils.isEmpty(ip))
{
System.out.println("没有找到摄像头:"+MAC);
... ... @@ -113,6 +105,16 @@ public class FFmCameraService {
}
}
public org.opencv.core.Mat getMat()
{
Frame frame = getFrame();
if(null == frame)
{
return null;
}
return converter2.convert(frame);
}
public Frame getFrame() {
Frame frame = null;
try {
... ...
... ... @@ -5,16 +5,11 @@ import com.zhonglai.luhui.smart.feeder.dto.ConfigurationParameter;
import com.zhonglai.luhui.smart.feeder.dto.VeiwDto;
import com.zhonglai.luhui.smart.feeder.opencv.OpenCVUtil;
import com.zhonglai.luhui.smart.feeder.service.impl.HtmllVeiwServiceImpl;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameUtils;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.opencv.opencv_core.IplImage;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -34,7 +29,7 @@ public class FishGroupImageRecognitionService {
private static final Logger logger = LoggerFactory.getLogger(FishGroupImageRecognitionService.class);
@Autowired
private CameraService cameraService;
private FFmCameraService fFmCameraService;
@Autowired
private ScheduledExecutorService scheduledExecutorService;
... ... @@ -69,19 +64,18 @@ public class FishGroupImageRecognitionService {
// 创建FrameConverter对象
public void start()
{
if(cameraService.getVideoIsOpen()) //摄像头打开才能识别
if(fFmCameraService.getVideoIsOpen()) //摄像头打开才能识别
{
isRun = true;
configurationParameterService.setConfig(ConfigurationParameter.FishGroupImageRecognition,true);
VideoCapture videoCapture = cameraService.getVideoCapture();
brightnessIdentifyFishRegion(videoCapture);
brightnessIdentifyFishRegion();
}
}
public void stop()
{
configurationParameterService.setConfig(ConfigurationParameter.FishGroupImageRecognition,false);
cameraService.close();
fFmCameraService.close();
isRun = false;
}
... ... @@ -124,15 +118,17 @@ public class FishGroupImageRecognitionService {
/**
* 亮度查找水面,透明度过滤鱼群
*/
private void brightnessIdentifyFishRegion(VideoCapture videoCapture )
private void brightnessIdentifyFishRegion()
{
logger.info("启动鱼群识别");
// 读取第一帧并获取视频大小
Mat previousFrame = new Mat();
if (!videoCapture.read(previousFrame)) {
org.opencv.core.Mat previousFrame = fFmCameraService.getMat();
if (null == previousFrame) {
System.out.println("无法读取视频帧");
return;
}
logger.info("鱼群识别时检测摄像头");
// 获取水域轮廓
if(null != largestContour)
... ... @@ -150,17 +146,16 @@ public class FishGroupImageRecognitionService {
scheduledExecutorService.scheduleWithFixedDelay(() -> {
try {
Boolean fishGroupImageRecognition = ((Boolean)configurationParameterService.getConfig(ConfigurationParameter.FishGroupImageRecognition));
Mat frame = new Mat();
Boolean isread = videoCapture.read(frame);
Mat frame = fFmCameraService.getMat();
Boolean isread = false;
if(null != frame)
{
isread = true;
}
logger.info("逐帧处理视频,开始处理的判断参数:鱼群图像识别是否开启 {}、摄像头是否可读取 {}",fishGroupImageRecognition,isread);
if(!isread )
{
logger.info("摄像头不可读取");
if(cameraService.getVideoIsOpen())
{
logger.info("重新初始化");
cameraService.clean();
}
frame.release();
return;
}
... ...
... ... @@ -10,7 +10,7 @@ import java.util.concurrent.ScheduledExecutorService;
@Configuration
public class InitService {
@Autowired
private CameraService cameraService;
private FFmCameraService fFmCameraService;
@Autowired
private ConfigurationParameterService configurationParameterService;
... ... @@ -40,7 +40,7 @@ public class InitService {
//配置参数
configurationParameterService.initConfigurationParameter();
//摄像头监听
cameraService.start();
fFmCameraService.start();
//鱼群识别
fishGroupImageRecognitionService.run();
//鱼群图像识别控制投料控制
... ...
... ... @@ -126,6 +126,7 @@ public class MqttCallback implements MqttCallbackExtended {
case 1:
srsService.run(300);
fishGroupImageRecognitionService.setText(veiwDto.getText());
srsService.setDisplaySrc(veiwDto.getDisplaySrc());
break;
}
}
... ...
... ... @@ -51,8 +51,17 @@ public class SrsService {
private boolean isPush = false;
private boolean displaySrc = false;
private int endTime = 0;
public boolean isDisplaySrc() {
return displaySrc;
}
public void setDisplaySrc(boolean displaySrc) {
this.displaySrc = displaySrc;
}
public void run(int time) throws Exception {
endTime = DateUtils.getNowTimeMilly()+time;
if(!isPush)
... ... @@ -147,7 +156,12 @@ public class SrsService {
// 将curveArea拼接到原来的Mat对象上
Mat pushmat = new Mat();
List<Mat> mats = new ArrayList<>();
if(displaySrc)
{
mats.add(src);
}
mats.add(curveArea);
Core.vconcat(mats, pushmat);
... ...