1. 程式人生 > >C#Winfrom和PYTHON接入騰訊雲OCR

C#Winfrom和PYTHON接入騰訊雲OCR

程式基本目的是實現提取圖片中的文字。做了一個winfrom介面,接了騰訊雲的OCR圖片識別。
這裡接入文件就不給出了,想看的直接去騰訊雲看接入文件即可。

由於想做跨平臺的應用。分析了接入文件,並且之前想做一個web介面介面的,但是無奈javascript技術不到家,表單傳遞時的header頭問題遲遲沒有解決。
所有就在我的網站里弄了一個介面來計算我的Authorization,計算方法是直接採用騰訊給出的標準文件中的方法。直接把計算結果echo到web上。
而我windows上採用的是C#程式設計。主要想法就是先使用HTTPREQUEST方法做一個簡單的爬蟲。具體方法在我之前的部落格《C#使用Winfrom程式設計時的注意事項》有講過到。具體連結:

https://blog.csdn.net/qq_27180763/article/details/82949365
主要還是卡在了header傳值的問題。由於Authorization不是標準Header值,需要採用別的方式來進行傳遞。這是比較需要注意的。
這個地方我主要採用了Content-Type=application/x-www-form-urlencoded,用POST方法把上傳檔案到騰訊雲的API介面,然後接受返回值而已。
接受的時候也要注意,由於收到的資訊是JSON碼的形式,需要自己下載一個Newtonsoft.Json.dll的檔案,並加入解決方案的引用當中。然後名稱空間引用

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

這樣就可以直接使用JObject物件來轉換string型的json物件。並採用json標準來進行物件的訪問。

C# Winfrom的介面就不具體放出,我先給出C#的程式碼。

Windows下的程式碼

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace PDF轉WORD
{

    public partial class Form2 : Form
    {
        private string filename="";
        public Form2()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            this.Text = "圖片轉PDF";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "請選擇檔案";
            fileDialog.Filter = "(*jpg*)|*.jpg*||(*.jpg*)||*.*";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string file = fileDialog.FileName;
                filename = file;
                label2.Text = filename.ToString();
                MessageBox.Show("選擇成功!");
            }
        }

        private void POST_DATA() {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(@"我的web伺服器"));
            req.Method = "POST";
            req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0";
            req.ContentType = "application/x-www-form-urlencoded";
            req.Accept = "application/json";
            string result1 = "";

            StringBuilder builder = new StringBuilder();

            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            { reqStream.Write(data, 0, data.Length); reqStream.Close(); }
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //獲取響應內容         
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            { result1 = reader.ReadToEnd(); }

            HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri("http://recognition.image.myqcloud.com/ocr/general"));
            Stream memStream = new MemoryStream();
            webReq.Method = "POST";
            string boundary = "--------------" + DateTime.Now.Ticks.ToString("x");// 邊界符 
            webReq.ContentType = "multipart/form-data; boundary=" + boundary;
            byte[] enter = Encoding.ASCII.GetBytes("\r\n");  //換行
            memStream.Write(enter, 0, enter.Length);
            Dictionary<string, string> dic = new Dictionary<string, string>()
            {
                {"appid","你的APPID"}
            };
            //寫入文字欄位
            string inputPartHeaderFormat = "--" + boundary + "\r\n" + "Content-Disposition:form-data;name=\"{0}\";" + "\r\n\r\n{1}\r\n";
            foreach (var kv in dic)
            {
                string inputPartHeader = string.Format(inputPartHeaderFormat, kv.Key, kv.Value);
                var inputPartHeaderBytes = Encoding.ASCII.GetBytes(inputPartHeader);
                memStream.Write(inputPartHeaderBytes, 0, inputPartHeaderBytes.Length);
            }

            var fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
            // 寫入檔案 
            string imagePartHeader = "--" + boundary + "\r\n" +
                                     "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
                                     "Content-Type: image/jpeg\r\n\r\n";
            var header = string.Format(imagePartHeader, "image", "1.jpg");
            var headerbytes = Encoding.UTF8.GetBytes(header);
            memStream.Write(headerbytes, 0, headerbytes.Length);
            var buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                memStream.Write(buffer, 0, bytesRead);
            }
            byte[] endBoundary = Encoding.ASCII.GetBytes("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "\r\n" + boundary + "--\r\n");
            memStream.Write(endBoundary, 0, endBoundary.Length);
            webReq.ContentLength = memStream.Length;
            webReq.Headers.Add(HttpRequestHeader.Authorization, result1);
            webReq.Host = "recognition.image.myqcloud.com";
            var requestStream = webReq.GetRequestStream();
            memStream.Position = 0;
            memStream.CopyTo(requestStream);
            HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            var ret = sr.ReadToEnd();
            sr.Close();
            response.Close();
            requestStream.Close();
            memStream.Close();
            JObject content_info = JObject.Parse(ret);
//這裡就是主要的對返回的json的處理了
            for (int i = 0; i < content_info["data"]["items"].Count(); i++)
            {
                try
                {
                    richTextBox1.AppendText(content_info["data"]["items"][i]["itemstring"].ToString());
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(filename!="")
            {
                Thread th1 = new Thread(POST_DATA);
                th1.IsBackground = true;
                th1.Start();
            }
            else {
                MessageBox.Show("請先選擇圖片。", "提示!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            filename = "";
            if (filename == "")
            {
                label2.Text = "暫未選擇圖片";
                MessageBox.Show("清空成功!");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
        }
    }
}

以上就是主要介面的程式碼。而主介面的程式碼就不再放出,我先給出windows下的實驗效果。(照片帶水印)

Windows下的效果

在這裡插入圖片描述

在這裡插入圖片描述

LINUX上的程式碼實現(ubuntu)

用python實現的,所以程式碼比較簡潔。主要用到的函式庫為requests
沒什麼需要注意的==畢竟python比較簡單。直接安裝官方給的文件設定image和header就可以了。話不多說直接上程式碼QWQ

#!/usr/bin/env python
#encoding=utf-8
import re
import sys,os
import requests
def GetAuthorization():
	html = requests.post(url="我的WEB伺服器地址")
	html = html.text
	info = html.split("\n")
	return info[1].replace("\n","")

Authorization = str(GetAuthorization().replace("\n","").replace("\r",""))
def createRequest(filepath):
	global Authorization
	headers = {
		'host':'recognition.image.myqcloud.com',
		'Authorization':Authorization
	}
	files = {
		'appid':(None,'你的appid'),
		'image':('1.jpg',open(filepath,'rb'),'image/jpeg')
	}

	r = requests.post("http://recognition.image.myqcloud.com/ocr/general", files=files,headers=headers)	
	responseinfo = r.content
	data = responseinfo.decode('utf-8')
	print data	
	r_index = r'itemstring":"(.*?)"'
	result = re.findall(r_index, data)
	for i in result:
	    print(i)

def main():
	createRequest(sys.argv[1])


if __name__ == '__main__':
	main()

程式的第一個引數就是你要上傳檔案的絕對路徑==就是一個普通指令碼,沒有什麼很大的技術難度。
我要處理的照片還是上面那張,現在給出掃描結果。

LINUX下的效果圖

在這裡插入圖片描述