azure image recognition by local

27 October 2019

먼저 MS Azure에 가입하고, computer-vision에도 구독을 추가하도록 합니다.

그 이후, 본인의 cognitive-service의 subscription key를 확인하고, 해당 key를 통해 아래와 같이 request 를 해주도록 합니다.

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@SpringBootApplication
public class VisionApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(VisionApplication.class);
    }

    @Override
    public void run(String... args) {
        RestTemplate restTemplate = new RestTemplate();

        String urlBase = "https://westcentralus.api.cognitive.microsoft.com/vision/v2.1/analyze";

        String requestParameters =
                "visualFeatures=Categories,Description,Color";

        String uri = urlBase + "?" + requestParameters;

        String subscriptionKey = "{mySubscriptionKey}";

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Ocp-Apim-Subscription-Key", subscriptionKey);
        httpHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);

        byte[] body = readBytesFromFile("{myFilePath}.jpg");

        HttpEntity httpEntity = new HttpEntity<>(body, httpHeaders);

        ResponseEntity response = restTemplate.exchange(uri, HttpMethod.POST, httpEntity, String.class);

        System.out.println(response.getBody());
    }

    private byte[] readBytesFromFile(String filePath) {

        FileInputStream fileInputStream = null;
        byte[] bytesArray = null;

        try {
            File file = new File(filePath);
            bytesArray = new byte[(int) file.length()];
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(bytesArray);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bytesArray;
    }
}
위 코드에서 mySubscriptionKey와 {myFilePath}.jpg를 본인의 subscriptionkey와 jpg filepath로 변경하면 아래와 같이 response가 오는 것을 확인할 수 있습니다.
{"categories":[],"color":{"dominantColorForeground":"Brown","dominantColorBackground":"Brown","dominantColors":["Brown","White"],"accentColor":"BE740D","isBwImg":false,"isBWImg":false},"description":{"tags":["lion","cat","grass","animal","mammal","outdoor","field","brown","standing","looking","sitting","camera","close","sheep","open","black","face","green","large","white","grassy","young","dog","laying"],"captions":[{"text":"a lion looking at the camera","confidence":0.98581289400436622}]},"requestId":"####","metadata":{"width":926,"height":616,"format":"Jpeg"}}
reference : https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts/csharp-analyze