ETH官方钱包

前往
大廳
主題

AI跨領域數據科學 課程紀錄 d34 Azure雲端平臺實務 (3) 進階服務

傳說中的巴哈魔法師~ | 2022-04-27 21:39:53 | 巴幣 2 | 人氣 281

今天
1.使用Azure機器學習 雲端運算 找出最適合指定資料 (銀行市場行銷資料集) 的演算法與模型 並架成網路服務 以Python及PowerBI展現
2.使用Azure電腦視覺 api 以Python展現 及 Visual Studio 2022環境中撰寫C# 介面



1.Azure機器學習 雲端運算

開啟Azure網站 > 建立資源
這部分會使用
資源群組 機器學習

-
資源群組>建立

機器學習>建立
選擇剛建立的資源群組
選擇一個所在地區 (避免出問題 這邊地區皆選美中)
新建容器

機器學習>概觀
啟動工作室 進入Microsoft Azure Machine Learning Studio介面
新建
Notebooks (for小型檔案)、Automated ML、Designer (for大型企劃)
從本地檔案創建
上傳 bankmarketing.csv檔

bankmarking.csv檔
根據各項資料 來看人是否會接受行銷 (分類問題 是/否 欄位:y)
由於資料不平衡 (絕大多數人拒絕行銷)
少的資料需要多抽幾次 (過取樣) 多的資料要少抽幾次 (欠取樣)

day_of_week欄位不加入訓練

剛剛建立了Dataset 現在選擇他
設定名稱、預測欄位 (以此資料集為例 選y) 、計算機
適當的調整國家、電腦叢集computer cluster
設定最大可用虛擬機數量
選擇問題種類 分類問題 (可勾選加入基礎深度學習模型) 、回歸問題、時間序列問題
bankmarketing資料集是一個分類問題 (是/否)
設定驗證方式
選擇執行 2次 交叉比對 驗證20%資料

設定完成交給他跑
Models>依照AUC weighted排序 找出合適演算法與模型 選取
Deploy>Deploy to web service
也可直接下載打包帶走
建立web service
Compute type>Azure Container Instance (自己選擇)

Microsoft Azure Machine Learning Studio介面>End points
看到已經成功建立
複製REST endpoint

-
使用python輸入資料進行預測
url填入剛剛複製的REST endpoint

import urllib.request
import json
import os
import ssl

def allowSelfSignedHttps(allowed):
    if allowed and not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None):
        ssl._create_default_https_context = ssl._create_unverified_context

allowSelfSignedHttps(True)

data = {
    "Inputs": {
        "data":
        [
            {
                'age': "34",
                'job': "admin.",
                'marital': "single",
                'education': "university.degree",
                'default': "no",
                'housing': "yes",
                'loan': "no",
                'contact': "cellular",
                'month': "jul",
                'duration': "2122",
                'campaign': "1",
                'pdays': "999",
                'previous': "0",
                'poutcome': "nonexistent",
                'emp.var.rate': "1.4",
                'cons.price.idx': "93.918",
                'cons.conf.idx': "-42.7",
                'euribor3m': "4.962",
                'nr.employed': "5228.1",
            },
        ],
    },
    "GlobalParameters": {
        'method': "predict",
    }
}

body = str.encode(json.dumps(data))

url = 'http://3720979e-f930-4205-bb42-66029bf804b6.centralus.azurecontainer.io/score'
api_key = ''
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}

req = urllib.request.Request(url, body, headers)

try:
    response = urllib.request.urlopen(req)

    result = response.read()
    print(result)
except urllib.error.HTTPError as error:
    print("The request failed with status code: " + str(error.code))
    print(error.info())
    print(json.loads(error.read().decode("utf8", 'ignore')))

b'{"Results": ["yes"]}'

成功預測

-
使用PowerBI視覺化運算結果

取得資料>csv bankmarketing_train.csv 載入
常用>轉換資料 進入Power Query
Power Query的常用>AI見解 Azure Machine Learning
選擇剛剛的建立web service的模型
略過安全性檢查
可以看到 原始資料y 與 預測結果AzureML.mlws 欄位
關閉並套用 回到PowerBI
以資料顯示>新建資料行
jugde = IF([y]=[AzureML.mlws],"t","f")
建立混淆矩陣

