0

我开发了一个 android 应用程序,它将拍摄一个数字的照片并将其发送到服务器。在服务器中,图像将通过神经网络识别数字并将结果发回。我收到错误 500 而不是结果。

这是我的android代码:

//sendbmp is the photo taken from the camera. Its file type is .bmp
sendbmp = Tools.getFocusedBitmap(getApplicationContext(), camera, data, focusBox.getBox());

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    jpgbitmap = sendbmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byteArray = stream.toByteArray();
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mJSONArray = new JSONArray(byteArray);
            base64array = Base64.encodeToString(byteArray,Base64.DEFAULT);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

   new Networkconnection().execute();

这是 Networkconnection() 的代码,这是我将照片发送到服务器的地方。我正在使用 okhttp:

private class Networkconnection extends AsyncTask<Void, Void, String> {

    String result;
    String new_result;
    @Override
    protected String doInBackground(Void... params) {
        Response response;
        OkHttpClient client = new OkHttpClient();
        String byteValue = String.valueOf(base64array);
        RequestBody body = RequestBody.create(MediaType.parse("json"), base64array);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        try {
             response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

这是我使用烧瓶的服务器端:

 @app.route('/', methods=['GET','POST'])
def hello_world():
    received_img = request.args.get('body')
    decode_Imge = pic.open(BytesIO(base64.b64decode(received_img)))
    result = Net.image_function(decode_Imge)
return result

if __name__ == '__main__':
    Net = Network([784, 100,10])
    for x in range(0,4):
        training_data, validation_data,test_data = load_data_wrapper()
        Net.SGD(training_data, 30, 10, 5.0, test_data = test_data)
   app.run(host ="MyIP") //In my code I actually have my ip address and not "MYIP"

注意:当我运行服务器时,我没有收到任何错误。所有类都在服务器的一个文档中。当我打印回溯时,我得到以下信息: Traceback (most recent call last): File "C:\Python27\lib\site-packages\werkzeug\serving.py", line 648, in inner fd = int(os.environ['WERKZEUG_SERVER_FD']) File "C:\Python27\lib\os.py", line 425, in __getitem__ return self.data[key.upper()] KeyError: 'WERKZEUG_SERVER_FD'

这是我得到的错误,不幸的是,部分错误被按钮阻止了:错误 500

4

1 回答 1

0

原来我根本没有向服务器发送任何东西。下面是使用 okhttp 向服务器发送项目的正确代码:

这就是你如何把它变成base64:

//getting img from camera
sendbmp = Tools.getFocusedBitmap(getApplicationContext(), camera, data, focusBox.getBox());
     //converting image to base64
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    jpgbitmap = sendbmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byteArray = stream.toByteArray();
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mJSONArray = new JSONArray(byteArray);
            base64array = Base64.encodeToString(byteArray,Base64.DEFAULT);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

这是您将其发送到服务器的方式

 Response response;
        OkHttpClient client = new OkHttpClient();
        String byteValue = String.valueOf(base64array);
        //RequestBody body = RequestBody.create(MediaType.parse("json"), base64array);//RequestBody.create(MediaType.parse("application/json"),base64array);

        try {
            MediaType MEDIA_TYPE_MARKDOWN
                    = MediaType.parse("text/x-markdown; charset=utf-8");


            Request request = new Request.Builder()
                    .url(url)
                    .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, byteValue))
                    .build();
             response = client.newCall(request).execute();
            result = response.body().string();
        } catch (Exception e) {
            Log.d("Error: " , e.getMessage());
        }

这是您使用烧瓶在服务器中检索它的方式:

try:
    received_img = request.data
except (Exception) as err:
        traceback.print_exc(err)
//remember that the image we received is in base64, here we are converting it to image
imgdata = base64.b64decode(received_img)
filename = 'some_image.bmp'  # I assume you have a way of picking unique filenames
with open(filename, 'wb') as f:
    f.write(imgdata)

resutl = image_function(filename)
于 2016-01-26T00:38:32.810 回答