1. 깃허브 구글 소스 파일 다운

https://github.com/GoogleCloudPlatform/cloud-vision

위 URL에서 파일을 다운로드 합니다. 여러가지 파일들이 한번에 다운로드 됩니다. 

 

2. cloud-vision-master / android / CloudVision 열기

안드로이드 스튜디오로 CloudVision파일을 Open 합니다.

 

3. Key 설정 build.gradle (Module: app)

android {
    ...
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        buildTypes.each {
          //it.buildConfigField 'String', 'API_KEY', ApiKey
            it.buildConfigField ('String', 'API_KEY', '"[ApiKey]"')
        }
    }
    ...
}

it.buildConfigField 'String', 'API_KEY', ApiKey을 위의 코드로 바꿔주고, ApiKey에 생성한 Key를 넣어줍니다.

오류가 발생했었는데, 저렇게 바꿔주면 오류가 사라집니다.

 

4. Key 설정 MainActivity

...
public class MainActivity extends AppCompatActivity {
    private static final String CLOUD_VISION_API_KEY = ApiKey;
    public static final String FILE_NAME = "temp.jpg";
    private static final String ANDROID_CERT_HEADER = "X-Android-Cert";
    private static final String ANDROID_PACKAGE_HEADER = "X-Android-Package";
    private static final int MAX_LABEL_RESULTS = 10;
    private static final int MAX_DIMENSION = 1200;
    ...
}

ApiKey에 생성한 Key를 넣어줍니다.

 

5. 사용

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

지금은 LABEL_DETECTION 으로 설정되어있습니다.

하지만 원하는 기능을 사용하고 싶으면 아래 코드를 수정하면 됩니다.

labelDetection.setType("LABEL_DETECTION");
/*
  TYPE_UNSPECIFIED	Unspecified feature type.
  FACE_DETECTION	Run face detection.
  LANDMARK_DETECTION	Run landmark detection.
  LOGO_DETECTION	Run logo detection.
  LABEL_DETECTION	Run label detection.
  TEXT_DETECTION	Run text detection / optical character recognition (OCR). Text detection is optimized for areas of text within a larger image; if the image is a document, use DOCUMENT_TEXT_DETECTION instead.
  DOCUMENT_TEXT_DETECTION	Run dense text document OCR. Takes precedence when both DOCUMENT_TEXT_DETECTION and TEXT_DETECTION are present.
  SAFE_SEARCH_DETECTION	Run Safe Search to detect potentially unsafe or undesirable content.
  IMAGE_PROPERTIES	Compute a set of image properties, such as the image's dominant colors.
  CROP_HINTS	Run crop hints.
  WEB_DETECTION	Run web detection.
  PRODUCT_SEARCH	Run Product Search.
  OBJECT_LOCALIZATION	Run localizer for object detection.
*/

모든 feature들을 주석처리 했습니다. 

만약 feature을 수정했다면, 출력 메세지도 수정해주어야 합니다. 

private static String convertResponseToString(BatchAnnotateImagesResponse response) {
        StringBuilder message = new StringBuilder("I found these things:\n\n");

        List<EntityAnnotation> labels = response.getResponses().get(0).getLabelAnnotations();
        if (labels != null) {
            for (EntityAnnotation label : labels) {
                message.append(String.format(Locale.US, "%.3f: %s", label.getScore(), label.getDescription()));
                message.append("\n");
            }
        } else {
            message.append("nothing");
        }

        return message.toString();
    }

 아래 코드를 수정해야합니다.

List<EntityAnnotation> labels = response.getResponses().get(0).getLabelAnnotations();

getLabelgetLabelAnnotations() 이 부분을 사용한 feature에 맞게 바꾸어주면되는데,

속성들을 보고 찾아서 쓰면 금방 알아낼수 있을 것입니다.

예를 들면 WEB_DETECTION의 경우 getWebDetection()으로 바꾸어주면 됩니다.

 

+ Recent posts