ZHJA_HJGL/HJGL_ZH/FineUIPro.Web/RLSB/RLSBProccess.ashx.cs

448 lines
16 KiB
C#
Raw Normal View History

2024-05-08 17:17:11 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Text;
using BLL;
namespace RLSB
{
public class LabelImage
{
public string Label { get; set; }
public string Image { get; set; }
}
public class LabelImageResult
{
public List<LabelImage> list { get; set; }
public int err { get; set; }
}
public class LabelResult
{
public string[] list { get; set; }
public int err { get; set; }
public string desc { get; set; }
}
/// <summary>
/// RLSBProccess 的摘要说明
/// </summary>
public class RLSBProccess : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
try
{
if (context.Request.HttpMethod == "POST")
{
DoProccess(context);
}
else
{
DoGetProccess(context);
}
}
catch (ThreadAbortException ex)
{
//忽略此异常!
}
catch (Exception ex)
{
context.Response.Write(ex.ToString());
try { context.Response.End(); }
catch { }
}
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
}
Object m_GetObj = null;
HttpContext m_tmpContext = null;
void GetCallBackLabel(int err, string content)
{
if (m_GetObj != null)
{
LabelResult lr = m_GetObj as LabelResult;
if (lr != null)
{
lr.list = content.Split(';');
lr.err = 0;
lr.desc = "";
if (m_tmpContext != null)
m_tmpContext.Response.Write(JsonHelper.JsonSerializer(lr));
}
}
}
void GetCallBackLabelImage(int err, string content)
{
if (m_GetObj != null)
{
LabelImageResult lr = m_GetObj as LabelImageResult;
if (lr != null)
{
string[] aliAry = content.Split(';');
lr.list = new List<LabelImage>();
foreach (var s in aliAry)
{
LabelImage li = new LabelImage();
string[] tmp = s.Split(',');
if (tmp.Count() == 2)
{
li.Image = tmp[1];
li.Label = tmp[0];
lr.list.Add(li);
}
}
if (m_tmpContext != null)
m_tmpContext.Response.Write(JsonHelper.JsonSerializer(lr));
}
}
}
/// <summary>
/// 新加方法
/// </summary>
/// <param name="context"></param>
void GetCallBackLabelImageByLabel(int err, string content)
{
string[] aliAry = content.Split(';'); // format: label,image;label,image
foreach (var s in aliAry)
{
LabelImage li = new LabelImage();
string[] tmp = s.Split(',');
if (tmp.Count() == 2)
{
if (tmp[0].Equals(searchLabel))
{
searchImage = tmp[1];
break;
}
}
}
}
/// <summary>
/// 新加方法
/// </summary>
/// <param name="context"></param>
void GetAllCallBackLabelImageByLabel(int err, string content)
{
string[] aliAry = content.Split(';'); // format: label,image;label,image
foreach (var s in aliAry)
{
LabelImage li = new LabelImage();
string[] tmp = s.Split(',');
if (tmp.Count() == 2)
{
if (tmp[0].Equals(searchLabel))
{
allSearchImage += tmp[1] + ",";
}
}
}
if (!string.IsNullOrEmpty(allSearchImage))
{
allSearchImage = allSearchImage.Substring(0,allSearchImage.LastIndexOf(","));
}
}
string searchLabel;
string searchImage;
string allSearchImage;
/// <summary>
/// 新加方法根据标签获取图片封装
/// </summary>
/// <param name="context"></param>
public string GetImageByLabel(string label)
{
searchLabel = label;
searchImage = "未找到匹配的图片";
libFace.GetAllLabelAndImage(GetCallBackLabelImageByLabel);
//searchImage;//此时searchImage就是对应标签的图片文件名
return searchImage;
}
/// <summary>
/// 新加方法根据标签获取所有图片封装
/// </summary>
/// <param name="context"></param>
public string GetAllImageByLabel(string label)
{
searchLabel = label;
libFace.GetAllLabelAndImage(GetAllCallBackLabelImageByLabel);
if (string.IsNullOrEmpty(allSearchImage))
{
allSearchImage = "未找到匹配的图片";
}
//allSearchImage;//此时allSearchImage就是对应标签的所有图片文件名
return allSearchImage;
}
void DoGetProccess(HttpContext context)
{
string action = context.Request.Params["action"];
switch (action)
{
case "alllabel":
LabelResult lr = new LabelResult();
m_GetObj = lr;
m_tmpContext = context;
libFace.GetAllLabel(GetCallBackLabel);//.Split(';');
//lr.list
break;
case "alllabelimage":
m_GetObj = new LabelImageResult();
m_tmpContext = context;
libFace.GetAllLabelAndImage(GetCallBackLabelImage);
break;
case "getimage":
string filename = context.Request.Params["name"].Trim();
if (filename == "")
{
context.Response.End();
break;
}
string filepath = context.Server.MapPath("~/SeetaFaceDir/modelimages/" + filename);
context.Response.ContentType = "image/jpg";
try
{
context.Response.WriteFile(filepath);
}
catch { }
break;
default:
//LabelResult
lr = new LabelResult();
lr.err = 3;
lr.desc = "无效的action请求!";
context.Response.Write(JsonHelper.JsonSerializer(lr));
break;
}
context.Response.End();
}
void GetCallBackLabelQuickShiBie(int err, string content)
{
if (null != m_GetObj)
{
StringBuilder sb = m_GetObj as StringBuilder;
if (sb != null)
{
sb.Append(content);
}
}
}
/// <summary>
/// 文件内容是十六进制的字符串,需要读出来传递给库
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
string QuickShiBie(string filename)
{
try
{
FileStream stream = File.OpenRead(filename);
StreamReader sr = new StreamReader(stream);
string strFeature = sr.ReadToEnd();
StringBuilder sb = new StringBuilder();
m_GetObj = sb;
libFace.GetNameByFeature(strFeature, 3, GetCallBackLabelQuickShiBie);
sr.Close();
return sb.ToString();
}
catch (Exception)
{
return "ERR:识别异常!";
}
}
string TrainFile(HttpServerUtility server, string filename, string label)
{
try
{
//bool b = libFace.AddFileLabel(filename, label);
//由于训练图片需要时间,这里加个线程处理,即时返回客户端,但并不表示马上训练成功,防止上传图时被柱塞
new Thread(() =>
{
try
{
bool b = libFace.TrainFileLabel(filename, label);
System.IO.File.Delete(filename);
//修改焊工是否已训练
Model.HJGL_BS_Welder welder = BLL.HJGL_PersonManageService.GetWelderByIdentityCard(label);
if (welder != null)
{
welder.IsFaceTrain = true;
BLL.HJGL_PersonManageService.UpdateBSWelderIsFaceTrain(welder);
}
//修改保管员是否已训练
Model.Weld_Storeman storeman = BLL.StoremanInfoService.GetStoremanByIdentityCard(label);
if (storeman != null)
{
storeman.IsFaceTrain = true;
BLL.StoremanInfoService.UpdateStoreManIsFaceTrain(storeman);
}
Logger.WriteLog(server, "TrainFile " + filename + " " + (b ? "成功" : "失败"));
}
catch (Exception ex)
{
string log = string.Format("TrainFile {0}Exception:" + ex.ToString(), filename);
Logger.WriteLog(server, log);
System.IO.File.Delete(filename);
}
}).Start();
return "上传图已加入训练队列!服务器正处理中,稍后将生效!";
}
catch (Exception)
{
return "训练异常!";
}
}
void GetCallBackLabelShibie(int err, string content)
{
if (null != m_GetObj)
{
StringBuilder sb = m_GetObj as StringBuilder;
if (sb != null)
{
sb.Append(content);
}
}
}
/// <summary>
/// 识别人脸filename上传的图片所存的位置
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
string Shibie(string filename)
{
try
{
var sb = new StringBuilder();
m_GetObj = sb;
libFace.GetNameByFile(filename, 3, GetCallBackLabelShibie);
return sb.ToString();
}
catch (Exception)
{
return "ERR:识别异常!";
}
}
void DoProccess(HttpContext context)
{
string action = context.Request.Form["action"];
string filename;
switch (action)
{
//训练人脸库,上传的是图片
case "train":
string label = context.Request.Form["label"].Trim();
if (string.IsNullOrEmpty(label))
{
context.Response.Write("失败,没有设置标签!");
break;
}
if (context.Request.Files.Count != 1)
{
context.Response.Write("失败,没有上传一个文件(一次支持一张)!");
break;
}
filename = context.Server.MapPath("~/tmpupload/" + (DateTime.Now).ToString("yyyyMMddHHmmss") + ".jpg");
context.Request.Files[0].SaveAs(filename);
context.Response.Write(TrainFile(context.Server, filename, label));
//由于控件内部处理也是使用此接口,所以过滤掉控件的请求,控件不执行以下代码
if (-1 == context.Request.UserAgent.IndexOf("ActiveXSeetaFace"))
{
context.Response.Write("<script type='text/javascript'>parent.OnUploadOver();</script>");
}
//由于使用线程处理,下面不能删文件,否则将出错!
//////System.IO.File.Delete(filename);
break;
//识别人脸
case "shibie":
if (context.Request.Files.Count != 1)
{
context.Response.Write("失败,没有上传一个文件(一次支持一张)!");
break;
}
{
filename = context.Server.MapPath("~/tmpupload/" + (DateTime.Now).ToString("yyyyMMddHHmmss") + ".jpg");
context.Request.Files[0].SaveAs(filename);
context.Response.Write(Shibie(filename));
System.IO.File.Delete(filename);
}
break;
//快速识别,上传的是特证码(十六进制字符串)
case "quickshibie":
if (context.Request.Files.Count != 1)
{
context.Response.Write("失败,没有上传一个文件(一次支持一张)!");
break;
}
{
filename = context.Server.MapPath("~/tmpupload/" + (DateTime.Now).ToString("yyyyMMddHHmmss") + ".jpg");
context.Request.Files[0].SaveAs(filename);
context.Response.Write(QuickShiBie(filename));
System.IO.File.Delete(filename);
}
break;
//以标签方式删除库
//返回JSON格式
case "delliblabel":
context.Response.ContentType = "application/json";
label = context.Request.Form["label"].Trim();
if (string.IsNullOrEmpty(label))
{
context.Response.Write("{\"err\":1,\"desc\":\"失败,没有设置标签!\"}");
break;
}
bool bOK = libFace.DelLibByLabel(label);
if (bOK)
{
context.Response.Write("{\"err\":0,\"desc\":\"删除成功!\"}");
}
else
{
context.Response.Write("{\"err\":1,\"desc\":\"删除失败或者此标签已不存在!\"}");
}
break;
//以图片名方式删除库
case "dellibfile":
context.Response.ContentType = "application/json";
filename = context.Request.Form["filename"];
if (string.IsNullOrEmpty(filename))
{
context.Response.Write("{\"err\":1,\"desc\":\"失败,没有设置文件名!\"}");
break;
}
bOK = libFace.DelLibByFileName(filename);
if (bOK)
{
context.Response.Write("{\"err\":0,\"desc\":\"删除成功!\"}");
}
else
{
context.Response.Write("{\"err\":2,\"desc\":\"删除失败或者此文件已不存在!\"}");
}
break;
default:
context.Response.Write("失败,请求有误!未知的action");
break;
}
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}