作者 钟来

模块整理

package com.zhonglai.luhui.smart.feeder.config.manager;
import com.zhonglai.luhui.smart.feeder.service.DeviceService;
import com.zhonglai.luhui.smart.feeder.service.SrsService;
import com.zhonglai.luhui.smart.feeder.service.EhCacheService;
import com.zhonglai.luhui.smart.feeder.service.SerialPortService;
import com.zhonglai.luhui.smart.feeder.service.TerminalService;
... ... @@ -29,6 +29,9 @@ public class ShutdownManager
@Autowired
private TerminalService terminalService;
@Autowired
private SrsService srsService;
@PreDestroy
public void destroy()
... ... @@ -36,6 +39,7 @@ public class ShutdownManager
terminalService.close();
serialPortService.close();
ehCacheService.shutdown();
srsService.stop();
shutdownAsyncManager();
}
... ...
... ... @@ -7,13 +7,21 @@ import java.io.Serializable;
public class VeiwDto implements Serializable {
private static final long serialVersionUID = -6059514681715227839L;
private Mat frame; //原始图像
private Mat binaryImage; //临时图像
private Integer size; //面积大小
private Double absValue; //变化的斜率
private Integer push_camera;
private Boolean isText;
public Boolean getText() {
return isText;
}
public void setText(Boolean text) {
isText = text;
}
public Integer getPush_camera() {
return push_camera;
}
... ... @@ -22,20 +30,11 @@ public class VeiwDto implements Serializable {
this.push_camera = push_camera;
}
public VeiwDto(Mat frame, Integer size) {
this.frame = frame;
this.size = size;
}
public VeiwDto(Mat frame, Mat binaryImage, Integer size) {
this.frame = frame;
this.binaryImage = binaryImage;
public VeiwDto(Integer size) {
this.size = size;
}
public VeiwDto(Mat frame, Mat binaryImage, Integer size, Double absValue) {
this.frame = frame;
this.binaryImage = binaryImage;
public VeiwDto( Integer size, Double absValue) {
this.size = size;
this.absValue = absValue;
}
... ... @@ -51,22 +50,6 @@ public class VeiwDto implements Serializable {
public VeiwDto() {
}
public Mat getFrame() {
return frame;
}
public void setFrame(Mat frame) {
this.frame = frame;
}
public Mat getBinaryImage() {
return binaryImage;
}
public void setBinaryImage(Mat binaryImage) {
this.binaryImage = binaryImage;
}
public Integer getSize() {
return size;
}
... ...
... ... @@ -98,6 +98,8 @@ public class OpenCVUtil {
Mat extractedImage = new Mat();
Core.bitwise_and(frame, extractedRegion, extractedImage);
extractedRegion.release(); //释放内存
return extractedImage;
}
... ...
package com.zhonglai.luhui.smart.feeder.service;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
public class FFmCameraService {
public static void main(String[] args) {
// int maxCameraIndex = 10; // 最大尝试的摄像头索引数量
// for (int cameraIndex = 0; cameraIndex < maxCameraIndex; ++cameraIndex) {
// try {
// FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(cameraIndex);
// grabber.start(); // 尝试开始,如果失败将抛出异常
// try {
// // 创建一个窗口用于显示摄像头的视频流
// CanvasFrame frame = new CanvasFrame("Webcam");
//
// // 判断窗口是否关闭
// while (frame.isVisible()) {
// // 抓取一帧视频并将其放在窗口上显示,该操作会阻塞程序,直到下一帧视频可用
// Frame grabbedFrame = grabber.grab();
// frame.showImage(grabbedFrame);
// }
//
// // 关闭窗口
// frame.dispose();
// } finally {
// // 停止抓取
// grabber.stop();
// }
// break; // 如果找到了摄像头并成功打开,就结束循环
// } catch (FrameGrabber.Exception e) {
// // 如果失败,就继续尝试下一个摄像头
// System.out.println("Failed to start the camera with device index: " + cameraIndex);
// }
// }
}
}
... ...
... ... @@ -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);
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);
//标注识别对象
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 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;
}
... ...
... ... @@ -35,6 +35,9 @@ public class MqttCallback implements MqttCallbackExtended {
@Autowired
private SrsService srsService;
@Autowired
private FishGroupImageRecognitionService fishGroupImageRecognitionService;
private MqttClient mqttclient;
... ... @@ -122,6 +125,7 @@ public class MqttCallback implements MqttCallbackExtended {
break;
case 1:
srsService.run(300);
fishGroupImageRecognitionService.setText(veiwDto.getText());
break;
}
}
... ...
... ... @@ -57,16 +57,17 @@ public class SrsService {
endTime = DateUtils.getNowTimeMilly()+time;
if(!isPush)
{
VeiwDto veiwDto = fishGroupImageRecognitionService.getVeiwDto();
init(veiwDto.getFrame().width(),veiwDto.getFrame().height()+200);
scheduledExecutorService.schedule(() -> {
while (endTime-DateUtils.getNowTimeMilly()>0)
{
Mat mat = drawChart(fishGroupImageRecognitionService.getVeiwDto().getFrame(),fishGroupImageRecognitionService.getVeiwDto().getAbsValue());
push(mat);
}
stop();
},1, TimeUnit.SECONDS);
isPush = true;
init(640,480+200);
// scheduledExecutorService.schedule(() -> {
// while (endTime-DateUtils.getNowTimeMilly()>0)
// {
// Mat mat = drawChart(fishGroupImageRecognitionService.getVeiwDto().getFrame(),fishGroupImageRecognitionService.getVeiwDto().getAbsValue());
// push(mat);
// mat.release();
// }
// stop();
// },1, TimeUnit.SECONDS);
}
}
public void stop()
... ... @@ -76,6 +77,21 @@ public class SrsService {
close();
}
public void timepush(Mat frame,Double absValue)
{
if(isPush)
{
if(endTime-DateUtils.getNowTimeMilly()>0)
{
Mat mat = drawChart(frame,absValue);
push(mat);
mat.release();
}else {
stop();
}
}
}
public SrsService init(int frameWidth,int frameHeight) throws Exception
{
... ... @@ -134,6 +150,21 @@ public class SrsService {
mats.add(src);
mats.add(curveArea);
Core.vconcat(mats, pushmat);
// 文字位置
Point org = new Point(50, 50);
// 字体类型
int fontFace = Imgproc.FONT_HERSHEY_SIMPLEX;
// 字体比例
double fontScale = 1;
// 文字颜色(黑色)
Scalar color = new Scalar(139, 0, 139);
// 添加文字
Imgproc.putText(pushmat, "area:"+area, org, fontFace, fontScale, color);
curveArea.release();
curve.release();
return pushmat;
}
... ... @@ -141,6 +172,7 @@ public class SrsService {
OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
Frame frame = converter.convert(pushmat);
push(frame);
pushmat.release();
}
public void push(Frame frame) {
... ...
... ... @@ -48,8 +48,10 @@ public class HtmllVeiwServiceImpl implements DisplayVeiwService {
bufImage = ImageIO.read(in);
in.close();
} catch (Exception e) {
mob.release();
e.printStackTrace();
}
mob.release();
return bufImage;
}
... ... @@ -133,11 +135,11 @@ public class HtmllVeiwServiceImpl implements DisplayVeiwService {
if((boolean)configurationParameterService.getConfig(ConfigurationParameter.VeiwDto_isFrame))
{
webSocketVO.setFrame(matToString(veiwDto.getFrame(),"jpg"));
// webSocketVO.setFrame(matToString(veiwDto.getFrame(),"jpg"));
}
if((boolean)configurationParameterService.getConfig(ConfigurationParameter.VeiwDto_isBinaryImage))
{
webSocketVO.setBinaryImage(matToString(veiwDto.getBinaryImage(),"jpg"));
// webSocketVO.setBinaryImage(matToString(veiwDto.getBinaryImage(),"jpg"));
}
if((boolean)configurationParameterService.getConfig(ConfigurationParameter.VeiwDto_isSize))
{
... ... @@ -151,7 +153,7 @@ public class HtmllVeiwServiceImpl implements DisplayVeiwService {
{
webSocketSever.sendWebSocketVO(webSocketVO);
}
} catch (IOException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
... ...
... ... @@ -40,11 +40,11 @@ public class JFrameVeiwServiceImpl implements DisplayVeiwService {
if((boolean)configurationParameterService.getConfig(ConfigurationParameter.VeiwDto_isBinaryImage))
{
fishRegionPanel.getLblImage().setIcon(new ImageIcon(convertMatToImage(veiwDto.getBinaryImage())));
// fishRegionPanel.getLblImage().setIcon(new ImageIcon(convertMatToImage(veiwDto.getBinaryImage())));
}
if((boolean)configurationParameterService.getConfig(ConfigurationParameter.VeiwDto_isFrame))
{
fishRegionPanel.getSrcImage().setIcon(new ImageIcon(convertMatToImage(veiwDto.getFrame())));
// fishRegionPanel.getSrcImage().setIcon(new ImageIcon(convertMatToImage(veiwDto.getFrame())));
}
if((boolean)configurationParameterService.getConfig(ConfigurationParameter.VeiwDto_isSize))
{
... ...