javacv2yolov3

工具:IDEA,jdk1.8,Maven3.6
依赖:javacv
注意:运行速度没有加载opecv_java411.dll快

<dependency>
  <groupId>org.bytedeco</groupId>
  <artifactId>javacv-platform</artifactId>
  <version>1.5.2</version>
</dependency>

文字输出代码:

package org.example;

import org.bytedeco.javacpp.*;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.leptonica.*;
import org.bytedeco.librealsense.frame;
import org.bytedeco.opencv.global.opencv_core;
import org.bytedeco.opencv.global.opencv_dnn;

import org.bytedeco.opencv.opencv_dnn.*;
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_imgproc.*;
import org.bytedeco.javacpp.indexer.*;

import static org.bytedeco.leptonica.global.lept.*;
import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_dnn.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
import static org.bytedeco.opencv.global.opencv_imgcodecs.*;

import org.apache.commons.lang3.time.StopWatch;
import org.bytedeco.opencv.opencv_text.FloatVector;
import org.bytedeco.opencv.opencv_text.IntVector;
import org.opencv.core.Core;
import org.opencv.dnn.Dnn;
import org.bytedeco.javacpp.*;

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.util.*;
import java.util.concurrent.*;
import java.io.*;


public class yolo {

    public void Yolo3(){
        Mat img = imread("F:\\maven\\hot_yolov3\\src\\resources\\20190520_132858.jpg");
        String cfg = "F:\\maven\\hot_yolov3\\src\\resources\\hot-yolov3.cfg";
        String model = "F:\\maven\\hot_yolov3\\src\\resources\\hot-yolov3.weights";

        Net net = opencv_dnn.readNetFromDarknet(cfg, model);
        Mat blob = opencv_dnn.blobFromImage(img, 1/255.0, new Size(416, 416), new Scalar(0.0), true, false,CV_32F);

        net.setInput(blob);

        StringVector outNames = net.getUnconnectedOutLayersNames();
        //System.out.println(outNames.toString());

        MatVector outs = new MatVector(outNames.size());

        net.forward(outs, outNames);

        float threshold = 0.25f;
        float nmsThreshold = 0.45f;
        GetResult(outs, img, threshold, nmsThreshold,true);
        //System.out.println(outs);
    }

    private void GetResult(MatVector output, Mat image, float threshold, float nmsThreshold, boolean nms)
    {
        nms = false;
        IntVector classIds = new IntVector();
        FloatVector confidences = new FloatVector();
        RectVector boxes = new RectVector();
        try{
            for(int i=0;i<output.size();++i)
            {
                Mat result = output.get(i);
                //System.out.println(result);
                for (int j = 0; j < result.rows(); j++)
                {
                    FloatPointer data = new FloatPointer(result.row(j).data());
                    Mat scores = result.row(j).colRange(5, result.cols());

                    Point classIdPoint = new Point(1);
                    DoublePointer confidence = new DoublePointer(1);

                    minMaxLoc(scores, null, confidence, null, classIdPoint, null);

                    if (confidence.get() > threshold)
                    {
                        int centerX = (int) (data.get(0) * image.cols());
                        int centerY = (int) (data.get(1) * image.rows());
                        int width = (int) (data.get(2) * image.cols());
                        int height = (int) (data.get(3) * image.rows());
                        int left = centerX - width / 2;
                        int top = centerY - height / 2;

                        classIds.push_back(classIdPoint.x());
                        confidences.push_back((float) confidence.get());
                        boxes.push_back(new Rect(left, top, width, height));
                    }
                }
            }

//            if (nms) {
//
//                IntPointer indices = new IntPointer(confidences.size());
//                for (int i = 0; i < confidences.size(); ++i) {
//                    Rect box = boxes.get(i);
//                    int classId = classIds.get(i);
//                    String res = "idx="+classId+"conf="+confidences.get(i);
//                    res += "box.x=" + box.x() +"box.y="+box.y()+"box.width"+box.width()+"box.height"+box.height();
//                    System.out.println(res);
//                }
//            }

            IntPointer indices = new IntPointer(confidences.size());
            FloatPointer confidencesPointer = new FloatPointer(confidences.size());
            confidencesPointer.put(confidences.get());

            NMSBoxes(boxes, confidencesPointer, threshold, nmsThreshold, indices, 1.f, 0);
//            NMSBoxes(boxes, confidencesPointer, threshold, nmsThreshold, indices);

            List<String> list = new ArrayList<String>();
            FileInputStream fis = new FileInputStream("F:\\maven\\hot_yolov3\\src\\resources\\hot.names");
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {
                list.add(line);
            }
            String[] Labels = list.toArray(new String[list.size()]);
            br.close();
            isr.close();
            fis.close();

            List diode = new ArrayList<>();
            List shadow = new ArrayList<>();
            List stain = new ArrayList<>();
            //System.out.println(indices.sizeof());
            //System.out.println(indices.limit());

            for (int m=0;m< indices.limit();++m)
            {
                int i = indices.get(m);
                Rect box = boxes.get(i);
                //System.out.println(box);
                int classId = classIds.get(i);
                //System.out.println("name:"+Labels);
                //System.out.println("classid"+classId);

                if (classId==0){
                    diode.add(box.x());
                    diode.add(box.y());
                    diode.add(box.width());
                    diode.add(box.height());
                }
                if (classId==1){

                    shadow.add(box.x());
                    shadow.add(box.y());
                    shadow.add(box.width());
                    shadow.add(box.height());
                }
                if (classId==2){
                    stain.add(box.x());
                    stain.add(box.y());
                    stain.add(box.width());
                    stain.add(box.height());
                }
                String res = "idx="+classId+"  name="+Labels[classId]+"  conf="+confidences.get(i);
                res += "  box.x=" + box.x() +"  box.y="+box.y()+"  box.width="+box.width()+"  box.height="+box.height();
                System.out.println(res);
            }

            String total_diode = "diode_num:"+diode.size()/4 +"  |diode_list:"+diode;
            String total_shadow = "shadow_num:"+shadow.size()/4 +"  |shadow_list:"+shadow;
            String total_stain = "stain_num:"+stain.size()/4 +"  |stain_list:"+stain;

            System.out.println(total_diode);
            System.out.println(total_shadow);
            System.out.println(total_stain);


        }catch(Exception e){
            System.out.println("GetResult error:" + e.getMessage());
        }
    }

