public static string GetCaptcha(byte[] image) { // Get the prediction result handle int poolHandle = Caffe.ForwardByTaskPool(taskPool, image, image.Length, "premuted_fc"); // Get the tensor handle float[] permute_fc = new float[Caffe.GetBlobLength(poolHandle)]; // Copy the tensor data CpyBlobData(permute_fc, poolHandle); string code = string.Empty; if (permute_fc.Length > 0) { int o = 0; float acc = 0F; int emptyLabel = alphabetSize - 1; int prev = emptyLabel; for (int i = 1; i < timeStep; i++) { o = Argmax(permute_fc, (i - 1) * alphabetSize + 1, i * alphabetSize, ref acc); if (o != emptyLabel && prev != o) { code += map[o + 1]; } prev = o; } code = code.Replace("_", "").Trim(); } ReleaseBlobData(poolHandle); return(code); }
public static bool InitCaptcha(string prototxtPath, string modelPath, string mapPath, int gpuId, int batchSize) { byte[] deploy = Util.GetFileStream(prototxtPath); byte[] model = Util.GetFileStream(modelPath); Caffe.taskPool = Caffe.CreateTaskPoolByData(deploy, deploy.Length, model, model.Length, 1F, "", 0, 0F, gpuId, batchSize); Caffe.prototxt = System.Text.Encoding.Default.GetString(deploy); string[] mapFile = Util.LoadStringFromFile(mapPath).Trim().Split("\r\n".ToArray()); Caffe.map = new ArrayList(); for (int i = 0; i < mapFile.Length; i++) { if (mapFile[i].Length > 0) { Caffe.map.Add(mapFile[i]); } } string time_step = Util.GetMiddleString(Caffe.prototxt, "time_step:", "b").Trim(); string layer = Util.GetMiddleString(Caffe.prototxt, "inner_product_param {", "{"); string alphabet_size = Util.GetMiddleString(layer, "num_output:", "\r\n").Trim(); Caffe.timeStep = int.Parse(time_step); Caffe.alphabetSize = int.Parse(alphabet_size); return(Caffe.taskPool != 0); }