沒有要繼續付錢就一樣把資料群組給刪了
PowerBI也可以關了



2.Azure電腦視覺 api

事前安裝:
Visual Studio 2022
Azure開發、.NET桌面開發

開啟Azure網站 > 建立資源
這部分會使用
資源群組 電腦視覺

-
資源群組>建立

電腦視覺>建立
選擇剛建立的資源群組
選擇一個所在地區 (避免出問題 這邊地區皆選美中)
想當個免費仔所以選擇免費層

電腦視覺>金鑰與端點
複製金鑰與端點

-
開啟Visual Studio 2022

建立新的專案> C# Windows 桌面>選擇Windows Forms App (.NET Framework)
專案>右鍵>管理NuGet套件
安裝Microsoft.Azure.CognitiveServices.Vision.ComputerVision 5.0.0版

設計介面

cvApiUrl填入端點
cvApiKey填入金鑰

接上API


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 Newtonsoft.Json;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;


namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        string imagePath, cvApiUrl, cvApiKey;
        public Form1()
        {
            InitializeComponent();
            cvApiUrl = "https://chiahsin-cv.cognitiveservices.azure.com/";
            cvApiKey = "70387db2782f4433bb19dd1a03201ab0";
        }

        private async Task AnalysisImageAsync(string _imagePath, string _cvApiUrl, string _cvApiKey)
        {
            try
            {
                //建立FileStream物件fs開啟圖檔
                FileStream fs = File.Open(_imagePath, FileMode.Open);
                //圖片路徑顯示在textBox1
                textBox1.Text = _imagePath;

                //建立電腦視覺辦識物件,同時指定電腦視覺辦識的雲端服務Key
                ComputerVisionClient visionClient = new ComputerVisionClient(
                    new ApiKeyServiceClientCredentials(_cvApiKey),
                    new System.Net.Http.DelegatingHandler[] { });

                //電腦視覺辦識物件指定雲端服務Api位址
                visionClient.Endpoint = _cvApiUrl;
                // 進行圖片的辨識
                ImageDescription objResult = await visionClient.DescribeImageInStreamAsync(fs);

                // 若辨識失敗則傳回null
                if (objResult == null)
                    return;

                // 放入辨識分析完之後的JSON所有內容
                richTextBox1.Text = JsonConvert.SerializeObject(objResult);

                //釋放檔案資源
                fs.Close();
                fs.Dispose();
                //panel1面板顯示指定的圖片
                panel1.BackgroundImageLayout = ImageLayout.Stretch;
                panel1.BackgroundImage = Image.FromFile(_imagePath);
            }
            catch (Exception e)
            {
                richTextBox2.Text = e.Message;
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.panel1 = new System.Windows.Forms.Panel();
            this.richTextBox1 = new System.Windows.Forms.TextBox();
            this.richTextBox2 = new System.Windows.Forms.TextBox();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.SuspendLayout();
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(41, 27);
            this.textBox1.Name = "textBox1";
            this.textBox1.ReadOnly = true;
            this.textBox1.Size = new System.Drawing.Size(479, 27);
            this.textBox1.TabIndex = 0;
            this.textBox1.UseWaitCursor = true;
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(542, 26);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(114, 24);
            this.button1.TabIndex = 1;
            this.button1.Text = "選取圖片";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // panel1
            //
            this.panel1.Location = new System.Drawing.Point(27, 79);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(360, 267);
            this.panel1.TabIndex = 2;
            this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
            //
            // richTextBox1
            //
            this.richTextBox1.Location = new System.Drawing.Point(406, 82);
            this.richTextBox1.Multiline = true;
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.ReadOnly = true;
            this.richTextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.richTextBox1.Size = new System.Drawing.Size(270, 156);
            this.richTextBox1.TabIndex = 3;
            //
            // richTextBox2
            //
            this.richTextBox2.Location = new System.Drawing.Point(407, 260);
            this.richTextBox2.Multiline = true;
            this.richTextBox2.Name = "richTextBox2";
            this.richTextBox2.ReadOnly = true;
            this.richTextBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.richTextBox2.Size = new System.Drawing.Size(270, 86);
            this.richTextBox2.TabIndex = 4;
            //
            // openFileDialog1
            //
            this.openFileDialog1.FileName = "openFileDialog1";
            //
            // Form1
            //
            this.ClientSize = new System.Drawing.Size(704, 390);
            this.Controls.Add(this.richTextBox2);
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private async void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                imagePath = openFileDialog1.FileName;
                await AnalysisImageAsync(imagePath, cvApiUrl, cvApiKey);
            }
        }
    }
}


