一开始我在网上找demo没有找到,在群里寻求帮助也没有得到结果,索性将网上的易语言模块反编译之后,提取出对应的dll以及代码,然后对照官方的c++代码,写出了下面的c#版本

/***
* @pName caffe_task_pool_demo
* @name CC
* @user wadezh
* @date 2018/6/16
* @desc
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks; namespace caffe_task_pool_demo
{
class CC
{ public static int taskPool { get; set; } = ;
public static string prototxt { get; set; }
public static ArrayList map { get; set; }
public static int timeStep { get; set; }
public static int alphabetSize { get; set; } /*Caffe_API TaskPool* __stdcall createTaskPoolByData( const void* prototxt_data, int prototxt_data_length, const void* caffemodel_data, int caffemodel_data_length, float scale_raw = 1, const char* mean_file = 0, int num_means = 0, float* means = 0, int gpu_id = -1, int batch_size = 3);*/ [DllImport("classification_dll.dll", EntryPoint = "createTaskPoolByData", CallingConvention = CallingConvention.StdCall)]
public static extern int CreateTaskPoolByData(byte[] prototxt_data,
int prototxt_data_length,
byte[] caffemodel_data,
int caffemodel_data_length,
float scale_raw = ,
string mean_file = "",
int num_means = ,
float means = ,
int gpu_id = -,
int cach_size = ); /*Caffe_API BlobData* __stdcall forwardByTaskPool(TaskPool* pool, const void* img, int len, const char* blob_name);*/ [DllImport("classification_dll.dll", EntryPoint = "forwardByTaskPool", CallingConvention = CallingConvention.StdCall)]
public static extern int ForwardByTaskPool(int poolHandle, byte[] image, int imageLen, string printBlobName); /*Caffe_API int __stdcall getBlobLength(BlobData* feature);*/
[DllImport("classification_dll.dll", EntryPoint = "getBlobLength", CallingConvention = CallingConvention.StdCall)]
public static extern int GetBlobLength(int feature); /*Caffe_API void __stdcall cpyBlobData(void* buffer, BlobData* feature);*/
[DllImport("classification_dll.dll", EntryPoint = "cpyBlobData", CallingConvention = CallingConvention.StdCall)]
public static extern int CpyBlobData(float[] buffer, int feature); /*Caffe_API void __stdcall releaseBlobData(BlobData* ptr);*/
[DllImport("classification_dll.dll", EntryPoint = "releaseBlobData", CallingConvention = CallingConvention.StdCall)]
public static extern int ReleaseBlobData(int ptr); private static int Argmax(float[] arr, int begin, int end, ref float acc)
{
acc = -;
int mxInd = ;
for (int i = begin; i < end; i++)
{
if (acc < arr[i])
{
mxInd = i;
acc = arr[i];
}
}
return mxInd - begin;
} public static bool InitCaptcha(string prototxtPath, string modelPath, string mapPath, int gpuId, int batchSize) {
byte[] deploy = Util.GetFileStream(prototxtPath);
byte[] model = Util.GetFileStream(modelPath);
CC.taskPool = CC.CreateTaskPoolByData(deploy, deploy.Length, model, model.Length, 1F, "", , 0F, gpuId, batchSize);
CC.prototxt = System.Text.Encoding.Default.GetString(deploy);
string[] mapFile = Util.LoadStringFromFile(mapPath).Trim().Split("\r\n".ToArray());
CC.map = new ArrayList();
for (int i = ; i < mapFile.Length; i++)
{
if (mapFile[i].Length > )
{
CC.map.Add(mapFile[i]);
}
}
string time_step = Util.GetMiddleString(CC.prototxt, "time_step:", "\r\n");
string layer = Util.GetMiddleString(CC.prototxt, "inner_product_param {", "{");
string alphabet_size = Util.GetMiddleString(layer, "num_output:", "\r\n");
CC.timeStep = int.Parse(time_step);
CC.alphabetSize = int.Parse(alphabet_size);
return CC.taskPool != ;
} public static string GetCaptcha(byte[] image) {
// Get the prediction result handle
int poolHandle = CC.ForwardByTaskPool(taskPool, image, image.Length, "premuted_fc"); // Get the tensor handle
float[] permute_fc = new float[CC.GetBlobLength(poolHandle)]; // Copy the tensor data
CpyBlobData(permute_fc, poolHandle);
string code = string.Empty; if (permute_fc.Length > )
{
int o = ;
float acc = 0F;
int emptyLabel = alphabetSize - ;
int prev = emptyLabel;
for (int i = ; i < timeStep; i++)
{
o = Argmax(permute_fc, (i - ) * alphabetSize + , i * alphabetSize, ref acc);
if (o != emptyLabel && prev != o) code += map[o + ];
prev = o;
}
code = code.Replace("_", "").Trim();
} ReleaseBlobData(poolHandle);
return code;
} protected class Util
{ public static byte[] GetFileStream(string path)
{
FileStream fs = new FileStream(path, FileMode.Open);
long size = fs.Length;
byte[] array = new byte[size];
fs.Read(array, , array.Length);
fs.Close();
return array;
} public static string LoadStringFromFile(string fileName)
{
string content = string.Empty; StreamReader sr = null;
try
{
sr = new StreamReader(fileName, System.Text.Encoding.UTF8);
content = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
} if (sr != null)
sr.Close(); return content;
} public static string GetMiddleString(string SumString, string LeftString, string RightString)
{
if (string.IsNullOrEmpty(SumString)) return "";
if (string.IsNullOrEmpty(LeftString)) return "";
if (string.IsNullOrEmpty(RightString)) return ""; int LeftIndex = SumString.IndexOf(LeftString);
if (LeftIndex == -) return "";
LeftIndex = LeftIndex + LeftString.Length;
int RightIndex = SumString.IndexOf(RightString, LeftIndex);
if (RightIndex == -) return "";
return SumString.Substring(LeftIndex, RightIndex - LeftIndex);
} } } }

