ORC
## Windows: Detection and Recognition(All)
1. Install NuGet Packages:
```
Sdcb.PaddleInference
Sdcb.PaddleInference.runtime.win64.mkl
Sdcb.PaddleOCR
Sdcb.PaddleOCR.KnownModels
OpenCvSharp4
OpenCvSharp4.runtime.win
```
2. Using following C# code to get result:
```csharp
OCRModel model = KnownOCRModel.PPOcrV2;
await model.EnsureAll();
byte[] sampleImageData;
string sampleImageUrl = @"https://www.tp-link.com.cn/content/images/detail/2164/TL-XDR5450易展Turbo版-3840px_03.jpg";
using (HttpClient http = new HttpClient())
{
Console.WriteLine("Download sample image from: " + sampleImageUrl);
sampleImageData = await http.GetByteArrayAsync(sampleImageUrl);
}
using (PaddleOcrAll all = new PaddleOcrAll(model.RootDirectory, model.KeyPath)
{
AllowRotateDetection = true, /* 允许识别有角度的文字 */
Enable180Classification = false, /* 允许识别旋转角度大于90度的文字 */
})
{
// Load local file by following code:
// using (Mat src2 = Cv2.ImRead(@"C:\test.jpg"))
using (Mat src = Cv2.ImDecode(sampleImageData, ImreadModes.Color))
{
PaddleOcrResult result = all.Run(src);
Console.WriteLine("Detected all texts: \n" + result.Text);
foreach (PaddleOcrResultRegion region in result.Regions)
{
Console.WriteLine($"Text: {region.Text}, Score: {region.Score}, RectCenter: {region.Rect.Center}, RectSize: {region.Rect.Size}, Angle: {region.Rect.Angle}");
}
}
}
```
## 仅检测Detection Only
```csharp
// Install following packages:
// Sdcb.PaddleInference
// Sdcb.PaddleInference.runtime.win64.mkl (required in Windows)
// Sdcb.PaddleOCR
// Sdcb.PaddleOCR.KnownModels
// OpenCvSharp4
// OpenCvSharp4.runtime.win (required in Windows)
// OpenCvSharp4.runtime.linux18.04 (required in Linux)
byte[] sampleImageData;
string sampleImageUrl = @"https://www.tp-link.com.cn/content/images/detail/2164/TL-XDR5450易展Turbo版-3840px_03.jpg";
using (HttpClient http = new HttpClient())
{
Console.WriteLine("Download sample image from: " + sampleImageUrl);
sampleImageData = await http.GetByteArrayAsync(sampleImageUrl);
}
OCRModel model = KnownOCRModel.PPOcrV2;
await model.EnsureAll();
using (PaddleOcrDetector detector = new PaddleOcrDetector(model.DetectionDirectory))
using (Mat src = Cv2.ImDecode(sampleImageData, ImreadModes.Color))
{
RotatedRect[] rects = detector.Run(src);
using (Mat visualized = PaddleOcrDetector.Visualize(src, rects, Scalar.Red, thickness: 2))
{
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "output.jpg");
Console.WriteLine("OutputFile: " + outputFile);
visualized.ImWrite(outputFile);
}
}
```
# Language supports
| Language | 中文名 | Code |
| -------------------- | ------------------ | ---------------------------------- |
| Chinese | 简体中文 | `KnownOCRModel.PPOcrV2` |
| Chinese Server | 简体中文(服务器版) | `KnownOCRModel.PPOcrServerV2` |
| English | 英文 | `KnownOCRModel.EnglishMobileV2` |
| Tranditional Chinese | 繁体中文 | `KnownOCRModel.EnglishMobileV2` |
| French | 法文 | `KnownOCRModel.FrenchMobileV2` |
| German | 德文 | `KnownOCRModel.GermanMobileV2` |
| Korean | 韩文 | `KnownOCRModel.KoreanMobileV2` |
| Japanese | 日文 | `KnownOCRModel.JapaneseMobileV2` |
| Telugu | 泰卢固文 | `KnownOCRModel.TeluguMobileV2` |
| Kannada | 卡纳达文 | `KnownOCRModel.KannadaMobileV2` |
| Tamil | 泰米尔文 | `KnownOCRModel.TamilMobileV2` |
| Latin | 拉丁文 | `KnownOCRModel.LatinMobileV2` |
| Arabic | 阿拉伯字母 | `KnownOCRModel.ArabicMobileV2` |
| Cyrillic | 斯拉夫字母 | `KnownOCRModel.CyrillicMobileV2` |
| Devanagari | 梵文字母 | `KnownOCRModel.DevanagariMobileV2` |
只需将演示代码中的 `KnownOCRModel.PPOcrV2` 替换为上面`代码`栏中您的特定语言,然后您就可以使用该语言。
# 技术细节
进行 OCR 有 3 个步骤:
1.检测 - 检测文本的位置、角度和面积(`PaddleOCRDetector`)
2. 分类 - 确定文本是否应该旋转 180 度。
3. 识别 - 将区域识别为文本
# 优化参数和性能提示
## PaddleOcrAll.Enable180Classification
默认值:`false`
这直接影响第2步,设置为`false`可以跳过这一步,这将无法检测到从右到左的文本(这应该可以接受,因为大多数文本方向是从左到右)。
关闭此选项可以使整个过程更快约 `~10%`。
## PaddleOcrAll.AllowRotateDetection
默认值:`true`
这允许检测任何旋转的文本。 如果您的主题是 0 度文本(如扫描的表格或屏幕截图),您可以将此参数设置为 `false`,这将提高 OCR 准确性和一点点性能。
## PaddleOcrAll.Detector.MaxSize
默认值:`2048`
这会影响 step #1 的最大大小,降低该值可以提高性能并减少内存使用,但也会降低准确性。
您也可以将此值设置为`null`,在这种情况下,图像不会按比例缩小检测,性能会下降,内存会变高,但应该能够获得更好的准确性。
## PaddleConfig.Defaults.UseGpu
默认值:`false`
如果你想使用GPU,你应该参考FAQ`如何启用GPU?`部分,CUDA/cuDNN/TensorRT需要手动安装。
##如何提高性能?
请查看“技术细节”部分并阅读“优化参数和性能提示”部分或 UseGpu。
检测:
1. 准备好你的 PaddleDetection 模型,或者从[my exported models](https://pan.baidu.com/s/1pYuvUIU3HyEpu4gG0qSeMg?pwd=r90c)下载一个模型(从官方 PaddlePaddle [PicoDet](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.3/configs/picodet)和 [PPYolo](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.3/configs/ppyolo)导出)。
2.检查你的模型
注意:PaddleDetection 推理模型**必须**应采用以下格式:
```
model_dir
-> infer_cfg.yml
-> model.pdiparams
-> model.pdiparams.info
-> model.pdmodel
```
如果你的模型文件名是`xxx.pdparams`,必须导出到推理模型,可以参考【本文档】(https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.3/deploy/README_en.md #11-the-export-model) 导出推理模型。
3. Install NuGet Packages:
```
Sdcb.PaddleInference
Sdcb.PaddleInference.runtime.win64.mkl
Sdcb.PaddleDetection
OpenCvSharp4
OpenCvSharp4.runtime.win
4. Using following C# code to get result:
```csharp
PaddleConfig.Defaults.UseGpu = false;
string modelDir = DetectionLocalModel.PicoDets.L_416_coco.Directory; // your model directory here
using (PaddleDetector detector = new PaddleDetector(modelDir, Path.Combine(modelDir, "infer_cfg.yml")))
using (VideoCapture vc = new VideoCapture())
{
vc.Open(0);
while (true)
{
using (Mat mat = vc.RetrieveMat())
{
DetectionResult[] results = detector.Run(mat);
using (Mat dest = PaddleDetector.Visualize(mat,
results.Where(x => x.Confidence > 0.5f),
detector.Config.LabelList.Length))
{
Cv2.ImShow("test", dest);
}
}
Cv2.WaitKey(1);
}
}
```