圖像分析

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 Newtonsoft.Json;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;


namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        string imagePath, cvApiUrl, cvApiKey;
        public Form1()
        {
            InitializeComponent();
            cvApiUrl = "https://chiahsin-cv.cognitiveservices.azure.com/";
            cvApiKey = "70387db2782f4433bb19dd1a03201ab0";
        }

        private async Task AnalysisImageAsync(string _imagePath, string _cvApiUrl, string _cvApiKey)
        {
            try
            {
                //建立FileStream物件fs開啟圖檔
                FileStream fs = File.Open(_imagePath, FileMode.Open);
                //圖片路徑顯示在textBox1
                textBox1.Text = _imagePath;

                VisualFeatureTypes[] visualFeatures = new VisualFeatureTypes[] {
                    VisualFeatureTypes.ImageType,VisualFeatureTypes.Color,VisualFeatureTypes.Faces,VisualFeatureTypes.Adult ,
                    VisualFeatureTypes.Categories,VisualFeatureTypes.Tags,VisualFeatureTypes.Description,VisualFeatureTypes.Objects,
                    VisualFeatureTypes.Brands
                };

                //建立電腦視覺辦識物件,同時指定電腦視覺辦識的雲端服務Key
                ComputerVisionClient visionClient = new ComputerVisionClient(
                    new ApiKeyServiceClientCredentials(_cvApiKey),
                    new System.Net.Http.DelegatingHandler[] { });

                //電腦視覺辦識物件指定雲端服務Api位址
                visionClient.Endpoint = _cvApiUrl;
                // 進行圖片的辨識
                ImageAnalysis objResult =await visionClient.AnalyzeImageInStreamAsync(fs, visualFeatures);

                // 若辨識失敗則傳回null
                if (objResult == null)
                    return;

                // 放入辨識分析完之後的JSON所有內容
                richTextBox1.Text = JsonConvert.SerializeObject(objResult);
                richTextBox2.Text = JsonConvert.SerializeObject(objResult.Categories);

                //釋放檔案資源
                fs.Close();
                fs.Dispose();
                //panel1面板顯示指定的圖片
                panel1.BackgroundImageLayout = ImageLayout.Stretch;
                panel1.BackgroundImage = Image.FromFile(_imagePath);
            }
            catch (Exception e)
            {
                richTextBox2.Text = e.Message;
            }
        }

        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.panel1 = new System.Windows.Forms.Panel();
            this.richTextBox1 = new System.Windows.Forms.TextBox();
            this.richTextBox2 = new System.Windows.Forms.TextBox();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.SuspendLayout();
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(41, 27);
            this.textBox1.Name = "textBox1";
            this.textBox1.ReadOnly = true;
            this.textBox1.Size = new System.Drawing.Size(479, 23);
            this.textBox1.TabIndex = 0;
            this.textBox1.UseWaitCursor = true;
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(542, 26);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(114, 24);
            this.button1.TabIndex = 1;
            this.button1.Text = "選取圖片";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // panel1
            //
            this.panel1.Location = new System.Drawing.Point(27, 79);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(360, 267);
            this.panel1.TabIndex = 2;
            //
            // richTextBox1
            //
            this.richTextBox1.Location = new System.Drawing.Point(406, 82);
            this.richTextBox1.Multiline = true;
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.ReadOnly = true;
            this.richTextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.richTextBox1.Size = new System.Drawing.Size(270, 156);
            this.richTextBox1.TabIndex = 3;
            //
            // richTextBox2
            //
            this.richTextBox2.Location = new System.Drawing.Point(407, 260);
            this.richTextBox2.Multiline = true;
            this.richTextBox2.Name = "richTextBox2";
            this.richTextBox2.ReadOnly = true;
            this.richTextBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.richTextBox2.Size = new System.Drawing.Size(270, 86);
            this.richTextBox2.TabIndex = 4;
            //
            // openFileDialog1
            //
            this.openFileDialog1.FileName = "openFileDialog1";
            //
            // Form1
            //
            this.ClientSize = new System.Drawing.Size(704, 390);
            this.Controls.Add(this.richTextBox2);
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private async void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                imagePath = openFileDialog1.FileName;
                await AnalysisImageAsync(imagePath, cvApiUrl, cvApiKey);
            }
        }
    }
}


