PaddleOCRHelper.cs 4.2 KB
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BLL
{
    public class PaddleOCRHelper
    {
        //static string baseUrl = ConfigHelper.Config.Get("PaddleServiceBase", "http://localhost:8090/paddle/getOcr");
        static string baseUrl = ConfigHelper.Config.Get("paddleOcr_HttpPath", "http://localhost:8090/paddle/getOcr"); 
        /// <summary>
        /// 开始检测
        /// </summary>
        /// <param name="pythonExePath">python解释器路径</param>
        /// <param name="pythonFile">python文件</param>
        /// <param name="imgPath">图像文件路径</param>
        /// <returns></returns>
        public static string StartTest(string imgPath)
        {
            string ocr = "";
            for (int i = 0; i < 2; i++)
            {
                if (!AppIsRun())
                {
                    LogNet.log.Info("ocr程序未允许,启动");
                    try
                    {
                        var paddle = "paddleOCR.exe";
                        Process process = new Process();
                        process.StartInfo = new ProcessStartInfo();
                        process.StartInfo.FileName = paddle;
                        process.StartInfo.WorkingDirectory = Application.StartupPath + "\\paddle";
                        process.Start();
                    }
                    catch (Exception ex)
                    {
                        LogNet.log.Error("打开paddleOCR失败", ex);
                    }
                    Thread.Sleep(5000);
                }
                if (AppIsRun())
                {
                    try
                    {
                        ocr = StartCplusOcr(imgPath);
                        break;
                    }
                    catch (Exception ex) {
                        LogNet.log.Error("OCR失败", ex);
                        AppKill();
                    }
                }
            }
            return ocr;
        }

        [HandleProcessCorruptedStateExceptions]
      public  static string StartCplusOcr(string imgPath)
        {
            LogNet.log.Error("进入OcrGet请求");
            string json=Http.Get($"{baseUrl}?ver=cplus&imgPath={imgPath}");
            LogNet.log.Error($"返回数据为:{json}");
            Result result= JsonConvert.DeserializeObject<Result>(json);
            return result?.data??"";
        }
       public static string StartPythonOcr(string imgPath)
        {
            if (!AppIsRun())
            {
                var onnxexe = ".\\paddle\\paddleOCR.exe";
                Process.Start(onnxexe);
                Thread.Sleep(2000);
            }
            string json = Http.Get($"{baseUrl}?ver=python&imgPath={imgPath}");
            Result result = JsonConvert.DeserializeObject<Result>(json);
            return result?.data ?? "";
        }
        static bool AppIsRun()
        {
            Process[] processes = Process.GetProcessesByName("paddleOCR");
            if (processes.Length > 0)
            {
                return true;
            }
            LogNet.log.Info("paddleOCR 未在运行,启动程序");
            return false;
        }
        static void AppKill()
        {
            Process[] processes = Process.GetProcessesByName("paddleOCR");
            foreach(var p in processes)
            {
                p.Kill();
            }
            LogNet.log.Info("paddleOCR AppKill");
        }
        public class Result
        {
            /// <summary>
            /// 状态码,0为正常
            /// </summary>
            public int code { get; set; } = 0;
            /// <summary>
            /// 返回数据
            /// </summary>
            public string data { get; set; } = "";
            /// <summary>
            /// 提示信息
            /// </summary>
            public string msg { get; set; } = "ok";
            /// <summary>
            /// 版本
            /// </summary>
            public string ver { get; set; } = "";

        }
    }
}