Android 个具有 python 后端的应用

Android app with python backend

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

熟悉 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);