臉部辨識

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 Newtonsoft.Json;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;


namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        string imagePath, cvApiUrl, cvApiKey,type;
        List<Rectangle> list = new List<Rectangle>();
        List<List<int>> coordinate = new List<List<int>>();
        List<int> list_age = new List<int>();
        List<string> list_gender = new List<string>();

        public Form1()
        {
            InitializeComponent();
            cvApiUrl = "https://chiahsin-cv.cognitiveservices.azure.com/";
            cvApiKey = "70387db2782f4433bb19dd1a03201ab0";
        }

        private async Task AnalysisImageAsync(string _imagePath, string _cvApiUrl, string _cvApiKey)
        {
            try
            {
                //建立FileStream物件fs開啟圖檔
                FileStream fs = File.Open(_imagePath, FileMode.Open);
                //圖片路徑顯示在textBox1
                textBox1.Text = _imagePath;

                VisualFeatureTypes[] visualFeatures = new VisualFeatureTypes[] {
                    VisualFeatureTypes.ImageType,VisualFeatureTypes.Color,VisualFeatureTypes.Faces,VisualFeatureTypes.Adult ,
                    VisualFeatureTypes.Categories,VisualFeatureTypes.Tags,VisualFeatureTypes.Description,VisualFeatureTypes.Objects,
                    VisualFeatureTypes.Brands
                };

                //建立電腦視覺辦識物件,同時指定電腦視覺辦識的雲端服務Key
                ComputerVisionClient visionClient = new ComputerVisionClient(
                    new ApiKeyServiceClientCredentials(_cvApiKey),
                    new System.Net.Http.DelegatingHandler[] { });

                //電腦視覺辦識物件指定雲端服務Api位址
                visionClient.Endpoint = _cvApiUrl;
                // 進行圖片的辨識
                ImageAnalysis objResult =await visionClient.AnalyzeImageInStreamAsync(fs, visualFeatures);

                // 若辨識失敗則傳回null
                if (objResult == null)
                    return;

                // 放入辨識分析完之後的JSON所有內容
                richTextBox1.Text = JsonConvert.SerializeObject(objResult);
                richTextBox2.Text = JsonConvert.SerializeObject(objResult.Faces);

                float px = objResult.Metadata.Width / panel1.Width;
                float py = objResult.Metadata.Height / panel1.Height;

                foreach (var face in objResult.Faces)
                {
                    var rct = new Rectangle((int)((face.FaceRectangle.Left-face.FaceRectangle.Width/2) / px),
                        (int)(face.FaceRectangle.Top / py), (int)(face.FaceRectangle.Width / px), (int)(face.FaceRectangle.Height / py));
                    coordinate.Add(new List<int>() { (int)((face.FaceRectangle.Left - face.FaceRectangle.Width / 2) / py), (int)(face.FaceRectangle.Top / px - 15)});
                    list.Add(rct);
                    list_age.Add(face.Age);
                    type = face.Gender.ToString();
                    if (face.Gender.HasValue)
                    {
                        list_gender.Add(face.Gender.ToString());
                    } else
                    {
                        list_gender.Add("無法辨識");
                    }
                }                

                //釋放檔案資源
                fs.Close();
                fs.Dispose();
                //panel1面板顯示指定的圖片
                panel1.BackgroundImageLayout = ImageLayout.Stretch;
                panel1.BackgroundImage = Image.FromFile(_imagePath);
            }
            catch (Exception e)
            {
                richTextBox2.Text = e.Message;
            }
        }

        private void richTextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Pen myPen = new Pen(Color.Blue, 2);
            Graphics dc = panel1.CreateGraphics();            
            foreach (Rectangle element in list)
            {
                dc.DrawRectangle(myPen, element);
            }
            for (int i = 0; i<list.Count; i++)
            {
                dc.DrawRectangle(myPen, list[i]);
                Label textControl = new Label();
                textControl.Text = list_age[i]+"歲,"+list_gender[i];
                textControl.BackColor = System.Drawing.Color.Transparent;
                textControl.Font = new Font("Arial", 8);
                textControl.AutoSize = true;
                textControl.Location = new Point(coordinate[i][0], coordinate[i][1]);
                textControl.ForeColor = Color.Red;
                panel1.Controls.Add(textControl);
            }

        }

        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.panel1 = new System.Windows.Forms.Panel();
            this.richTextBox1 = new System.Windows.Forms.TextBox();
            this.richTextBox2 = new System.Windows.Forms.TextBox();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.SuspendLayout();
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(41, 27);
            this.textBox1.Name = "textBox1";
            this.textBox1.ReadOnly = true;
            this.textBox1.Size = new System.Drawing.Size(479, 27);
            this.textBox1.TabIndex = 0;
            this.textBox1.UseWaitCursor = true;
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(542, 26);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(114, 24);
            this.button1.TabIndex = 1;
            this.button1.Text = "選取圖片";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // panel1
            //
            this.panel1.Location = new System.Drawing.Point(27, 79);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(360, 267);
            this.panel1.TabIndex = 2;
            this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
            //
            // richTextBox1
            //
            this.richTextBox1.Location = new System.Drawing.Point(406, 82);
            this.richTextBox1.Multiline = true;
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.ReadOnly = true;
            this.richTextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.richTextBox1.Size = new System.Drawing.Size(270, 156);
            this.richTextBox1.TabIndex = 3;
            this.richTextBox1.TextChanged += new System.EventHandler(this.richTextBox1_TextChanged);
            //
            // richTextBox2
            //
            this.richTextBox2.Location = new System.Drawing.Point(407, 260);
            this.richTextBox2.Multiline = true;
            this.richTextBox2.Name = "richTextBox2";
            this.richTextBox2.ReadOnly = true;
            this.richTextBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.richTextBox2.Size = new System.Drawing.Size(270, 86);
            this.richTextBox2.TabIndex = 4;
            this.richTextBox2.TextChanged += new System.EventHandler(this.richTextBox2_TextChanged);
            //
            // openFileDialog1
            //
            this.openFileDialog1.FileName = "openFileDialog1";
            //
            // Form1
            //
            this.ClientSize = new System.Drawing.Size(704, 390);
            this.Controls.Add(this.richTextBox2);
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private async void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                imagePath = openFileDialog1.FileName;
                await AnalysisImageAsync(imagePath, cvApiUrl, cvApiKey);
                panel1.Invalidate();
            }
        }
    }
}

