仿真器上的Google Cloud Vision API工作速度比实际设备快6倍

时间:2017-03-07 20:05:03

标签: android google-vision

我正在将Google Cloud Vision API集成到我的应用中。 我注意到文本检测,标签检测在真实设备上运行速度比在Android模拟器上慢6倍。

问题的原因是在实际设备中互联网速度不低。因为另一个也发出http请求的应用程序部分为真实设备和模拟器提供了一个性能。

此问题可在Google官方Cloud Vision Android Sample

重现

在2个不同的真实设备和2个仿真器上测试。

任何想法为什么会发生?

官方样本代码:

private void callCloudVision(final Bitmap bitmap) throws IOException {
    // Switch text to loading
    mImageDetails.setText(R.string.loading_message);

    // Do the real work in an async task, because we need to use the network anyway
    new AsyncTask<Object, Void, String>() {
        @Override
        protected String doInBackground(Object... params) {
            try {
                HttpTransport httpTransport = AndroidHttp.newCompatibleTransport();
                JsonFactory jsonFactory = GsonFactory.getDefaultInstance();

                Vision.Builder builder = new Vision.Builder(httpTransport, jsonFactory, null);
                builder.setVisionRequestInitializer(new
                        VisionRequestInitializer(CLOUD_VISION_API_KEY));
                Vision vision = builder.build();

                BatchAnnotateImagesRequest batchAnnotateImagesRequest =
                        new BatchAnnotateImagesRequest();
                batchAnnotateImagesRequest.setRequests(new ArrayList<AnnotateImageRequest>() {{
                    AnnotateImageRequest annotateImageRequest = new AnnotateImageRequest();

                    //https://cloud.google.com/vision/docs/languages
                    ImageContext imageContext = new ImageContext();
                    List<String> languages = new ArrayList<>();
                    languages.add("pl");
                    imageContext.setLanguageHints(languages);
                    annotateImageRequest.setImageContext(imageContext);

                    // Add the image
                    Image base64EncodedImage = new Image();
                    // Convert the bitmap to a JPEG
                    // Just in case it's a format that Android understands but Cloud Vision
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
                    byte[] imageBytes = byteArrayOutputStream.toByteArray();

                    // Base64 encode the JPEG
                    base64EncodedImage.encodeContent(imageBytes);
                    annotateImageRequest.setImage(base64EncodedImage);

                    // add the features we want
                    annotateImageRequest.setFeatures(new ArrayList<Feature>() {{
                        Feature labelDetection = new Feature();
                        labelDetection.setType("LABEL_DETECTION");
                        labelDetection.setMaxResults(10);
                        add(labelDetection);
                    }});

                    // Add the list of one thing to the request
                    add(annotateImageRequest);
                }});

                Vision.Images.Annotate annotateRequest =
                        vision.images().annotate(batchAnnotateImagesRequest);
                // Due to a bug: requests to Vision API containing large images fail when GZipped.
                annotateRequest.setDisableGZipContent(true);
                Log.d(TAG, "created Cloud Vision request object, sending request");

                BatchAnnotateImagesResponse response = annotateRequest.execute();
                return convertResponseToString(response);

            } catch (GoogleJsonResponseException e) {
                Log.d(TAG, "failed to make API request because " + e.getContent());
            } catch (IOException e) {
                Log.d(TAG, "failed to make API request because of other IOException " +
                        e.getMessage());
            }
            return "Cloud Vision API request failed. Check logs for details.";
        }

        protected void onPostExecute(String result) {
            mImageDetails.setText(result);
        }
    }.execute();
}

3次测试后处理一幅图像的时间结果:
真实设备
48409608471
49837573418
48430621961

模拟器
7725682517个
7967006977个
5478519794个

我用简单的

long startTime = System.nanoTime();
long endTime = System.nanoTime();
long duration = (endTime - startTime); 

0 个答案:

没有答案