├── SmartFactory ├── tengen.tbk ├── focus.png ├── msc_x64.dll ├── dll │ ├── iFlyDotNet.dll │ ├── Python.Runtime.dll │ ├── PyFaceRecognition.dll │ └── DirectShowLib-2005.dll ├── msc │ ├── asr │ │ └── common.jet │ └── tts │ │ ├── common.jet │ │ ├── xiaofeng.jet │ │ └── xiaoyan.jet ├── tengen.bnf ├── App.config ├── Properties │ ├── Settings.settings │ ├── Settings.Designer.cs │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ └── Resources.resx ├── App.xaml.cs ├── packages.config ├── SimpleCommand.cs ├── Talkback.cs ├── App.xaml ├── NAudioHelper.cs ├── GridLengthAnimation.cs ├── SmartFactory.csproj ├── MainWindow.xaml.cs └── MainWindow.xaml ├── PyFaceRecognition ├── Python.Runtime.dll ├── Properties │ └── AssemblyInfo.cs ├── FaceEncodings.cs ├── PyFaceRecognition.csproj └── FaceRecoginition.cs ├── iFlyDotNet ├── IFlyCommon.cs ├── Properties │ └── AssemblyInfo.cs ├── iFlyDotNet.csproj ├── IFlyTTS.cs └── IFlyAsr.cs ├── README.md ├── SmartFactory.sln ├── .gitattributes └── .gitignore /SmartFactory/tengen.tbk: -------------------------------------------------------------------------------- 1 | 开始加工|您好,加工已开始 2 | 停止加工|您好,加工已停止 -------------------------------------------------------------------------------- /SmartFactory/focus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ilovehotmilk/SmartFactory/HEAD/SmartFactory/focus.png -------------------------------------------------------------------------------- /SmartFactory/msc_x64.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ilovehotmilk/SmartFactory/HEAD/SmartFactory/msc_x64.dll -------------------------------------------------------------------------------- /SmartFactory/dll/iFlyDotNet.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ilovehotmilk/SmartFactory/HEAD/SmartFactory/dll/iFlyDotNet.dll -------------------------------------------------------------------------------- /SmartFactory/msc/asr/common.jet: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ilovehotmilk/SmartFactory/HEAD/SmartFactory/msc/asr/common.jet -------------------------------------------------------------------------------- /SmartFactory/msc/tts/common.jet: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ilovehotmilk/SmartFactory/HEAD/SmartFactory/msc/tts/common.jet -------------------------------------------------------------------------------- /SmartFactory/msc/tts/xiaofeng.jet: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ilovehotmilk/SmartFactory/HEAD/SmartFactory/msc/tts/xiaofeng.jet -------------------------------------------------------------------------------- /SmartFactory/msc/tts/xiaoyan.jet: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ilovehotmilk/SmartFactory/HEAD/SmartFactory/msc/tts/xiaoyan.jet -------------------------------------------------------------------------------- /PyFaceRecognition/Python.Runtime.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ilovehotmilk/SmartFactory/HEAD/PyFaceRecognition/Python.Runtime.dll -------------------------------------------------------------------------------- /SmartFactory/dll/Python.Runtime.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ilovehotmilk/SmartFactory/HEAD/SmartFactory/dll/Python.Runtime.dll -------------------------------------------------------------------------------- /SmartFactory/dll/PyFaceRecognition.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ilovehotmilk/SmartFactory/HEAD/SmartFactory/dll/PyFaceRecognition.dll -------------------------------------------------------------------------------- /SmartFactory/dll/DirectShowLib-2005.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ilovehotmilk/SmartFactory/HEAD/SmartFactory/dll/DirectShowLib-2005.dll -------------------------------------------------------------------------------- /SmartFactory/tengen.bnf: -------------------------------------------------------------------------------- 1 | #BNF+IAT 1.0; 2 | !grammar tengen; 3 | !slot ; 4 | 5 | !start ; 6 | 7 | :; 8 | 9 | :开始加工|停止加工; -------------------------------------------------------------------------------- /SmartFactory/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /SmartFactory/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /SmartFactory/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.Data; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | using System.Windows; 8 | 9 | namespace SmartFactory 10 | { 11 | /// 12 | /// App.xaml 的交互逻辑 13 | /// 14 | public partial class App : Application 15 | { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /SmartFactory/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SmartFactory/SimpleCommand.cs: -------------------------------------------------------------------------------- 1 | using System.Windows.Input; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace SmartFactory 9 | { 10 | public class SimpleCommand : ICommand 11 | { 12 | public event EventHandler CanExecuteChanged; 13 | 14 | private Action Do; 15 | 16 | public SimpleCommand(Action action) 17 | { 18 | Do = action; 19 | } 20 | 21 | public bool CanExecute(object parameter) 22 | { 23 | return true; 24 | } 25 | 26 | public void Execute(object parameter) 27 | { 28 | Do?.Invoke(parameter); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /iFlyDotNet/IFlyCommon.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.InteropServices; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace iFlyDotNet 9 | { 10 | public class IFlyCommon 11 | { 12 | public const string DllName = "msc_x64.dll"; 13 | 14 | [DllImport(DllName)] 15 | private static extern int MSPLogin(string user, string pwd, string param); 16 | 17 | [DllImport(DllName)] 18 | private static extern int MSPLogout(); 19 | 20 | public static void Login(string logConfig) 21 | { 22 | if (MSPLogin(null, null, logConfig) != 0) 23 | throw new Exception(logConfig+" 登陆失败"); 24 | } 25 | 26 | public static void Logout() 27 | { 28 | if (MSPLogout() != 0) 29 | throw new Exception("注销失败"); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /SmartFactory/Talkback.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace SmartFactory 9 | { 10 | public class Talkback 11 | { 12 | public static string GetResult(string asrStr) 13 | { 14 | try 15 | { 16 | using (StreamReader sr = new StreamReader("tengen.tbk")) 17 | { 18 | string line=null; 19 | while((line = sr.ReadLine()) != null) 20 | { 21 | var ret = line.Split('|'); 22 | if (ret.Length != 2) 23 | continue; 24 | if (ret[0] == asrStr) 25 | return ret[1]; 26 | } 27 | return null; 28 | } 29 | } 30 | catch 31 | { 32 | return null; 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /iFlyDotNet/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("iFlyAsrDotNet")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("iFlyAsrDotNet")] 13 | [assembly: AssemblyCopyright("Copyright © 2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 会使此程序集中的类型 18 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("a3d0f8aa-647b-4f74-9300-4717bac083c1")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 33 | //通过使用 "*",如下所示: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /PyFaceRecognition/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("PyFaceRecognition")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("PyFaceRecognition")] 13 | [assembly: AssemblyCopyright("Copyright © 2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 会使此程序集中的类型 18 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("6aed0ed3-a394-4527-8a2d-b4f206c315ce")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 33 | //通过使用 "*",如下所示: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /SmartFactory/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace SmartFactory.Properties 12 | { 13 | 14 | 15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] 17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 18 | { 19 | 20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 21 | 22 | public static Settings Default 23 | { 24 | get 25 | { 26 | return defaultInstance; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SmartFactory项目简介 2 | * 融合了离线人脸识别、实时离线语音识别和离线语音合成的WPF项目 3 | * 人脸识别方案基于[Face Recognition--Python](https://github.com/ageitgey/face_recognition) 4 | * 语音识别和语音合成方案是基于科大讯飞的[离线命令词识别SDK](http://www.xfyun.cn/services/commandWord)和[离线语音合成SDK](http://www.xfyun.cn/services/offline_tts)(收费,试用版35天使用期限) 5 | * 只支持Windows 64位操作系统 6 | 7 | ## 开发环境 8 | * Windows 7 9 | * Visual Studio 2017 10 | * .Net 4.6.1 11 | ## 运行环境配置 12 | ### 硬件 13 | * 摄像头、麦克风、音响 14 | ### 人脸识别 15 | * 下载[Python3.5运行环境压缩包](https://pan.baidu.com/s/1MSSY8c8R1ipaKfDJJpsXpg),解压至与可执行文件同一目录下 16 | * 可执行文件路径不可以包含中文!!! 17 | ### 语音识别和语音合成 18 | * 在科大讯飞官网注册并获取[离线命令词识别SDK](http://www.xfyun.cn/services/commandWord)和[离线语音合成SDK](http://www.xfyun.cn/services/offline_tts) 19 | * 将SmartFactory项目下的msc_x64.dll替换为离线命令词识别SDK中bin文件夹中的msc_x64.dll 20 | * 将SmartFactory项目下msc/asr文件夹中的**所有.jet文件**删除并替换为自己下载的离线命令词识别SDK中bin/msc/res/asr文件夹中的**所有.jet文件** 21 | * 将SmartFactory项目下msc/tts文件夹中的**所有.jet文件**删除并替换为自己下载的离线语音合成SDK中bin/msc/res/tts文件夹中的**所有.jet文件** 22 | * 用讯飞开放平台控制台中的APPID给SmartFactory项目下MainWindows.xaml.cs文件中的"loginParam"属性赋值(例如:loginParam = "APPID = 123456";) 23 | * 将SmartFactory项目下的tengen.bnf中的command替换为自己需要识别的命令词,不同命令词之间用"|"分隔 24 | * SmartFactory项目下的tengen.tbk中的每一行为一个完整的语音识别和合成的反馈组合,"|"的左边为语音识别的命令词,右边为语音合成需要反馈的语句 25 | -------------------------------------------------------------------------------- /SmartFactory/App.xaml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /PyFaceRecognition/FaceEncodings.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Xml.Serialization; 7 | 8 | namespace PyFaceRecognition 9 | { 10 | [Serializable] 11 | public class FaceEncodings 12 | { 13 | [XmlArray] 14 | public List Items { get; } = new List(); 15 | 16 | public float[] this[string name] 17 | { 18 | get 19 | { 20 | if (Items.Exists((face) => face.Name == name)) 21 | return Items.Find((face) => face.Name == name).Encoding; 22 | else 23 | return null; 24 | } 25 | set 26 | { 27 | if (Items.Exists((face) => face.Name == name)) 28 | Items.Find((face) => face.Name == name).Encoding = value; 29 | } 30 | } 31 | 32 | public int Count(string name) 33 | { 34 | return Items.FindAll((face) => face.Name == name).Count; 35 | } 36 | } 37 | 38 | [Serializable] 39 | public class FaceEncoding 40 | { 41 | public FaceEncoding() { } 42 | public FaceEncoding(string name,float[] encoding) 43 | { 44 | Name = name; 45 | Encoding = encoding; 46 | } 47 | 48 | [XmlElement] 49 | public string Name { get; set; } 50 | [XmlArray] 51 | public float[] Encoding { get; set; } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /SmartFactory/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Resources; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | using System.Windows; 6 | 7 | // 有关程序集的一般信息由以下 8 | // 控制。更改这些特性值可修改 9 | // 与程序集关联的信息。 10 | [assembly: AssemblyTitle("SmartFactory")] 11 | [assembly: AssemblyDescription("")] 12 | [assembly: AssemblyConfiguration("")] 13 | [assembly: AssemblyCompany("")] 14 | [assembly: AssemblyProduct("SmartFactory")] 15 | [assembly: AssemblyCopyright("Copyright © 2018")] 16 | [assembly: AssemblyTrademark("")] 17 | [assembly: AssemblyCulture("")] 18 | 19 | // 将 ComVisible 设置为 false 会使此程序集中的类型 20 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 21 | //请将此类型的 ComVisible 特性设置为 true。 22 | [assembly: ComVisible(false)] 23 | 24 | //若要开始生成可本地化的应用程序,请设置 25 | //.csproj 文件中的 CultureYouAreCodingWith 26 | //例如,如果您在源文件中使用的是美国英语, 27 | //使用的是美国英语,请将 设置为 en-US。 然后取消 28 | //对以下 NeutralResourceLanguage 特性的注释。 更新 29 | //以下行中的“en-US”以匹配项目文件中的 UICulture 设置。 30 | 31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] 32 | 33 | 34 | [assembly: ThemeInfo( 35 | ResourceDictionaryLocation.None, //主题特定资源词典所处位置 36 | //(未在页面中找到资源时使用, 37 | //或应用程序资源字典中找到时使用) 38 | ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置 39 | //(未在页面中找到资源时使用, 40 | //、应用程序或任何主题专用资源字典中找到时使用) 41 | )] 42 | 43 | 44 | // 程序集的版本信息由下列四个值组成: 45 | // 46 | // 主版本 47 | // 次版本 48 | // 生成号 49 | // 修订号 50 | // 51 | // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 52 | // 方法是按如下所示使用“*”: : 53 | // [assembly: AssemblyVersion("1.0.*")] 54 | [assembly: AssemblyVersion("1.0.0.0")] 55 | [assembly: AssemblyFileVersion("1.0.0.0")] 56 | -------------------------------------------------------------------------------- /iFlyDotNet/iFlyDotNet.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {A3D0F8AA-647B-4F74-9300-4717BAC083C1} 8 | Library 9 | Properties 10 | iFlyDotNet 11 | iFlyDotNet 12 | v4.6.1 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | TRACE;DEBUG 21 | prompt 22 | 4 23 | x64 24 | 25 | 26 | pdbonly 27 | true 28 | bin\Release\ 29 | TRACE 30 | prompt 31 | 4 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /SmartFactory.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27130.2024 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartFactory", "SmartFactory\SmartFactory.csproj", "{3E2F4478-F067-4577-B5E2-52E5871D1C59}" 7 | ProjectSection(ProjectDependencies) = postProject 8 | {A3D0F8AA-647B-4F74-9300-4717BAC083C1} = {A3D0F8AA-647B-4F74-9300-4717BAC083C1} 9 | {6AED0ED3-A394-4527-8A2D-B4F206C315CE} = {6AED0ED3-A394-4527-8A2D-B4F206C315CE} 10 | EndProjectSection 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PyFaceRecognition", "PyFaceRecognition\PyFaceRecognition.csproj", "{6AED0ED3-A394-4527-8A2D-B4F206C315CE}" 13 | EndProject 14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "iFlyDotNet", "iFlyDotNet\iFlyDotNet.csproj", "{A3D0F8AA-647B-4F74-9300-4717BAC083C1}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Any CPU = Debug|Any CPU 19 | Release|Any CPU = Release|Any CPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {3E2F4478-F067-4577-B5E2-52E5871D1C59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {3E2F4478-F067-4577-B5E2-52E5871D1C59}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {3E2F4478-F067-4577-B5E2-52E5871D1C59}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {3E2F4478-F067-4577-B5E2-52E5871D1C59}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {6AED0ED3-A394-4527-8A2D-B4F206C315CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {6AED0ED3-A394-4527-8A2D-B4F206C315CE}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {6AED0ED3-A394-4527-8A2D-B4F206C315CE}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {6AED0ED3-A394-4527-8A2D-B4F206C315CE}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {A3D0F8AA-647B-4F74-9300-4717BAC083C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {A3D0F8AA-647B-4F74-9300-4717BAC083C1}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {A3D0F8AA-647B-4F74-9300-4717BAC083C1}.Release|Any CPU.ActiveCfg = Release|Any CPU 33 | {A3D0F8AA-647B-4F74-9300-4717BAC083C1}.Release|Any CPU.Build.0 = Release|Any CPU 34 | EndGlobalSection 35 | GlobalSection(SolutionProperties) = preSolution 36 | HideSolutionNode = FALSE 37 | EndGlobalSection 38 | GlobalSection(ExtensibilityGlobals) = postSolution 39 | SolutionGuid = {3E77C4E7-4759-4A73-A972-2093CA0B2C46} 40 | EndGlobalSection 41 | EndGlobal 42 | -------------------------------------------------------------------------------- /SmartFactory/NAudioHelper.cs: -------------------------------------------------------------------------------- 1 | using NAudio.Mixer; 2 | using NAudio.Wave.SampleProviders; 3 | using System.Linq; 4 | using NAudio.CoreAudioApi; 5 | using NAudio.Gui; 6 | using NAudio.Wave; 7 | using System; 8 | using System.Collections.Generic; 9 | using System.IO; 10 | using System.Threading; 11 | 12 | namespace SmartFactory 13 | { 14 | class NAudioHelper 15 | { 16 | private WaveIn waveSource = null; 17 | 18 | private List pcmData = null; 19 | 20 | public Action PcmDataAvailable; 21 | /// 22 | /// 开始录音 23 | /// 24 | public void StartRec() 25 | { 26 | waveSource = new WaveIn(); 27 | pcmData = new List(); 28 | waveSource.WaveFormat = new WaveFormat(16000, 16, 1); // 16bit,16KHz,Mono的录音格式 29 | waveSource.DataAvailable += new EventHandler(waveSource_DataAvailable); 30 | waveSource.RecordingStopped += new EventHandler(waveSource_RecordingStopped); 31 | waveSource.StartRecording(); 32 | } 33 | 34 | /// 35 | /// 停止录音 36 | /// 37 | public void StopRec() 38 | { 39 | waveSource?.StopRecording(); 40 | // Close Wave(Not needed under synchronous situation) 41 | if (waveSource != null) 42 | { 43 | waveSource.Dispose(); 44 | waveSource = null; 45 | } 46 | } 47 | 48 | /// 49 | /// 开始录音回调函数 50 | /// 51 | /// 52 | /// 53 | private void waveSource_DataAvailable(object sender, WaveInEventArgs e) 54 | { 55 | PcmDataAvailable?.Invoke(e.Buffer); 56 | } 57 | 58 | /// 59 | /// 录音结束回调函数 60 | /// 61 | /// 62 | /// 63 | private void waveSource_RecordingStopped(object sender, StoppedEventArgs e) 64 | { 65 | if (waveSource != null) 66 | { 67 | waveSource.Dispose(); 68 | waveSource = null; 69 | } 70 | } 71 | 72 | /// 73 | /// 播放PCM音频 74 | /// 75 | /// 76 | public void Play(byte[] pcmData) 77 | { 78 | var waveOut = new WaveOut(); 79 | waveOut.Init(new RawSourceWaveStream(pcmData, 0, pcmData.Length, new WaveFormat(16000, 16, 1))); 80 | waveOut.Play(); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /PyFaceRecognition/PyFaceRecognition.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {6AED0ED3-A394-4527-8A2D-B4F206C315CE} 8 | Library 9 | Properties 10 | PyFaceRecognition 11 | PyFaceRecognition 12 | v4.6.1 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | x64 24 | 25 | 26 | pdbonly 27 | true 28 | bin\Release\ 29 | TRACE 30 | prompt 31 | 4 32 | 33 | 34 | 35 | C:\Users\zb\AppData\Local\Programs\Python\Python35\Lib\site-packages\Python.Runtime.dll 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /SmartFactory/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 此代码由工具生成。 4 | // 运行时版本: 4.0.30319.42000 5 | // 6 | // 对此文件的更改可能导致不正确的行为,如果 7 | // 重新生成代码,则所做更改将丢失。 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace SmartFactory.Properties 12 | { 13 | 14 | 15 | /// 16 | /// 强类型资源类,用于查找本地化字符串等。 17 | /// 18 | // 此类是由 StronglyTypedResourceBuilder 19 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 20 | // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen 21 | // (以 /str 作为命令选项),或重新生成 VS 项目。 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources 26 | { 27 | 28 | private static global::System.Resources.ResourceManager resourceMan; 29 | 30 | private static global::System.Globalization.CultureInfo resourceCulture; 31 | 32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 33 | internal Resources() 34 | { 35 | } 36 | 37 | /// 38 | /// 返回此类使用的缓存 ResourceManager 实例。 39 | /// 40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 41 | internal static global::System.Resources.ResourceManager ResourceManager 42 | { 43 | get 44 | { 45 | if ((resourceMan == null)) 46 | { 47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SmartFactory.Properties.Resources", typeof(Resources).Assembly); 48 | resourceMan = temp; 49 | } 50 | return resourceMan; 51 | } 52 | } 53 | 54 | /// 55 | /// 覆盖当前线程的 CurrentUICulture 属性 56 | /// 使用此强类型的资源类的资源查找。 57 | /// 58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 59 | internal static global::System.Globalization.CultureInfo Culture 60 | { 61 | get 62 | { 63 | return resourceCulture; 64 | } 65 | set 66 | { 67 | resourceCulture = value; 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /iFlyDotNet/IFlyTTS.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.InteropServices; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace iFlyDotNet 9 | { 10 | public class IFlyTTS 11 | { 12 | [DllImport(IFlyCommon.DllName)] 13 | private static extern IntPtr QTTSSessionBegin(string param, ref int errorCode); 14 | 15 | [DllImport(IFlyCommon.DllName)] 16 | private static extern int QTTSTextPut(string sessionID, string textString, uint textLen, string param); 17 | 18 | [DllImport(IFlyCommon.DllName)] 19 | private static extern IntPtr QTTSAudioGet(string sessionID, ref uint audioLen, ref int synthStatus, ref int errorCode); 20 | 21 | [DllImport(IFlyCommon.DllName)] 22 | private static extern int QTTSSessionEnd(string sessionID, string hints); 23 | 24 | [DllImport(IFlyCommon.DllName)] 25 | private static extern int QTTSGetParam(string sessionID, string paramName, string paramValue, ref uint valueLen); 26 | 27 | private IFlyTTS() 28 | { 29 | 30 | } 31 | 32 | private string sessionId; 33 | 34 | private int errorCode = -1; 35 | 36 | private static IFlyTTS _iFlyTTS; 37 | 38 | public static IFlyTTS GetInstance() 39 | { 40 | return _iFlyTTS ?? (_iFlyTTS = new IFlyTTS()); 41 | } 42 | 43 | public byte[] GetPcmDataByTTS(string text) 44 | { 45 | string param = "engine_type = local, voice_name = xiaoyan, text_encoding = GB2312, tts_res_path = fo|tts\\xiaoyan.jet;fo|tts\\common.jet, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 2"; 46 | sessionId = Marshal.PtrToStringAnsi(QTTSSessionBegin(param, ref errorCode)); 47 | if (sessionId == null) 48 | throw new Exception("QISRSessionBegin errorCode: " + errorCode); 49 | errorCode = QTTSTextPut(sessionId, text, (uint)Encoding.Default.GetByteCount(text), null); 50 | if (errorCode!=0) 51 | throw new Exception("QTTSTextPut errorCode: " + errorCode); 52 | List listData = new List(); 53 | while (true) 54 | { 55 | uint audioLen = 0; 56 | int synthStatus=0; 57 | var dataPtr = QTTSAudioGet(sessionId, ref audioLen, ref synthStatus, ref errorCode); 58 | if (errorCode!=0) 59 | break; 60 | if (dataPtr!=IntPtr.Zero) 61 | { 62 | byte[] tmpData=new byte[audioLen]; 63 | Marshal.Copy(dataPtr, tmpData, 0, (int)audioLen); 64 | listData.AddRange(tmpData); 65 | } 66 | if (synthStatus==2) 67 | break; 68 | } 69 | if (errorCode != 0) 70 | { 71 | throw new Exception("QTTSAudioGet errorCode: " + errorCode); 72 | } 73 | errorCode = QTTSSessionEnd(sessionId, "Normal"); 74 | if (errorCode!=0) 75 | { 76 | throw new Exception("QTTSSessionEnd errorCode: " + errorCode); 77 | } 78 | return listData.ToArray(); 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /SmartFactory/GridLengthAnimation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows; 7 | using System.Windows.Media.Animation; 8 | 9 | namespace SmartFactory 10 | { 11 | public class GridLengthAnimation : AnimationTimeline 12 | { 13 | static GridLengthAnimation() 14 | { 15 | FromProperty = DependencyProperty.Register("From", typeof(GridLength), 16 | typeof(GridLengthAnimation)); 17 | 18 | ToProperty = DependencyProperty.Register("To", typeof(GridLength), 19 | typeof(GridLengthAnimation)); 20 | } 21 | 22 | public static readonly DependencyProperty FromProperty; 23 | public GridLength From 24 | { 25 | get 26 | { 27 | return (GridLength)GetValue(GridLengthAnimation.FromProperty); 28 | } 29 | set 30 | { 31 | SetValue(GridLengthAnimation.FromProperty, value); 32 | } 33 | } 34 | 35 | public static readonly DependencyProperty ToProperty; 36 | public GridLength To 37 | { 38 | get 39 | { 40 | return (GridLength)GetValue(GridLengthAnimation.ToProperty); 41 | } 42 | set 43 | { 44 | SetValue(GridLengthAnimation.ToProperty, value); 45 | } 46 | } 47 | 48 | protected override System.Windows.Freezable CreateInstanceCore() 49 | { 50 | return new GridLengthAnimation(); 51 | } 52 | 53 | 54 | public override Type TargetPropertyType 55 | { 56 | get 57 | { 58 | return typeof(GridLength); 59 | } 60 | } 61 | 62 | public const string EasingFunctionPropertyName = "EasingFunction"; 63 | 64 | public IEasingFunction EasingFunction 65 | { 66 | get 67 | { 68 | return (IEasingFunction)GetValue(EasingFunctionProperty); 69 | } 70 | set 71 | { 72 | SetValue(EasingFunctionProperty, value); 73 | } 74 | } 75 | 76 | public static readonly DependencyProperty EasingFunctionProperty = DependencyProperty.Register( 77 | EasingFunctionPropertyName, 78 | typeof(IEasingFunction), 79 | typeof(GridLengthAnimation), 80 | new UIPropertyMetadata(null)); 81 | 82 | public override object GetCurrentValue(object defaultOriginValue, 83 | object defaultDestinationValue, AnimationClock animationClock) 84 | { 85 | double fromVal = ((GridLength)GetValue(FromProperty)).Value; 86 | 87 | double toVal = ((GridLength)GetValue(ToProperty)).Value; 88 | 89 | double progress = animationClock.CurrentProgress.Value; 90 | 91 | IEasingFunction easingFunction = EasingFunction; 92 | if (easingFunction != null) 93 | { 94 | progress = easingFunction.Ease(progress); 95 | } 96 | 97 | 98 | if (fromVal > toVal) 99 | return new GridLength((1 - progress) * (fromVal - toVal) + toVal, GridUnitType.Star); 100 | 101 | return new GridLength(progress * (toVal - fromVal) + fromVal, GridUnitType.Star); 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /SmartFactory/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /SmartFactory/SmartFactory.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {3E2F4478-F067-4577-B5E2-52E5871D1C59} 8 | WinExe 9 | SmartFactory 10 | SmartFactory 11 | v4.6.1 12 | 512 13 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 14 | 4 15 | true 16 | 17 | 18 | 19 | 20 | x64 21 | true 22 | full 23 | false 24 | bin\Debug\ 25 | DEBUG;TRACE 26 | prompt 27 | 4 28 | 29 | 30 | AnyCPU 31 | pdbonly 32 | true 33 | bin\Release\ 34 | TRACE 35 | prompt 36 | 4 37 | 38 | 39 | 40 | 41 | 42 | 43 | False 44 | dll\DirectShowLib-2005.dll 45 | 46 | 47 | ..\packages\MahApps.Metro.1.5.0\lib\net45\MahApps.Metro.dll 48 | 49 | 50 | ..\packages\MahApps.Metro.IconPacks.2.0.0\lib\net46\MahApps.Metro.IconPacks.dll 51 | 52 | 53 | ..\packages\NAudio.1.8.4\lib\net35\NAudio.dll 54 | 55 | 56 | False 57 | dll\Python.Runtime.dll 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 4.0 70 | 71 | 72 | 73 | 74 | 75 | 76 | ..\packages\WPFMediaKit.2.2.0\lib\WPFMediaKit.dll 77 | 78 | 79 | 80 | 81 | MSBuild:Compile 82 | Designer 83 | 84 | 85 | 86 | 87 | 88 | MSBuild:Compile 89 | Designer 90 | 91 | 92 | App.xaml 93 | Code 94 | 95 | 96 | 97 | MainWindow.xaml 98 | Code 99 | 100 | 101 | 102 | 103 | Code 104 | 105 | 106 | True 107 | True 108 | Resources.resx 109 | 110 | 111 | True 112 | Settings.settings 113 | True 114 | 115 | 116 | ResXFileCodeGenerator 117 | Resources.Designer.cs 118 | 119 | 120 | Always 121 | 122 | 123 | Always 124 | 125 | 126 | Always 127 | 128 | 129 | Always 130 | 131 | 132 | 133 | SettingsSingleFileGenerator 134 | Settings.Designer.cs 135 | 136 | 137 | Always 138 | 139 | 140 | Always 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | Always 152 | 153 | 154 | 155 | 156 | {a3d0f8aa-647b-4f74-9300-4717bac083c1} 157 | iFlyDotNet 158 | 159 | 160 | {6aed0ed3-a394-4527-8a2d-b4f206c315ce} 161 | PyFaceRecognition 162 | 163 | 164 | 165 | 166 | 167 | 168 | 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 169 | 170 | 171 | 172 | -------------------------------------------------------------------------------- /iFlyDotNet/IFlyAsr.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Runtime.InteropServices; 6 | using System.Text; 7 | using System.Threading; 8 | using System.Threading.Tasks; 9 | using System.Xml; 10 | 11 | namespace iFlyDotNet 12 | { 13 | 14 | public class IFlyAsr 15 | { 16 | [DllImport(IFlyCommon.DllName)] 17 | private static extern int QISRAudioWrite(string sessionID, byte[] waveData, uint waveLen, int audioStatus, ref int epStatus, ref int recogStatus); 18 | 19 | [DllImport(IFlyCommon.DllName)] 20 | private static extern IntPtr QISRGetResult(string sessionID, ref int rsltStatus, int waitTime, ref int errorCode); 21 | 22 | [DllImport(IFlyCommon.DllName)] 23 | private static extern int QISRBuildGrammar(string grammarType, byte[] grammarContent, uint grammarLength, string param, BuildGrammarCallBack callback, IntPtr userData); 24 | 25 | [DllImport(IFlyCommon.DllName)] 26 | private static extern IntPtr QISRSessionBegin(string grammarList, string param, ref int errorCode); 27 | 28 | [DllImport(IFlyCommon.DllName)] 29 | private static extern int QISRSessionEnd(string sessionID, string hints); 30 | 31 | private delegate int BuildGrammarCallBack(int ecode, string info, IntPtr c); 32 | 33 | private struct UserData 34 | { 35 | public int build_fini; //标识语法构建是否完成 36 | public int update_fini; //标识更新词典是否完成 37 | public int errcode; //记录语法构建或更新词典回调错误码 38 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 39 | public string grammar_id; //保存语法构建返回的语法ID 40 | } 41 | 42 | private UserData globalUserData 43 | { 44 | get 45 | { 46 | return Marshal.PtrToStructure(userDataPtr); 47 | } 48 | set 49 | { 50 | if (userDataPtr != null) 51 | Marshal.StructureToPtr(value, userDataPtr, true); 52 | } 53 | } 54 | private readonly string asrResPath = "fo|asr/common.jet"; 55 | private readonly string grmBuildPath = "asr/GrmBuild"; 56 | private IntPtr userDataPtr; 57 | private string sessionId; 58 | int errorCode = -1; 59 | int epStatus = -1; 60 | int recStatus = -1; 61 | private IFlyAsr() 62 | { 63 | 64 | } 65 | 66 | private static IFlyAsr _iFlyAsr; 67 | 68 | public Action GetResult; 69 | public static IFlyAsr GetInstance() 70 | { 71 | return _iFlyAsr ?? (_iFlyAsr = new IFlyAsr()); 72 | } 73 | 74 | public void Init(string bnfPath) 75 | { 76 | userDataPtr = Marshal.AllocHGlobal(Marshal.SizeOf()); 77 | 78 | byte[] bnfStr; 79 | using (FileStream fs = new FileStream(bnfPath, FileMode.Open)) 80 | { 81 | bnfStr = new byte[fs.Length]; 82 | fs.Read(bnfStr, 0, (int)fs.Length); 83 | } 84 | string param = string.Format("engine_type = local,asr_res_path = {0}, sample_rate = 16000, grm_build_path = {1}, ", asrResPath, grmBuildPath); 85 | int BuildGrammarCallBack(int ecode, string info, IntPtr ptr) 86 | { 87 | var data = Marshal.PtrToStructure(ptr); 88 | data.build_fini = 1; 89 | data.errcode = ecode; 90 | if (ecode == 0 && info != null) 91 | data.grammar_id = info; 92 | globalUserData = data; 93 | return 0; 94 | } 95 | var ret = QISRBuildGrammar("bnf", bnfStr, (uint)bnfStr.Length, param, BuildGrammarCallBack, userDataPtr); 96 | if (ret != 0) 97 | throw new Exception("构建语法调用失败"); 98 | while (globalUserData.build_fini != 1) 99 | Thread.Sleep(100); 100 | if (globalUserData.errcode != 0) 101 | throw new Exception("构建语法失败,错误码:" + globalUserData.errcode); 102 | } 103 | 104 | public string RunAsr(byte[] pcmData, int confidence) 105 | { 106 | string xml = null; 107 | string param = string.Format("engine_type = local, " + 108 | "asr_res_path = {0}, asr_threshold = {1}, " + 109 | "sample_rate = 16000, grm_build_path = {2}, " + 110 | "local_grammar = {3}, result_type = xml, " + 111 | "result_encoding = GB2312, ", asrResPath, confidence, grmBuildPath, globalUserData.grammar_id); 112 | int errorCode = -1; 113 | var sessionId = Marshal.PtrToStringAnsi(QISRSessionBegin(null, param, ref errorCode)); 114 | if (sessionId == null) 115 | throw new Exception("QISRSessionBegin errorCode: " + errorCode); 116 | try 117 | { 118 | int epStatus = -1, recStatus = -1; 119 | errorCode = QISRAudioWrite(sessionId, pcmData, (uint)pcmData.Length, 1, ref epStatus, ref recStatus); 120 | if (errorCode != 0) 121 | throw new Exception("QISRAudioWrite errorCode: " + errorCode); 122 | errorCode = QISRAudioWrite(sessionId, null, 0, 4, ref epStatus, ref recStatus); 123 | if (errorCode != 0) 124 | throw new Exception("QISRAudioWrite errorCode: " + errorCode); 125 | int rssStatus = -1; 126 | while (5 != rssStatus && 0 == errorCode) 127 | { 128 | xml = Marshal.PtrToStringAnsi(QISRGetResult(sessionId, ref rssStatus, 0, ref errorCode)); 129 | } 130 | if (errorCode != 0) 131 | throw new Exception("QISRGetResult errorCode: " + errorCode); 132 | QISRSessionEnd(sessionId, null); 133 | } 134 | catch (Exception ex) 135 | { 136 | QISRSessionEnd(sessionId, null); 137 | throw ex; 138 | } 139 | if (xml == null) 140 | return null; 141 | XmlDocument xmlDoc = new XmlDocument(); 142 | xmlDoc.LoadXml(xml); 143 | var node = xmlDoc.SelectSingleNode("//command"); 144 | return node.InnerText; 145 | } 146 | 147 | public void RunAsrRealTime(byte[] pcmData, int confidence) 148 | { 149 | string xml = null; 150 | if (sessionId == null) 151 | { 152 | string param = string.Format("engine_type = local, " + 153 | "asr_res_path = {0}, asr_threshold = {1}, " + 154 | "sample_rate = 16000, grm_build_path = {2}, " + 155 | "local_grammar = {3}, result_type = xml, " + 156 | "result_encoding = GB2312, vad_eos = 500", asrResPath, confidence, grmBuildPath, globalUserData.grammar_id); 157 | sessionId = Marshal.PtrToStringAnsi(QISRSessionBegin(null, param, ref errorCode)); 158 | if (sessionId == null) 159 | throw new Exception("QISRSessionBegin errorCode: " + errorCode); 160 | errorCode = QISRAudioWrite(sessionId, pcmData, (uint)pcmData.Length, 1, ref epStatus, ref recStatus); //first 161 | if (errorCode != 0) 162 | throw new Exception("QISRAudioWrite errorCode: " + errorCode); 163 | } 164 | try 165 | { 166 | if (epStatus != 3) 167 | { 168 | errorCode = QISRAudioWrite(sessionId, pcmData, (uint)pcmData.Length, 2, ref epStatus, ref recStatus); //continue 169 | if (errorCode != 0) 170 | throw new Exception("QISRAudioWrite errorCode: " + errorCode); 171 | return; 172 | } 173 | errorCode = QISRAudioWrite(sessionId, null, 0, 4, ref epStatus, ref recStatus); //last 174 | if (errorCode != 0) 175 | throw new Exception("QISRAudioWrite errorCode: " + errorCode); 176 | int rssStatus = -1; 177 | while (5 != rssStatus && 0 == errorCode) 178 | { 179 | xml = Marshal.PtrToStringAnsi(QISRGetResult(sessionId, ref rssStatus, 0, ref errorCode)); 180 | } 181 | if (errorCode != 0) 182 | throw new Exception("QISRGetResult errorCode: " + errorCode); 183 | QISRSessionEnd(sessionId, null); 184 | sessionId = null; 185 | } 186 | catch (Exception ex) 187 | { 188 | QISRSessionEnd(sessionId, null); 189 | sessionId = null; 190 | GetResult?.Invoke(ex.Message); 191 | } 192 | if (xml == null) 193 | return; 194 | XmlDocument xmlDoc = new XmlDocument(); 195 | xmlDoc.LoadXml(xml); 196 | var result = xmlDoc.SelectSingleNode("//command").InnerText; 197 | GetResult?.Invoke(result); 198 | } 199 | 200 | public void StopAsrRealTime() 201 | { 202 | if (sessionId != null) 203 | { 204 | QISRSessionEnd(sessionId, null); 205 | sessionId = null; 206 | } 207 | } 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /SmartFactory/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using System.Windows.Navigation; 2 | using System.Collections.ObjectModel; 3 | using System.Runtime.ConstrainedExecution; 4 | using System.Drawing.Imaging; 5 | using MahApps.Metro.Controls; 6 | using MahApps.Metro.Controls.Dialogs; 7 | using PyFaceRecognition; 8 | using System; 9 | using System.Collections.Generic; 10 | using System.Drawing; 11 | using System.IO; 12 | using System.Linq; 13 | using System.Text; 14 | using System.Threading.Tasks; 15 | using System.Windows; 16 | using System.Windows.Controls; 17 | using System.Windows.Data; 18 | using System.Windows.Documents; 19 | using System.Windows.Input; 20 | using System.Windows.Media; 21 | using System.Windows.Media.Imaging; 22 | using WPFMediaKit.DirectShow.Controls; 23 | using System.Windows.Media.Animation; 24 | using iFlyDotNet; 25 | using System.Threading; 26 | 27 | namespace SmartFactory 28 | { 29 | /// 30 | /// MainWindow.xaml 的交互逻辑 31 | /// 32 | public partial class MainWindow : MetroWindow 33 | { 34 | FaceRecoginition faceRec; 35 | 36 | NAudioHelper nAudioHelper; 37 | 38 | private Queue resultQueue=new Queue(); 39 | 40 | private const string adminPwd = "123"; 41 | 42 | private readonly string loginParam = "appid = 5a404c2c"; 43 | 44 | private readonly string bnfPath = "tengen.bnf"; 45 | 46 | public ObservableCollection FaceList { get; } = new ObservableCollection(); 47 | 48 | private ICommand deleteFaceCommand; 49 | 50 | public ICommand DeleteFaceCommand 51 | { 52 | get 53 | { 54 | return deleteFaceCommand ?? (deleteFaceCommand = new SimpleCommand(DeleteFace)); 55 | } 56 | } 57 | 58 | 59 | public MainWindow() 60 | { 61 | InitializeComponent(); 62 | txtTip.Text = ""; 63 | faceRec = FaceRecoginition.GetInstance(new Func(() => GetBitmap())); 64 | faceRec.ResultEvent += ResultHandler; 65 | faceRec.ExceptionEvent += (msg) => Console.WriteLine(msg); 66 | if (loginParam != "") 67 | { 68 | IFlyCommon.Login(loginParam); 69 | IFlyAsr.GetInstance().Init(bnfPath); 70 | IFlyAsr.GetInstance().GetResult += (asrStr) => 71 | { 72 | this.ShowModalMessageExternal("识别结果", asrStr); 73 | //var ttsStr = Talkback.GetResult(asrStr); 74 | //if (ttsStr == null) 75 | // return; 76 | //var data = IFlyTTS.GetInstance().GetPcmDataByTTS(ttsStr); 77 | //nAudioHelper.Play(data); 78 | }; 79 | nAudioHelper = new NAudioHelper(); 80 | nAudioHelper.PcmDataAvailable += (data) => IFlyAsr.GetInstance().RunAsrRealTime(data, 60); 81 | } 82 | } 83 | 84 | private void MetroWindow_Loaded(object sender, RoutedEventArgs e) 85 | { 86 | if (MultimediaUtil.VideoInputDevices.Any()) 87 | videoCaptureElement.VideoCaptureDevice = MultimediaUtil.VideoInputDevices[0]; 88 | faceRec?.Start(); 89 | nAudioHelper?.StartRec(); 90 | } 91 | 92 | private Bitmap GetBitmap() 93 | { 94 | Bitmap bitmap = null; 95 | this.Dispatcher.Invoke(() => 96 | { 97 | var imageSource = videoCaptureElement.CloneSingleFrameImage().Source; 98 | bitmap = ImageSourceToBitmap(imageSource); 99 | }); 100 | return bitmap; 101 | 102 | Bitmap ImageSourceToBitmap(ImageSource imageSource) 103 | { 104 | int width=0; 105 | int height=0; 106 | if (imageSource == null) 107 | throw new Exception("ImageSource is null"); 108 | var bitmapSource = imageSource as BitmapSource; 109 | if (bitmapSource == null) 110 | { 111 | width = (int)imageSource.Width; 112 | height = (int)imageSource.Height; 113 | var dv = new DrawingVisual(); 114 | using (var dc = dv.RenderOpen()) 115 | { 116 | dc.DrawImage(imageSource, new Rect(0, 0, width, height)); 117 | } 118 | var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); 119 | rtb.Render(dv); 120 | bitmapSource = BitmapFrame.Create(rtb); 121 | } 122 | using (MemoryStream ms = new MemoryStream()) 123 | { 124 | var encoder = new PngBitmapEncoder(); 125 | encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); 126 | encoder.Save(ms); 127 | 128 | Bitmap bp = new Bitmap(ms); 129 | int startX= (int)(width * 0.26); 130 | int startY= (int)(height * 0.18); 131 | int iWidth = (int)(width * 0.48); 132 | int iHeight = (int)(height * 0.64); 133 | Bitmap bmpout = new Bitmap(iWidth, iHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); 134 | Graphics g = Graphics.FromImage(bmpout); 135 | g.DrawImage(bp, new Rectangle(0,0, iWidth, iHeight), new Rectangle(startX, startY, iWidth,iHeight), GraphicsUnit.Pixel); 136 | g.Dispose(); 137 | return bmpout; 138 | } 139 | } 140 | } 141 | 142 | private void MetroWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) 143 | { 144 | faceRec?.Stop(); 145 | nAudioHelper?.StopRec(); 146 | } 147 | 148 | private void ResultHandler(string result) 149 | { 150 | resultQueue.Enqueue(result); 151 | if (resultQueue.Count < 2) 152 | return; 153 | if (resultQueue.Max() == resultQueue.Min()) 154 | { 155 | this.Dispatcher.Invoke(() => 156 | { 157 | switch (result) 158 | { 159 | case "未检测到人脸": 160 | if (txtTip.Text != "请将脸放入识别框中") 161 | { 162 | txtTip.Text = "请将脸放入识别框中"; 163 | Storyboard story = (Storyboard)this.FindResource("TipStory"); 164 | story.Begin(); 165 | } 166 | break; 167 | case "请先录入人脸": 168 | if (txtTip.Text != "请先录入人脸") 169 | { 170 | txtTip.Text = "请先录入人脸"; 171 | Storyboard story = (Storyboard)this.FindResource("TipStory"); 172 | story.Begin(); 173 | } 174 | break; 175 | case "未知": 176 | break; 177 | default: 178 | if (!txtTip.Text.Contains(result)) 179 | { 180 | txtTip.Text = "你好,"+result; 181 | Storyboard story = (Storyboard)this.FindResource("TipStory"); 182 | story.Begin(); 183 | AuthenticationPassed(); 184 | } 185 | break; 186 | } 187 | }); 188 | 189 | resultQueue.Clear(); 190 | return; 191 | } 192 | resultQueue.Dequeue(); 193 | } 194 | 195 | private void btnOk_Click(object sender, RoutedEventArgs e) 196 | { 197 | if (txtUserName.Text != "") 198 | { 199 | videoCaptureElement.Pause(); 200 | if (!PasswordAuthentication()) 201 | { 202 | videoCaptureElement.Play(); 203 | return; 204 | } 205 | else 206 | faceRec.EncodingEnter(txtUserName.Text); 207 | videoCaptureElement.Play(); 208 | txtUserName.Text = ""; 209 | tbFaceManagement.IsChecked = false; 210 | tbFaceManagement_Click(null, null); 211 | } 212 | else 213 | this.ShowModalMessageExternal("提示", "请输入用户名"); 214 | } 215 | 216 | private void btnCancel_Click(object sender, RoutedEventArgs e) 217 | { 218 | txtUserName.Text = ""; 219 | tbFaceManagement.IsChecked = false; 220 | tbFaceManagement_Click(null, null); 221 | } 222 | 223 | private void tbFaceManagement_Click(object sender, RoutedEventArgs e) 224 | { 225 | if ((bool)tbFaceManagement.IsChecked) 226 | { 227 | faceRec.ResultEvent = null; 228 | txtTip.Text = "请将脸放入取景框中"; 229 | Storyboard story = (Storyboard)this.FindResource("TipStory"); 230 | story.Begin(); 231 | } 232 | else 233 | { 234 | txtTip.Text = ""; 235 | faceRec.ResultEvent += ResultHandler; 236 | } 237 | } 238 | 239 | private void tbFaceKnown_Click(object sender, RoutedEventArgs e) 240 | { 241 | if ((bool)tbFaceKnown.IsChecked) 242 | { 243 | FaceList.Clear(); 244 | faceRec.FaceList?.ForEach((face) => 245 | { 246 | FaceList.Add(face); 247 | }); 248 | } 249 | } 250 | 251 | private void DeleteFace(object obj) 252 | { 253 | if (!PasswordAuthentication()) 254 | return; 255 | var faceName = obj as string; 256 | if (faceName == null) 257 | return; 258 | FaceList.Remove(faceName); 259 | faceRec.EncodingDelete(faceName); 260 | } 261 | 262 | private bool PasswordAuthentication() 263 | { 264 | LoginDialogData result = this.ShowModalLoginExternal("身份验证", "输入密码", new LoginDialogSettings { ColorScheme = this.MetroDialogOptions.ColorScheme, ShouldHideUsername = true }); 265 | if (result != null) 266 | { 267 | if (result.Password == adminPwd) 268 | return true; 269 | } 270 | this.ShowModalMessageExternal("提示", "密码错误"); 271 | return false; 272 | } 273 | 274 | private void btnPwdLogin_Click(object sender, RoutedEventArgs e) 275 | { 276 | if (PasswordAuthentication()) 277 | AuthenticationPassed(); 278 | } 279 | 280 | private void AuthenticationPassed() 281 | { 282 | 283 | } 284 | } 285 | } 286 | -------------------------------------------------------------------------------- /PyFaceRecognition/FaceRecoginition.cs: -------------------------------------------------------------------------------- 1 | using Python.Runtime; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Drawing; 5 | using System.Drawing.Imaging; 6 | using System.IO; 7 | using System.Linq; 8 | using System.Text; 9 | using System.Threading; 10 | using System.Threading.Tasks; 11 | using System.Xml.Serialization; 12 | 13 | namespace PyFaceRecognition 14 | { 15 | public class FaceRecoginition 16 | { 17 | private dynamic face_recognition = null; 18 | private dynamic np = null; 19 | private dynamic io = null; 20 | private dynamic pil = null; 21 | private dynamic builtins = null; 22 | private FaceEncodings recFaceEncodings; 23 | private CancellationTokenSource faceRecTokenSource; 24 | private Task faceRecTask; 25 | private static FaceRecoginition instance; 26 | private Func getBitmap; 27 | private volatile float threshold = 0.3f; 28 | private volatile int editEncodings=0; 29 | private volatile string faceName=""; 30 | private static object locker = new object(); 31 | private FaceRecoginition(Func func) 32 | { 33 | string exeDir = AppDomain.CurrentDomain.BaseDirectory; 34 | string envPythonHome = exeDir + @"Python35"; 35 | Environment.SetEnvironmentVariable("PYTHONHOME", envPythonHome, EnvironmentVariableTarget.Process); 36 | Environment.SetEnvironmentVariable("PYTHONPATH", envPythonHome + "\\Lib;" 37 | + envPythonHome + "\\Lib\\site-packages;" 38 | + envPythonHome + "\\DLLs", EnvironmentVariableTarget.Process); 39 | Environment.SetEnvironmentVariable("PATH", envPythonHome, EnvironmentVariableTarget.Process); 40 | getBitmap = func; 41 | 42 | } 43 | 44 | public float Threshold { get=>threshold; set=> threshold = value; } 45 | 46 | public List FaceList 47 | { 48 | get 49 | { 50 | if (recFaceEncodings == null) 51 | return null; 52 | return recFaceEncodings.Items.Select((face) => face.Name).Distinct().ToList(); 53 | } 54 | } 55 | 56 | public Action ResultEvent; 57 | 58 | public Action ExceptionEvent; 59 | 60 | public bool IsRunning 61 | { 62 | get 63 | { 64 | return faceRecTask == null ? false: faceRecTask.Status == TaskStatus.Running; 65 | } 66 | } 67 | 68 | public static FaceRecoginition GetInstance(Func func) 69 | { 70 | return instance ?? (instance = new FaceRecoginition(func)); 71 | } 72 | 73 | public void Start() 74 | { 75 | if (!IsRunning) 76 | { 77 | faceRecTask = new Task(() => RecThread(threshold), (faceRecTokenSource = new CancellationTokenSource()).Token); 78 | faceRecTask.Start(); 79 | } 80 | } 81 | 82 | private void RecThread(float threshold) 83 | { 84 | try 85 | { 86 | 87 | if (!PythonEngine.IsInitialized) 88 | PythonEngine.Initialize(); 89 | var ts = PythonEngine.BeginAllowThreads(); 90 | using (Py.GIL()) 91 | { 92 | builtins = PythonEngine.ImportModule("builtins"); 93 | np = PythonEngine.ImportModule("numpy"); 94 | face_recognition = PythonEngine.ImportModule("face_recognition"); 95 | io = PythonEngine.ImportModule("io"); 96 | pil = PythonEngine.ImportModule("PIL"); 97 | string result = ""; 98 | while (!faceRecTokenSource.IsCancellationRequested) 99 | { 100 | var unknown_face_encodings = GetUnknownFaceEncodings(); 101 | if (unknown_face_encodings == null) //未检测到人脸 102 | { 103 | result = "未检测到人脸"; 104 | editEncodings = 0; 105 | goto OnEvent; 106 | } 107 | var unknown_face_encoding = unknown_face_encodings[0]; 108 | if (editEncodings > 0) //录入或删除 109 | { 110 | var unknownFaceEncoding = (unknown_face_encoding as PyObject).As(); 111 | using (FileStream fs = new FileStream("ValidFaceEncodings.xml", FileMode.OpenOrCreate)) 112 | { 113 | XmlSerializer xmlSerializer = new XmlSerializer(typeof(FaceEncodings)); 114 | try 115 | { 116 | recFaceEncodings = (FaceEncodings)xmlSerializer.Deserialize(fs); 117 | if (recFaceEncodings == null) 118 | throw new Exception(); 119 | switch (editEncodings) 120 | { 121 | case 1: //录入 122 | if (recFaceEncodings.Count(faceName) >= 5) 123 | recFaceEncodings[faceName] = unknownFaceEncoding; 124 | else 125 | recFaceEncodings.Items.Add(new FaceEncoding(faceName, unknownFaceEncoding)); 126 | break; 127 | case 2: //删除 128 | if (recFaceEncodings[faceName] != null) 129 | recFaceEncodings.Items.RemoveAll((face) => face.Name == faceName); 130 | break; 131 | default: break; 132 | } 133 | } 134 | catch 135 | { 136 | if (editEncodings == 2) //删除 137 | goto exit; 138 | recFaceEncodings = new FaceEncodings(); 139 | recFaceEncodings.Items.Add(new FaceEncoding(faceName, unknownFaceEncoding)); 140 | } 141 | fs.SetLength(0); //clear 142 | xmlSerializer.Serialize(fs, recFaceEncodings); 143 | } 144 | exit: 145 | editEncodings = 0; 146 | faceName = ""; 147 | } 148 | else //识别 149 | { 150 | using (FileStream fs = new FileStream("ValidFaceEncodings.xml", FileMode.OpenOrCreate)) 151 | { 152 | XmlSerializer xmlSerializer = new XmlSerializer(typeof(FaceEncodings)); 153 | try 154 | { 155 | recFaceEncodings = (FaceEncodings)xmlSerializer.Deserialize(fs); 156 | if (recFaceEncodings == null) 157 | throw new Exception(); 158 | } 159 | catch 160 | { 161 | result = "请先录入人脸"; 162 | goto OnEvent; 163 | } 164 | } 165 | } 166 | var known_face_encodings = GetKnownFaceEncodings(); 167 | var face_distances = face_recognition.face_distance(known_face_encodings, unknown_face_encoding); 168 | var faceDistances = ((PyObject)face_distances).As().ToList(); 169 | var validDistances = faceDistances.Where((dis) => dis <= threshold); 170 | if (!validDistances.Any()) 171 | result = "未知"; 172 | else 173 | result = recFaceEncodings.Items[faceDistances.IndexOf(validDistances.Min())].Name; 174 | ((PyObject)face_distances).Dispose(); 175 | ((PyObject)unknown_face_encodings).Dispose(); 176 | ((PyObject)known_face_encodings).Dispose(); 177 | 178 | OnEvent: 179 | if (!faceRecTokenSource.IsCancellationRequested) 180 | ResultEvent?.Invoke(result); 181 | } 182 | 183 | dynamic GetKnownFaceEncodings() 184 | { 185 | var _known_face_encodings = new PyList(); 186 | var knownFaces = new List(); 187 | recFaceEncodings.Items.ForEach((face) => 188 | { 189 | knownFaces.Add(face.Encoding); 190 | }); 191 | 192 | knownFaces.ForEach((face) => 193 | { 194 | var known_face = new PyList(); 195 | var listface = new List(face); 196 | listface.ForEach((faceitem) => 197 | { 198 | known_face.Append(new PyFloat(faceitem)); 199 | }); 200 | _known_face_encodings.Append(np.array(known_face)); 201 | known_face.Dispose(); 202 | }); 203 | return _known_face_encodings; 204 | } 205 | 206 | dynamic GetUnknownFaceEncodings() 207 | { 208 | MemoryStream memoryStream = new MemoryStream(); 209 | try 210 | { 211 | getBitmap().Save(memoryStream, ImageFormat.Jpeg); 212 | } 213 | catch (Exception ex) 214 | { 215 | throw new Exception("获取图片失败,请检查图片源后重试。\n" + ex); 216 | } 217 | var imageBytes = memoryStream.ToArray(); 218 | var bytes = np.array(imageBytes.ToList(), Py.kw("dtype", np.uint8)); 219 | var image_bytes = io.BytesIO(bytes); 220 | var image = pil.Image.open(image_bytes); 221 | var unknown_image = np.array(image); 222 | var unknown_face_encodings = face_recognition.face_encodings(unknown_image); 223 | var _len = builtins.len(unknown_face_encodings); 224 | var len = ((PyObject)_len).As(); 225 | ((PyObject)bytes).Dispose(); 226 | ((PyObject)image_bytes).Dispose(); 227 | ((PyObject)image).Dispose(); 228 | ((PyObject)unknown_image).Dispose(); 229 | ((PyObject)_len).Dispose(); 230 | if (len > 0) 231 | return unknown_face_encodings; 232 | else 233 | return null; 234 | } 235 | 236 | } 237 | PythonEngine.EndAllowThreads(ts); 238 | } 239 | catch (Exception ex) 240 | { 241 | ExceptionEvent?.Invoke("RecThread Exception :" + ex); 242 | } 243 | } 244 | 245 | public void Stop() 246 | { 247 | if (IsRunning) 248 | { 249 | faceRecTokenSource.Cancel(); 250 | while (IsRunning) 251 | Thread.Sleep(100); 252 | } 253 | } 254 | 255 | public void Dispose() 256 | { 257 | ResultEvent = null; 258 | ExceptionEvent = null; 259 | getBitmap = null; 260 | Stop(); 261 | } 262 | 263 | public void EncodingEnter(string name) 264 | { 265 | faceName = name; 266 | editEncodings = 1; //录入 267 | } 268 | 269 | public void EncodingDelete(string name) 270 | { 271 | faceName = name; 272 | editEncodings = 2; //删除 273 | } 274 | } 275 | } 276 | -------------------------------------------------------------------------------- /SmartFactory/MainWindow.xaml: -------------------------------------------------------------------------------- 1 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | false 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 108 | 109 | 110 | 111 | 115 | 119 | 120 | 121 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 152 | 156 | 157 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 |