(主要是圖片經過變形塞入格子中 導致框框看起來跑掉了)

文件辨識

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 Newtonsoft.Json;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;


namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        string imagePath, cvApiUrl, cvApiKey;
        List<Rectangle> list = new List<Rectangle>();
        List<List<int>> coordinate = new List<List<int>>();
        List<int> list_age = new List<int>();
        List<string> list_gender = new List<string>();

        public Form1()
        {
            InitializeComponent();
            cvApiUrl = "https://chiahsin-cv.cognitiveservices.azure.com/";
            cvApiKey = "70387db2782f4433bb19dd1a03201ab0";
        }

        private async Task AnalysisImageAsync(string _imagePath, string _cvApiUrl, string _cvApiKey)
        {
            const int numberOfCharsInOperationId = 36;
            try
            {
                //建立FileStream物件fs開啟圖檔
                FileStream fs = File.Open(_imagePath, FileMode.Open);
                //圖片路徑顯示在textBox1
                textBox1.Text = _imagePath;

                //建立電腦視覺辦識物件,同時指定電腦視覺辦識的雲端服務Key
                ComputerVisionClient visionClient = new ComputerVisionClient(
                    new ApiKeyServiceClientCredentials(_cvApiKey),
                    new System.Net.Http.DelegatingHandler[] { });

                //電腦視覺辦識物件指定雲端服務Api位址
                visionClient.Endpoint = _cvApiUrl;
                // 進行圖片的辨識
                BatchReadFileInStreamHeaders objResult = await visionClient.BatchReadFileInStreamAsync(fs);
                await GetTextAsync(visionClient, objResult.OperationLocation, numberOfCharsInOperationId);

                // 若辨識失敗則傳回null
                if (objResult == null)
                    return;

                // 放入辨識分析完之後的JSON所有內容
                richTextBox2.Text = JsonConvert.SerializeObject(objResult);

                //釋放檔案資源
                fs.Close();
                fs.Dispose();
                //panel1面板顯示指定的圖片
                panel1.BackgroundImageLayout = ImageLayout.Stretch;
                panel1.BackgroundImage = Image.FromFile(_imagePath);
            }
            catch (Exception e)
            {
                richTextBox2.Text = e.Message;
            }
        }

        private async Task GetTextAsync(ComputerVisionClient computerVision, string operationLocation, int numberOfCharsInOperationId)
        {
            string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

            ReadOperationResult result = await computerVision.GetReadOperationResultAsync(operationId);

            int i = 0;
            int maxRetries = 10;
            while ((result.Status == TextOperationStatusCodes.Running ||
                    result.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries)
            {
                richTextBox1.Text = "Server status:"+ result.Status+", waiting "+i+" seconds...";
                await Task.Delay(1000);
                result = await computerVision.GetReadOperationResultAsync(operationId);
            }

            var recResults = result.RecognitionResults;
            foreach (TextRecognitionResult recResult in recResults)
            {
                foreach (Line line in recResult.Lines)
                {
                    richTextBox1.Text= richTextBox1.Text + Environment.NewLine + line.Text;
                }
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            /*Pen myPen = new Pen(Color.Blue, 2);
            Graphics dc = panel1.CreateGraphics();            
            foreach (Rectangle element in list)
            {
                dc.DrawRectangle(myPen, element);
            }
            for (int i = 0; i<list.Count; i++)
            {
                dc.DrawRectangle(myPen, list[i]);
                Label textControl = new Label();
                textControl.Text = list_age[i]+"歲,"+list_gender[i];
                textControl.BackColor = System.Drawing.Color.Transparent;
                textControl.Font = new Font("Arial", 8);
                textControl.AutoSize = true;
                textControl.Location = new Point(coordinate[i][0], coordinate[i][1]);
                textControl.ForeColor = Color.Red;
                panel1.Controls.Add(textControl);
            }*/

        }

        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.panel1 = new System.Windows.Forms.Panel();
            this.richTextBox1 = new System.Windows.Forms.TextBox();
            this.richTextBox2 = new System.Windows.Forms.TextBox();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.SuspendLayout();
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(41, 27);
            this.textBox1.Name = "textBox1";
            this.textBox1.ReadOnly = true;
            this.textBox1.Size = new System.Drawing.Size(479, 23);
            this.textBox1.TabIndex = 0;
            this.textBox1.UseWaitCursor = true;
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(542, 26);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(114, 24);
            this.button1.TabIndex = 1;
            this.button1.Text = "選取圖片";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // panel1
            //
            this.panel1.Location = new System.Drawing.Point(27, 79);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(360, 267);
            this.panel1.TabIndex = 2;
            this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
            //
            // richTextBox1
            //
            this.richTextBox1.Location = new System.Drawing.Point(406, 82);
            this.richTextBox1.Multiline = true;
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.ReadOnly = true;
            this.richTextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.richTextBox1.Size = new System.Drawing.Size(270, 156);
            this.richTextBox1.TabIndex = 3;
            //
            // richTextBox2
            //
            this.richTextBox2.Location = new System.Drawing.Point(407, 260);
            this.richTextBox2.Multiline = true;
            this.richTextBox2.Name = "richTextBox2";
            this.richTextBox2.ReadOnly = true;
            this.richTextBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.richTextBox2.Size = new System.Drawing.Size(270, 86);
            this.richTextBox2.TabIndex = 4;
            //
            // openFileDialog1
            //
            this.openFileDialog1.FileName = "openFileDialog1";
            //
            // Form1
            //
            this.ClientSize = new System.Drawing.Size(704, 390);
            this.Controls.Add(this.richTextBox2);
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private async void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                imagePath = openFileDialog1.FileName;
                await AnalysisImageAsync(imagePath, cvApiUrl, cvApiKey);
                panel1.Invalidate();
            }
        }
    }
}