    public static void main(String[] args) {
        yolo be =  new yolo();
        be.Yolo3();
    }
}

图片保存代码:

package com.yolo3test;

import org.bytedeco.javacpp.*;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.leptonica.*;
import org.bytedeco.librealsense.frame;
import org.bytedeco.opencv.global.opencv_core;
import org.bytedeco.opencv.global.opencv_dnn;

import org.bytedeco.opencv.opencv_dnn.*;
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_imgproc.*;
import org.bytedeco.javacpp.indexer.*;

import static org.bytedeco.leptonica.global.lept.*;
import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_dnn.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
import static org.bytedeco.opencv.global.opencv_imgcodecs.*;

import org.apache.commons.lang3.time.StopWatch;
import org.bytedeco.opencv.opencv_text.FloatVector;
import org.bytedeco.opencv.opencv_text.IntVector;
import org.opencv.core.Core;
import org.opencv.dnn.Dnn;
import org.bytedeco.javacpp.*;
import org.opencv.imgproc.Imgproc;

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.util.*;
import java.util.concurrent.*;
import java.io.*;


public class yolo {

    public void Yolo3(){
        Mat img = imread("F:\\maven\\hot_yolov3\\src\\resources\\633.jpg");
        String cfg = "F:\\maven\\hot_yolov3\\src\\resources\\hot-yolov3.cfg";
        String model = "F:\\maven\\hot_yolov3\\src\\resources\\hot-yolov3.weights";

        Net net = opencv_dnn.readNetFromDarknet(cfg, model);
        Mat blob = opencv_dnn.blobFromImage(img, 1/255.0, new Size(416, 416), new Scalar(0.0), true, false,CV_32F);

        net.setInput(blob);

        StringVector outNames = net.getUnconnectedOutLayersNames();
        //System.out.println(outNames.toString());

        MatVector outs = new MatVector(outNames.size());

        net.forward(outs, outNames);

        float threshold = 0.25f;
        float nmsThreshold = 0.45f;
        GetResult(outs, img, threshold, nmsThreshold,true);
        //System.out.println(outs);
    }

