반응형

C#에서 구글 클라우드 비전 API를 사용해 보자.

 

Google Could Platform - APIs&Services 에서 Cloud Vision API를 활성화(Enable)한다.

 

Service account key를 생성하고 JSON파일로 다운로드한다.

 

Visual Studio - NuGet Package Manager에서 Google.Cloud.Vision.V1을 설치한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using System.IO;
using Google.Cloud.Vision.V1;
 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Image image = Image.FromFile("./barbara.png");
                string credentialsString = File.ReadAllText("./your_jsoncredentials.json");
 
                ImageAnnotatorClient client = new ImageAnnotatorClientBuilder
                {
                    JsonCredentials = credentialsString
                    //CredentialsPath = "./your_jsoncredentials.json"
                    // 간단히 CredentialsPath에 서비스 계정 키(json) 파일을 직접 대입해도 된다.
                }.Build();
 
                IReadOnlyList<FaceAnnotation> result = client.DetectFaces(image);
                foreach (FaceAnnotation face in result)
                {
                    string poly = string.Join(" - ", face.BoundingPoly.Vertices.Select(v => $"({v.X}, {v.Y})"));
                    Console.WriteLine($"Confidence: {(int)(face.DetectionConfidence * 100)}%; BoundingPoly: {poly}");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
 

 

소스를 입력하고 빌드한다.

 

 

분석 이미지

 

분석 이미지의 얼굴 좌표와 신뢰도가 함께 표시된다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using Google.Cloud.Vision.V1;
using Newtonsoft.Json;
 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Image image = Image.FromFile("./barbara.png");
                Credentials credentials = new Credentials
                {
                    type = "your_type",
                    project_id = "your_project_id",
                    private_key_id = "your_key_id",
                    private_key = "-----BEGIN PRIVATE KEY-----your_key-----END PRIVATE KEY-----\n",
                    client_email = "your_email",
                    client_id = "your_client_id",
                    auth_uri = "your_auth_uri",
                    token_uri = "your_token_uri",
                    auth_provider_x509_cert_url = "your_auth_cert_url",
                    client_x509_cert_url = "your_client_cert_url"
                };
 
                ImageAnnotatorClient client = new ImageAnnotatorClientBuilder
                {
                    JsonCredentials = JsonConvert.SerializeObject(credentials)
 
                }.Build();
 
                IReadOnlyList<FaceAnnotation> result = client.DetectFaces(image);
                foreach (FaceAnnotation face in result)
                {
                    string poly = string.Join(" - ", face.BoundingPoly.Vertices.Select(v => $"({v.X}, {v.Y})"));
                    Console.WriteLine($"Confidence: {(int)(face.DetectionConfidence * 100)}%; BoundingPoly: {poly}");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
 
    class Credentials
    {
        public string type;
        public string project_id;
        public string private_key_id;
        public string private_key;
        public string client_email;
        public string client_id;
        public string auth_uri;
        public string token_uri;
        public string auth_provider_x509_cert_url;
        public string client_x509_cert_url;
    }
}
 

 

위 소스처럼 서비스 계정 키(json)의 내용을 코드에 직접 삽입 할 수도 있다.

 

※ 참고

Google.Cloud.Vision.V1

 

반응형
Posted by J-sean
: