【问题标题】:Android app with python backend带有 python 后端的 Android 应用程序
【发布时间】:2016-04-23 06:36:15
【问题描述】:

嘿,我用 python 创建了一个神经网络,这个网络可以识别手写数字。我想在我的 android 应用程序中使用它。安卓应用程序会拍一张手写数字的照片,并将其发送到神经网络,神经网络将计算出数字并将其发送回应用程序。我该怎么做?我查看了谷歌云平台,但我很困惑。我想知道如何将图片从应用程序发送到我的 python 神经网络,然后将输出发送回来。

【问题讨论】:

    标签: java android python neural-network


    【解决方案1】:

    让自己熟悉 REST(RestEasy、Jersey 等)的概念,创建一个 REST 服务器,其端点可以接收包含您的 base64 图片的 JSON 字符串。该应用程序使用 base64 转换图片并将其发送到 REST 服务器。在您的 REST 服务器中对其进行取消编码,将其委托给 python 脚本,将结果转换回 JSON 并将其发送回应用程序。应用本身接收 JSON 并从中获取数据。

    这就是我使用 WildFly 服务器和 RestEasy 的方式:

    //app side 
    PictureInputBo pictureInputBo = new PictureInputBo([your binary]); //encodes it to base64
    
    //This is working Java code and can be used.
    //It will automatically convert to JSON and sent to the "convert" endpoint of the server
    ResteasyClient client = new ResteasyClientBuilder().build();
    ResteasyWebTarget target = client.target("http://yourserver:8080/your-webservice/rest/convert/");
    Response response = target.request().accept(MediaType.APPLICATION_JSON).post(Entity.entity(pictureInputBo, "application/json;charset=UTF-8;version=1"));
    
    
    //Server side..."Converter Endpoint", this is also working Java code.
    //it will automatically converted back to the Java object "PictureInputBo" by RestEasy
    @POST
    @Path("/convert")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response convertPicture(@NotNull(message = "Must not be null") @Valid PictureInputBo inputBo)
    {
        //Here you pass your business object to the converter service which processes the data
        //(pass it to python or whatever) 
        PictureOutputBo result = converterService.convert(inputBo);
    
        //Resteasy converts it back to JSON and responds it to the app.
        return Response.ok().entity(result).build();
    }
    
    
    //Back in your app.
    check if response.getStatus() == 200) //HTTP Status OK
    PictureOutputBo pictureOutputBo = response.readEntity(PictureOutputBo.class);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 2017-11-29
      相关资源
      最近更新 更多