    private void GetResult(MatVector output, Mat image, float threshold, float nmsThreshold, boolean nms)
    {
        nms = false;
        IntVector classIds = new IntVector();
        FloatVector confidences = new FloatVector();
        RectVector boxes = new RectVector();
        try{
            for(int i=0;i<output.size();++i)
            {
                Mat result = output.get(i);
                //System.out.println(result);
                for (int j = 0; j < result.rows(); j++)
                {
                    FloatPointer data = new FloatPointer(result.row(j).data());
                    Mat scores = result.row(j).colRange(5, result.cols());

                    Point classIdPoint = new Point(1);
                    DoublePointer confidence = new DoublePointer(1);

                    minMaxLoc(scores, null, confidence, null, classIdPoint, null);

                    if (confidence.get() > threshold)
                    {
                        int centerX = (int) (data.get(0) * image.cols());
                        int centerY = (int) (data.get(1) * image.rows());
                        int width = (int) (data.get(2) * image.cols());
                        int height = (int) (data.get(3) * image.rows());
                        int left = centerX - width / 2;
                        int top = centerY - height / 2;

                        classIds.push_back(classIdPoint.x());
                        confidences.push_back((float) confidence.get());
                        boxes.push_back(new Rect(left, top, width, height));
                    }
                }
            }

//            if (nms) {
//
//                IntPointer indices = new IntPointer(confidences.size());
//                for (int i = 0; i < confidences.size(); ++i) {
//                    Rect box = boxes.get(i);
//                    int classId = classIds.get(i);
//                    String res = "idx="+classId+"conf="+confidences.get(i);
//                    res += "box.x=" + box.x() +"box.y="+box.y()+"box.width"+box.width()+"box.height"+box.height();
//                    System.out.println(res);
//                }
//            }

            IntPointer indices = new IntPointer(confidences.size());
            FloatPointer confidencesPointer = new FloatPointer(confidences.size());
            confidencesPointer.put(confidences.get());

            NMSBoxes(boxes, confidencesPointer, threshold, nmsThreshold, indices, 1.f, 0);
//            NMSBoxes(boxes, confidencesPointer, threshold, nmsThreshold, indices);

            List<String> list = new ArrayList<String>();
            FileInputStream fis = new FileInputStream("F:\\maven\\hot_yolov3\\src\\resources\\hot.names");
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {
                list.add(line);
            }
            String[] Labels = list.toArray(new String[list.size()]);
            br.close();
            isr.close();
            fis.close();

            List diode = new ArrayList<>();
            List shadow = new ArrayList<>();
            List stain = new ArrayList<>();
            //System.out.println(indices.sizeof());
            //System.out.println(indices.limit());

            IplImage rawImage = null;
            rawImage = cvLoadImage("F:\\maven\\hot_yolov3\\src\\resources\\633.jpg");

            for (int m=0;m< indices.limit();++m)
            {
                int i = indices.get(m);
                Rect box = boxes.get(i);
                //System.out.println(box);
                int classId = classIds.get(i);
                //System.out.println("name:"+Labels);
                //System.out.println("classid"+classId);

                if (classId==0){
                    int x1 = box.x() ;
                    int y1 = box.y() ;
                    int x2 = box.x() + box.width();
                    int y2 = box.y() + box.height();


                    CvPoint pt1 = cvPoint(x1,y1);
                    CvPoint pt2 = cvPoint(x2,y2);
                    CvScalar color = cvScalar(255,0,0,0);       // blue [green] [red]
                    cvRectangle(rawImage, pt1, pt2, color, 1, 4, 0);

                    diode.add(box.x());
                    diode.add(box.y());
                    diode.add(box.width());
                    diode.add(box.height());
                }
                if (classId==1){
                    int x1 = box.x() ;
                    int y1 = box.y() ;
                    int x2 = box.x() + box.width();
                    int y2 = box.y() + box.height();
                    CvPoint pt1 = cvPoint(x1,y1);
                    CvPoint pt2 = cvPoint(x2,y2);
                    CvScalar color = cvScalar(0,255,0,0);       // blue [green] [red]
                    cvRectangle(rawImage, pt1, pt2, color, 1, 4, 0);

                    shadow.add(box.x());
                    shadow.add(box.y());
                    shadow.add(box.width());
                    shadow.add(box.height());
                }
                if (classId==2){
                    int x1 = box.x() ;
                    int y1 = box.y()  ;
                    int x2 = box.x() + box.width();
                    int y2 = box.y() + box.height();
                    CvPoint pt1 = cvPoint(x1,y1);
                    CvPoint pt2 = cvPoint(x2,y2);
                    CvScalar color = cvScalar(0,0,255,0);       // blue [green] [red]
                    cvRectangle(rawImage, pt1, pt2, color, 1, 4, 0);

                    stain.add(box.x());
                    stain.add(box.y());
                    stain.add(box.width());
                    stain.add(box.height());
                }
                String res = "idx="+classId+"  name="+Labels[classId]+"  conf="+confidences.get(i);
                res += "  box.x=" + box.x() +"  box.y="+box.y()+"  box.width="+box.width()+"  box.height="+box.height();
                System.out.println(res);
            }
            cvSaveImage("Highlight633.jpg",rawImage);


            String total_diode = "diode_num:"+diode.size()/4 +"  |diode_list:"+diode;
            String total_shadow = "shadow_num:"+shadow.size()/4 +"  |shadow_list:"+shadow;
            String total_stain = "stain_num:"+stain.size()/4 +"  |stain_list:"+stain;

            System.out.println(total_diode);
            System.out.println(total_shadow);
            System.out.println(total_stain);


        }catch(Exception e){
            System.out.println("GetResult error:" + e.getMessage());
        }
    }

    public static void main(String[] args) {
        yolo be =  new yolo();
        be.Yolo3();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值