C# Google Cloud Vision API - 구글 클라우드 비전 API 2
C# 2022. 2. 11. 13:46 |반응형
C# WinForm 으로 구글 클라우드 비전 API 결과를 확인해 보자. 아래 링크를 참고해 기본적인 구글 클라우드 비전 API 사용 준비를 한다.
2022.02.11 - [C#] - C# Google Cloud Vision API - 구글 클라우드 비전 API 1
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Google.Cloud.Vision.V1;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
ImageAnnotatorClient client;
public Form1()
{
InitializeComponent();
try
{
client = new ImageAnnotatorClientBuilder
{
CredentialsPath = "./values-324913-58dd97f8b8ec.json"
}.Build();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
System.Drawing.Image systemImage;
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
systemImage = System.Drawing.Image.FromFile(dlg.FileName);
pictureBox1.Image = systemImage;
pictureBox1.Refresh();
//Forces the control to invalidate its client area and immediately redraw itself and any child controls.
// 미리 Refresh()를 호출하지 않으면 아래 얼굴 영역 사각형, 신뢰도 텍스트 출력이 보이지 않는다.
Graphics graphics = pictureBox1.CreateGraphics();
Google.Cloud.Vision.V1.Image visionImage = Google.Cloud.Vision.V1.Image.FromFile(dlg.FileName);
//MemoryStream memoryStream = new MemoryStream();
//pictureBox1.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
//Google.Cloud.Vision.V1.Image visionImage = Google.Cloud.Vision.V1.Image.FromBytes(memoryStream.ToArray());
// Google.Cloud.Vision.V1.Image와 System.Drawing.Image는 서로 호환되지 않는다.
IReadOnlyList<FaceAnnotation> result = client.DetectFaces(visionImage);
foreach (FaceAnnotation face in result)
{
Google.Protobuf.Collections.RepeatedField<Vertex> vertices = face.BoundingPoly.Vertices;
Rectangle rectangle = new Rectangle(vertices[0].X, vertices[0].Y, vertices[2].X - vertices[0].X,
vertices[2].Y - vertices[0].Y);
// 얼굴 영역 (vertices[0]: 좌상단, vertices[1]:우상단, vertices[2]:우하단, vertices[3]:좌하단)
StringFormat stringFormat = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
// 신뢰도 문자열 센터 정렬
graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);
graphics.DrawString($"Confidence: {(int)(face.DetectionConfidence * 100)}%", Font,
Brushes.Red, rectangle, stringFormat);
}
graphics.Dispose();
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
}
}
|
소스를 입력하고 빌드한다.
반응형
'C#' 카테고리의 다른 글
C# with SQLite - 데이터베이스 연동 1 (0) | 2022.02.18 |
---|---|
C# Google Cloud Vision API - 구글 클라우드 비전 API 3 (0) | 2022.02.11 |
C# Google Cloud Vision API - 구글 클라우드 비전 API 1 (0) | 2022.02.11 |
C# with MariaDB(MySQL) - 데이터베이스 연동 (0) | 2022.02.08 |
C# AdsJumbo - 윈도우 앱(프로그램)에 광고 넣기 (0) | 2022.02.03 |