java - 带有 python 后端的 Android 应用程序

标签 java android python neural-network

嘿,我使用 python 创建了一个神经网络,该网络可以识别手写数字。我想在我的 Android 应用程序中使用它。 Android 应用程序会拍摄手写数字的照片,并将其发送到神经网络,神经网络将计算出该数字并将其发送回应用程序。我该怎么做?我查看了 Google Cloud Platform,但很困惑。我想知道如何将应用程序中的图片发送到我的 python 神经网络,然后将输出发回。

最佳答案

让自己熟悉 REST 的概念(RestEasy、Jersey 等),创建一个带有端点的 REST 服务器,该端点可以接收包含您的 Base64 图片的 JSON 字符串。应用程序将图片进行base64转换并将其发送到REST服务器。在 REST 服务器中取消编码,将其委托(delegate)给 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);

关于java - 带有 python 后端的 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34848817/

相关文章:

java - 子图匹配(JUNG)

java - 为什么 spring 框架不将我的 YAML 列表绑定(bind)到实体?

android - Http请求拦截未显示任何日志信息

android - 这是检查系统上是否存在罗盘传感器的正确方法吗?

python - 使用字符串作为变量名

java - 如何使用ast解析器获取信息

实现匿名函数的匿名接口(interface)的java语法

java - 错误 :getReadableDatabase() on a null object reference

Python boto ec2 - 我如何等到图像创建或失败

python - 寻找数据边界处的峰值