添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
import org.opencv.dnn.Dnn; import org.opencv.dnn.Net; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import org.opencv.videoio.VideoCapture; public class AgeGenderRecognition {    private static final String[] GENDER_LIST = {"Male", "Female"};    private static final String[] AGE_LIST = {"(0-2)", "(4-6)", "(8-12)", "(15-20)", "(25-32)", "(38-43)", "(48-53)", "(60-100)"};    private static final double[] MODEL_MEAN_VALUES = {78.4263377603, 87.7689143744, 114.895847746};    private static final double CONFIDENCE_THRESHOLD = 0.7;    public static void main(String[] args) {        // Load networks        Net ageNet = Dnn.readNetFromCaffe("age_net.caffemodel", "age_deploy.prototxt");        Net genderNet = Dnn.readNetFromCaffe("gender_net.caffemodel", "gender_deploy.prototxt");        Net faceNet = Dnn.readNetFromTensorflow("opencv_face_detector_uint8.pb", "opencv_face_detector.pbtxt");        // Open a video file or an image file or a camera stream        VideoCapture cap;        if (args.length > 0) {            cap = new VideoCapture(args[0]);        } else {            cap = new VideoCapture(0);        while (true) {            // Read frame            Mat frame = new Mat();            cap.read(frame);            // Get face bounding boxes            Mat frameFace = frame.clone();            List bboxes = new ArrayList<>();            Size size = new Size(300,300);            Scalar scalar = new Scalar(104, 117, 123);            Mat blob = Dnn.blobFromImage(frameFace, 1.0, size, scalar, false, false);            faceNet.setInput(blob);            Mat detections = faceNet.forward();            for (int i = 0; i < detections.size().height; i++) {            //double confidence = detections.get(0, 0, i, 2)[0];                double confidence;                confidence = detections.get(i, 2)[0];                if (confidence > CONFIDENCE_THRESHOLD) {                    int x1 = (int) (detections.get(i, 3)[0] * frame.cols());                    int y1 = (int) (detections.get(i, 4)[0] * frame.rows());                    int x2 = (int) (detections.get(i, 5)[0] * frame.cols());                    int y2 = (int) (detections.get(i, 6)[0] * frame.rows());                    bboxes.add(new Rect(x1, y1, x2 - x1, y2 - y1));                    Imgproc.rectangle(frameFace, new org.opencv.core.Point(x1, y1), new org.opencv.core.Point(x2, y2), new org.opencv.core.Scalar(0, 255, 0), (int) Math.round(frame.rows() / 150), 8, 0);        if (bboxes.isEmpty()) {            System.out.println("No face Detected, Checking next frame");            continue;        for (Rect bbox : bboxes) {            Mat face = new Mat(frame, bbox);            Size sizeb = new Size(227,227);            Scalar scalarb = new Scalar(MODEL_MEAN_VALUES);            blob = Dnn.blobFromImage(face, 1.0, sizeb, scalarb, false);            genderNet.setInput(blob);            Mat genderPreds = genderNet.forward();            String gender = GENDER_LIST[(int) genderPreds.get(0, 0)[0]];            //System.out.println("Gender Output : " + genderPreds);            System.out.println("Gender : " + gender + ", conf = " + genderPreds.get(0, 0)[0]);            ageNet.setInput(blob);            Mat agePreds = ageNet.forward();            agePreds = agePreds.reshape(1, 1); // reshape to 2D matrix with one row            Point maxLoc = new Point();            Core.minMaxLoc(agePreds);            int ageIdx = (int) maxLoc.x;            Imgcodecs.imwrite("age-gender-out-"+args[0]+".jpg", frameFace);            System.out.println("Age : " + AGE_LIST[ageIdx] + ", conf = " + agePreds.get(0, ageIdx)[0]);        cap.release();

I get!
This is what chatGPT is saying about debugging the code, but it's not helpful...

The error message "java.lang.UnsatisfiedLinkError: 'long org.opencv.dnn.Dnn.readNetFromCaffe_0(java.lang.String, java.lang.String)'" suggests that the program is trying to call a native method that could not be found.

This error is caused by an issue with the native libraries of OpenCV.

The most common causes of this error are:

OpenCV library files are not present in the classpath.
OpenCV library files are not compatible with the version of Java you are using.
The OpenCV native library files (e.g. opencv_javaXXX.dll, libopencv_javaXXX.so) are not in the PATH.
You are using a 32-bit version of Java with a 64-bit version of the OpenCV native libraries or vice versa.

To solve this error, you should check that you have the correct version of OpenCV and its Java bindings installed, and that they are included in the classpath of your project.
Also, you should check that the native libraries of OpenCV are in the PATH or in the directory of your project.
You should also make sure that you are using the correct version of Java and that it is compatible with the version of OpenCV you are using.

Please check the following:

Make sure you have the correct version of OpenCV for your platform
Make sure the opencv jar is in your classpath
Make sure the native library (dll or so) is in your PATH or in the working directory of your program
Make sure the java version you are using is compatible with the openCV version you are using.

Please let me know if you have further questions or issues.
My openCV path is: C:\opencv\build\java\opencv-460.jar

The class path is correct in netbeans, as I checked this:
Right-click on your project in the Project pane and select Properties.
Select Libraries from the left sidebar.
Click on the Add JAR/Folder button and navigate to the location of the opencv-xxx.jar file.
Click on the Open button.
Click on the OK button.


The OpenCV native library files (e.g. opencv_javaXXX.dll, libopencv_javaXXX.so) are not in the PATH.
Make sure the native library (dll or so) is in your PATH or in the working directory of your program


Have you checked that any dll are in the correct locations?
Ok I sorted part of the erros, being the unstisfied link, but it still won't load the models.


It found a ® character where it was expecting a { character.
Some code fom a old project of mine, that was in opencv2.4.x

Anyway It didn't take much to get past that error and getting a new one!

Once I get it acceepting the DNN I'm going to sort out adding arguments, as I have some nice code for that (once I can get it to run)!