- [C#] Chia sẽ source code nhận dạng nhiều khuôn mặt RealTime
- [C#] Đặt mật khẩu bảo vệ cho database Sqlite
- [C#] Tách file thành nhiều phần để download
- [SQL SERVER] Chia sẽ hàm convert table sang class C#
- [C#] Hướng dẫn Split files and Merge files
- [C#] Hướng dẫn Sử dụng IL Disassembler (ildasm.exe) và IL Assembler (ilasm.exe) để chỉnh sửa mã nguồn
- [C#] Hướng dẫn lấy icon từ process
- [C#] Hướng dẫn chuyển đổi chuỗi sang nhị phân và ngược lại
- [C#] Hướng dẫn sử dụng Expando Object với từ khóa Dynamic
- [C#] Hướng dẫn sử dụng thuật toán mã hóa và giải mã Atom-128 algorithm
- [C#] Hướng dẫn sắp xếp column listview (sort column header listview)
- [C#] Hướng dẫn lấy icon từ thuộc tính file shell32.dll trong windows
- [SQL SERVER] Hướng dẫn tạo bảng cấu trúc cây theo phương pháp đệ quy
- [SQL SERVER] Hướng dẫn sử dụng hàm WITH để đệ quy trong sql
- C# - Kéo thả dòng từ một lưới sang một lưới khác trong cùng một form
- [C#] Hướng dẫn tạo checkbox Datagridview và truyền dữ liệu giữa hai Gridview
- [C#] Hướng dẫn sử dụng StopWatch để kiểm tra tốc độ xử lý trong Visual Studio
- [C#] Hướng dẫn tạo hotkey cho ứng dụng winform
- [C#] Hướng dẫn mã hóa và giải mã sử dụng thuật toán ROT13
- [C#] Hướng dẫn sử dụng thuật toán mã hóa RC4 (Rivest Cipher 4 )
[C#] Hướng dẫn sử dụng Microsoft Cognitive Services để nhận dạng hình ảnh
Bài viết hôm nay, mình sẽ hướng dẫn các bạn sử dụng Microsoft Cognitive Services để nhận dạng hình ảnh trong lập trình C#.
[C#] Microsoft Cognitive Services là gì?
Microsoft Cognitive Services bao gồm một bộ các API ứng dụng trí tuệ nhân tạo thông minh, cho phép lập trình viên ở mọi cấp độ từ những bạn sinh viên viết ứng dụng đầu tiên của mình hay những lập trình viên chuyên nghiệp làm việc cho những công ty, tổ chức lớn đều có thể tạo ra được những ứng thông minh hơn một cách dễ dàng.
Các API của Cognitive Services được viết dưới dạng REST API
do vậy lập trình viên có thể tích hợp các API này trên nhiều nền tảng khác nhau như iOS, Android, hay Windows, chỉ cần có kết nối Internet.
Tính đến thời điểm viết bài viết này, Microsoft Cognitive Services bao gồm 21 API được chia thành 5 nhóm: Vision
, Speech
, Language
, Knowledge
và Search
. Hãy cùng tìm hiểu 5 nhóm API này là gì?
Bây giờ mình sẽ demo một ứng dụng sử dụng Microsoft Cognitive Services. Khi import hình ảnh vào thì nhận dạng hình ảnh đó là hình ảnh gì qua API.

Để sử dụng được Microsoft Cognitive Services, các bạn vào địa chỉ trang web link bên dưới để đăng ký và lấy key API nhé.
Link: https://azure.microsoft.com/en-us/try/cognitive-services/

Sau khi, login thành công, các bạn nhấn vào nút Add Vision AP
I để tạo ứng dụng cho mình như hình bên dưới:

Dưới đây là Source code Microsoft Cognitive Services C#:
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.Net.Http; using System.IO; using Newtonsoft.Json.Linq; using Newtonsoft.Json; namespace VisionApi { public partial class Form1 : Form { public Form1() { InitializeComponent(); } const string subscriptionKey = "386cd088733f4272980ed71d1963c0fa"; const string uriBase = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze"; string imageFilePath = ""; private void btnBrowse_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "JPEG *.jpg|*.jpg|PNG *.png|*.png"; if (ofd.ShowDialog() == DialogResult.OK) { imageFilePath = ofd.FileName; pictureBox1.Image = Image.FromFile(imageFilePath); btnRecoginze.Enabled = true; } } async Task<string> MakeAnalaysisRequest(string imageFilePath) { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey); string requestParamters = "visualFeatures=Categories,Description,Color&language=en"; string uri = uriBase + "?" + requestParamters; HttpResponseMessage responseMessage; byte[] dataByte = GetImageAsBytesArray(imageFilePath); using (ByteArrayContent content = new ByteArrayContent(dataByte)) { content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); responseMessage = await client.PostAsync(uri, content); string contentString = await responseMessage.Content.ReadAsStringAsync(); return contentString; } } private byte[] GetImageAsBytesArray(string imageFilePath) { FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(fileStream); return reader.ReadBytes((int)fileStream.Length); } private async void btnRecoginze_Click(object sender, EventArgs e) { btnRecoginze.Enabled = false; btnBrowse.Enabled = false; btnRecoginze.Text = "Đang xử lý dữ liệu..."; string result = await MakeAnalaysisRequest(imageFilePath); if (result == "") { label2.Text = "Kết nối thất bại"; } else { JObject data = JObject.Parse(result); JToken description = data["description"]; string a = (string)data.SelectToken("description.captions[0].text"); label2.Text = a; } btnBrowse.Enabled = true; btnRecoginze.Text = "Bạn thấy gì?"; } } }
HAPPY CODING :)