项目中我已经将caffemodel以及prototxt等文件都打包,可以直接运行

我封装的这个CC类只支持GPU任务池识别,识别速度比较快

链接:https://pan.baidu.com/s/17tSh3IE3Xv_YlJhSOhKddg 密码:ct5z

最新文章

  1. 出现了内部错误-网站中X509Certificate2加载证书时出错
  2. linux 安装rz sz命令
  3. 基于Windows10 x64+visual Studio2013+Python2.7.12环境下的Caffe配置学习
  4. Python演讲笔记1
  5. 【Extjs】large按钮,图片全部覆盖按钮
  6. hadoop学习过程中一系列问题
  7. poj3449Geometric Shapes
  8. 改进uboot,添加自定义快捷菜单
  9. Frameset布局
  10. UITextView换行问题解决办法
  11. ASP.NET MVC默认配置如有跳转到指定的Area区域中的对应程序中
  12. Spring Boot入门 and Spring Boot与ActiveMQ整合
  13. SQL问题+知识点总结总
  14. 【读书笔记】iOS-nonatomic
  15. mysql重置登录密码
  16. javascript替代Array.prototype.some操作
  17. TOJ3216 我要4444
  18. 虚拟机安装centos7, 再安装gitlab 简单步骤
  19. IIS7启用gzip
  20. SQL Server 查询分析器键盘快捷方式

热门文章

  1. canvas高级动画示例
  2. centos7.3部署django用uwsgi和nginx[亲测可用]
  3. 合并SCVMM虚拟机的差异磁盘,并删除那些难以删除的Checkpoints(Shapshots)
  4. AOP术语
  5. Flume学习总结
  6. javascript数组操作(创建、元素删除、数组的拷贝)
  7. 在Centos7下安装Python+Selenium+Firefox学习环境
  8. JS如何判断浏览器类型,如何模拟浏览器类型(模拟微信浏览器)
  9. Shiro01 功能点框图、架构图、身份认证逻辑、身份认证代码实现
  10. 9-eclispe中右键BuildPath没有了