|
...
|
...
|
@@ -6,6 +6,8 @@ import com.zhonglai.luhui.smart.feeder.dto.VeiwDto; |
|
|
|
import com.zhonglai.luhui.smart.feeder.dto.VeiwType;
|
|
|
|
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.OpenCVFrameConverter;
|
|
|
|
import org.opencv.core.Mat;
|
|
|
|
import org.opencv.core.MatOfPoint;
|
|
|
|
import org.opencv.core.Scalar;
|
|
...
|
...
|
@@ -42,10 +44,17 @@ public class FishGroupImageRecognitionService { |
|
|
|
@Autowired
|
|
|
|
private DeviceService deviceService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private SrsService srsService;
|
|
|
|
|
|
|
|
private static Boolean isRun = false;
|
|
|
|
|
|
|
|
private VeiwDto veiwDto;
|
|
|
|
|
|
|
|
private Boolean isText = false;
|
|
|
|
|
|
|
|
private MatOfPoint largestContour;
|
|
|
|
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
scheduledExecutorService.scheduleWithFixedDelay(() -> {
|
|
...
|
...
|
@@ -91,14 +100,21 @@ public class FishGroupImageRecognitionService { |
|
|
|
double maxArea = 0;
|
|
|
|
int maxAreaIndex = -1;
|
|
|
|
for (int i = 0; i < contours.size(); i++) {
|
|
|
|
double area = Imgproc.contourArea(contours.get(i));
|
|
|
|
MatOfPoint matOfPoint = contours.get(i);
|
|
|
|
double area = Imgproc.contourArea(matOfPoint);
|
|
|
|
if (area > maxArea) {
|
|
|
|
maxArea = area;
|
|
|
|
maxAreaIndex = i;
|
|
|
|
}else{
|
|
|
|
matOfPoint.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 获取最大区域的轮廓
|
|
|
|
MatOfPoint largestContour = contours.get(maxAreaIndex);
|
|
|
|
|
|
|
|
firstBinaryImage.release();
|
|
|
|
hierarchy.release();
|
|
|
|
|
|
|
|
return largestContour;
|
|
|
|
}
|
|
|
|
|
|
...
|
...
|
@@ -116,7 +132,11 @@ public class FishGroupImageRecognitionService { |
|
|
|
}
|
|
|
|
logger.info("鱼群识别时检测摄像头");
|
|
|
|
// 获取水域轮廓
|
|
|
|
MatOfPoint largestContour = getDefaultMatOfPoint(previousFrame);
|
|
|
|
if(null != largestContour)
|
|
|
|
{
|
|
|
|
largestContour.release();
|
|
|
|
}
|
|
|
|
largestContour = getDefaultMatOfPoint(previousFrame);
|
|
|
|
|
|
|
|
Long time =66l;
|
|
|
|
if(null != configurationParameterService.getConfig(ConfigurationParameter.IdentificationFrequency))
|
|
...
|
...
|
@@ -124,10 +144,10 @@ public class FishGroupImageRecognitionService { |
|
|
|
time = (Long) configurationParameterService.getConfig(ConfigurationParameter.IdentificationFrequency);
|
|
|
|
}
|
|
|
|
// 逐帧处理视频
|
|
|
|
Mat frame = new Mat();
|
|
|
|
scheduledExecutorService.scheduleWithFixedDelay(() -> {
|
|
|
|
try {
|
|
|
|
Boolean fishGroupImageRecognition = ((Boolean)configurationParameterService.getConfig(ConfigurationParameter.FishGroupImageRecognition));
|
|
|
|
Mat frame = new Mat();
|
|
|
|
Boolean isread = videoCapture.read(frame);
|
|
|
|
logger.info("逐帧处理视频,开始处理的判断参数:鱼群图像识别是否开启 {}、摄像头是否可读取 {}",fishGroupImageRecognition,isread);
|
|
|
|
if(!isread )
|
|
...
|
...
|
@@ -142,8 +162,9 @@ public class FishGroupImageRecognitionService { |
|
|
|
|
|
|
|
}
|
|
|
|
if (fishGroupImageRecognition && isread) {
|
|
|
|
identify(frame,largestContour);
|
|
|
|
identify(frame);
|
|
|
|
}
|
|
|
|
frame.release();
|
|
|
|
}catch (Exception e)
|
|
|
|
{
|
|
|
|
logger.error("识别错误",e);
|
|
...
|
...
|
@@ -151,14 +172,18 @@ public class FishGroupImageRecognitionService { |
|
|
|
|
|
|
|
},0,time, TimeUnit.MILLISECONDS);
|
|
|
|
|
|
|
|
previousFrame.release();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setText(Boolean text) {
|
|
|
|
isText = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 识别
|
|
|
|
* @param frame
|
|
|
|
* @param largestContour
|
|
|
|
*/
|
|
|
|
private void identify(Mat frame, MatOfPoint largestContour)
|
|
|
|
private void identify(Mat frame)
|
|
|
|
{
|
|
|
|
//抠图
|
|
|
|
Mat shuiyu = OpenCVUtil.matting(frame,largestContour);
|
|
...
|
...
|
@@ -177,18 +202,23 @@ public class FishGroupImageRecognitionService { |
|
|
|
// 在水域二值图像中找所有轮廓
|
|
|
|
Imgproc.findContours(binaryImage, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
|
|
|
|
|
|
|
|
//计算大小
|
|
|
|
double area = getArea(contours);
|
|
|
|
|
|
|
|
if(null != isText && isText)
|
|
|
|
{
|
|
|
|
//标注识别对象
|
|
|
|
Imgproc.drawContours(frame, contours, -1, new Scalar(0, 0, 255), 2);
|
|
|
|
Imgproc.drawContours(frame, Arrays.asList(new MatOfPoint[]{largestContour}), 0, new Scalar(0, 255, 0), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
//计算大小
|
|
|
|
double area = getArea(contours);
|
|
|
|
|
|
|
|
//计算斜率
|
|
|
|
double absValue = deviceService.controlDevice(area);
|
|
|
|
configurationParameterService.setConfig(ConfigurationParameter.absValue,absValue);
|
|
|
|
|
|
|
|
veiwDto = new VeiwDto(frame,binaryImage,new Double(area).intValue(),absValue);
|
|
|
|
veiwDto = new VeiwDto(new Double(area).intValue(),absValue);
|
|
|
|
|
|
|
|
srsService.timepush(frame,absValue);
|
|
|
|
|
|
|
|
// 显示图像
|
|
|
|
logger.info("是否显示{},客户端数量{}",configurationParameterService.getConfig(ConfigurationParameter.ifVeiw),WebSocketClien.webSocketSet.size());
|
|
...
|
...
|
@@ -198,6 +228,11 @@ public class FishGroupImageRecognitionService { |
|
|
|
{
|
|
|
|
new HtmllVeiwServiceImpl(configurationParameterService).veiw(veiwDto);
|
|
|
|
}
|
|
|
|
|
|
|
|
shuiyu.release();
|
|
|
|
gray.release();
|
|
|
|
hierarchy.release();
|
|
|
|
binaryImage.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
...
|
...
|
@@ -211,12 +246,15 @@ public class FishGroupImageRecognitionService { |
|
|
|
int maxAreaIndex = -1;
|
|
|
|
double allArea = 0;
|
|
|
|
for (int i = 0; i < contours.size(); i++) {
|
|
|
|
double area = Imgproc.contourArea(contours.get(i));
|
|
|
|
MatOfPoint matOfPoint = contours.get(i);
|
|
|
|
double area = Imgproc.contourArea(matOfPoint);
|
|
|
|
if (area > maxArea) {
|
|
|
|
maxArea = area;
|
|
|
|
maxAreaIndex = i;
|
|
|
|
}
|
|
|
|
allArea += area;
|
|
|
|
|
|
|
|
matOfPoint.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
//删除最大
|
|
...
|
...
|
@@ -249,6 +287,9 @@ public class FishGroupImageRecognitionService { |
|
|
|
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((Integer) configurationParameterService.getConfig(ConfigurationParameter.kernelSize),(Integer) configurationParameterService.getConfig(ConfigurationParameter.kernelSize)));
|
|
|
|
Imgproc.morphologyEx(binaryImage, binaryImage, Imgproc.MORPH_OPEN, kernel);
|
|
|
|
|
|
|
|
grayImage.release();
|
|
|
|
kernel.release();
|
|
|
|
|
|
|
|
return binaryImage;
|
|
|
|
}
|
|
|
|
|
...
|
...
|
|