-
使用Python

import matplotlib.pyplot as plt
import requests
import time

# pip install azureml-core
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from msrest.authentication import CognitiveServicesCredentials

subscription_key填入金鑰
endpoint填入端點

# modify ur key and endpoint
subscription_key = '70387db2782f4433bb19dd1a03201ab0'
endpoint = 'https://chiahsin-cv.cognitiveservices.azure.com/'

computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))

圖片辨識 Tagging

def CVapi(img, client):
    tags_result_remote = client.tag_image(img)
    print("Tags in the remote image: ")
    if (len(tags_result_remote.tags) == 0):
        print("No tags detected.")
    else:
        for tag in tags_result_remote.tags:
            print("'{}' with confidence {:.2f}%".format(tag.name, tag.confidence * 100))
    print()
    print("End of Computer Vision quickstart.")
    return tags_result_remote

IMGURL='https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/landmark.jpg'
img = plt.imread(requests.get(IMGURL, stream=True).raw, format='jpg')
fig, ax = plt.subplots()
ax.imshow(img)
read_result=CVapi(IMGURL, computervision_client)
plt.show()

Tags in the remote image:
'outdoor' with confidence 99.00%
'building' with confidence 98.81%
'sky' with confidence 98.21%
'stadium' with confidence 98.17%
'ancient rome' with confidence 96.16%
'ruins' with confidence 95.04%
'amphitheatre' with confidence 93.99%
'ancient roman architecture' with confidence 92.65%
'historic site' with confidence 89.55%
'ancient history' with confidence 89.54%
'history' with confidence 86.72%
'archaeological site' with confidence 84.41%
'travel' with confidence 65.85%
'large' with confidence 61.02%
'city' with confidence 56.57%

End of Computer Vision quickstart.

光學字元辨識

def OCRapi(img_url, client):
    read_response = client.read(img_url,  raw=True)
    read_operation_location = read_response.headers["Operation-Location"]
    operation_id = read_operation_location.split("/")[-1]

    while True:
        read_result = client.get_read_result(operation_id)
        if read_result.status not in ['notStarted', 'running']:
            break
        time.sleep(1)

    return read_result

def convert_points_to_lines(points):
    x = points[::2]
    y = points[1::2]
    x.append(x[0])
    y.append(y[0])
    return x,y

IMGURL="https://raw.githubusercontent.com/MicrosoftDocs/azure-docs/master/articles/cognitive-services/Computer-vision/Images/readsample.jpg"
img = plt.imread(requests.get(IMGURL, stream=True).raw, format='jpg')
fig, ax = plt.subplots()
ax.imshow(img)

read_result=OCRapi(IMGURL, computervision_client)

for text_result in read_result.analyze_result.read_results:
    for line in text_result.lines:
        print(line.text)
        x,y = convert_points_to_lines(line.bounding_box)
        plt.plot(x,y,c='r')

plt.show()

The quick brown fox jumps
over the lazy dog!

創作回應

相關創作

更多創作