Java通过Mathematica实现人脸识别

/**
 * Created by web-214 on 2015/8/25.
 */


import com.wolfram.jlink.*;


import java.io.*;


public class SampleProgram {


    public static void main(String[] argv) {


        KernelLink ml = null;


        try {
            // ml = MathLinkFactory.createKernelLink(argv);
            String jLinkDir = "D:\\Program Files\\Wolfram Research\\Mathematica\\10.2\\SystemFiles\\Links\\JLink";
            System.setProperty("com.wolfram.jlink.libdir", jLinkDir);
            ml = MathLinkFactory.createKernelLink(
                    "-linkmode launch -linkname 'D:\\program files\\wolfram research\\mathematica\\10.2\\mathkernel.exe'");
        } catch (MathLinkException e) {
            System.out.println("Fatal error opening link: " + e.getMessage());
            return;
        }


        try {
            // Get rid of the initial InputNamePacket the kernel will send
            // when it is launched.
            ml.discardAnswer();


            ml.evaluate("<<MyPackage.m");
            ml.discardAnswer();


            ml.evaluate("2+2");
            ml.waitForAnswer();


            int result = ml.getInteger();
            System.out.println("2 + 2 = " + result);


            // Here's how to send the same input, but not as a string:
            ml.putFunction("EvaluatePacket", 1);
            ml.putFunction("Plus", 2);
            ml.put(3);
            ml.put(3);
            ml.endPacket();
            ml.waitForAnswer();
            result = ml.getInteger();
            System.out.println("3 + 3 = " + result);


            // If you want the result back as a string, use evaluateToInputForm
            // or evaluateToOutputForm. The second arg for either is the
            // requested page width for formatting the string. Pass 0 for
            // PageWidth->Infinity. These methods get the result in one
            // step--no need to call waitForAnswer.
            String strResult = ml.evaluateToOutputForm("4+4", 0);
            System.out.println("4 + 4 = " + strResult);


            String pic_path = "test11.jpg";
            File file = new File(pic_path);
            InputStream input = new FileInputStream(file);
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[input.available()];
            int rc = 0;
            while ((rc = input.read(buffer, 0, 100)) > 0) {
                swapStream.write(buffer, 0, rc);
            }


            // ml.putFunction("EvaluatePacket", 1);
            // ml.putFunction("Last", 1);
            // ml.putFunction("List", 2);
            // ml.put(1);
            // ml.put(2);


            // ml.putFunction("EvaluatePacket", 1);
            ml.putFunction("FindFaces", 1);
            ml.putFunction("Import", 1);
            ml.put("D:\\workspace\\Test\\test11.jpg");
            ml.endPacket();
            ml.waitForAnswer();
            double[][] result_list = ml.getDoubleArray2();
            for (int row = 0; row < result_list.length; row++) {
                for (int column = 0; column < result_list[row].length; column += 2) {
                    System.out.println("{" + result_list[row][column] + "," + result_list[row][column + 1] + "}");
                }
            }


        } catch (MathLinkException e) {
            System.out.println("MathLinkException occurred: " + e.getMessage());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            ml.close();
        }
    }
}

Mathematica举例

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值