Details
diff --git a/NeuralNetwork/NeuralNetwork.Model/NeuralNetwork.Model.csproj b/NeuralNetwork/NeuralNetwork.Model/NeuralNetwork.Model.csproj
index d42914c..b91bdb1 100644
--- a/NeuralNetwork/NeuralNetwork.Model/NeuralNetwork.Model.csproj
+++ b/NeuralNetwork/NeuralNetwork.Model/NeuralNetwork.Model.csproj
@@ -33,6 +33,8 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
+ <Reference Include="System.Drawing" />
+ <Reference Include="System.Drawing.Design" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
diff --git a/NeuralNetwork/NeuralNetwork.Model/Services/ITextRecognisingService.cs b/NeuralNetwork/NeuralNetwork.Model/Services/ITextRecognisingService.cs
index de7d181..f0bc300 100644
--- a/NeuralNetwork/NeuralNetwork.Model/Services/ITextRecognisingService.cs
+++ b/NeuralNetwork/NeuralNetwork.Model/Services/ITextRecognisingService.cs
@@ -6,8 +6,23 @@ using System.Threading.Tasks;
namespace NeuralNetwork.Model.Services
{
+ /// <summary>
+ /// Интерфейс для сервиса распознования
+ /// </summary>
public interface ITextRecognisingService
{
- int Work();
+ /// <summary>
+ /// Основной метод распознования
+ /// </summary>
+ /// <param name="image">картинка</param>
+ /// <returns></returns>
+ int Work(byte[] image);
+
+ /// <summary>
+ /// Вспомогательный метод, проверка открытия файла как картинки
+ /// </summary>
+ /// <param name="image"></param>
+ /// <returns></returns>
+ bool TryOpen(byte[] image);
}
}
diff --git a/NeuralNetwork/NeuralNetwork.Model/Services/TextRecognisingService.cs b/NeuralNetwork/NeuralNetwork.Model/Services/TextRecognisingService.cs
index 4f76039..89ddadb 100644
--- a/NeuralNetwork/NeuralNetwork.Model/Services/TextRecognisingService.cs
+++ b/NeuralNetwork/NeuralNetwork.Model/Services/TextRecognisingService.cs
@@ -4,11 +4,36 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using System.IO;
+using System.Drawing;
+
namespace NeuralNetwork.Model.Services
{
+ /// <summary>
+ /// Тестовая заглушка сервиса
+ /// </summary>
public class TextRecognisingTestService : ITextRecognisingService
{
- public int Work()
+ public bool TryOpen(byte[] image)
+ {
+ Bitmap bitmap = null;
+
+ try
+ {
+ using (MemoryStream stream = new MemoryStream(image))
+ {
+ bitmap = new Bitmap(stream);
+ }
+ }
+ catch (Exception)
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ public int Work(byte[] image)
{
return -1;
}
diff --git a/NeuralNetwork/NeuralNetwork.Test/TextRecognisingTest.cs b/NeuralNetwork/NeuralNetwork.Test/TextRecognisingTest.cs
index 9abb522..6c67605 100644
--- a/NeuralNetwork/NeuralNetwork.Test/TextRecognisingTest.cs
+++ b/NeuralNetwork/NeuralNetwork.Test/TextRecognisingTest.cs
@@ -5,6 +5,9 @@ using NeuralNetwork.Model.Services;
namespace NeuralNetwork.Test
{
+ /// <summary>
+ /// Unit тестирование работы сервиса распознания
+ /// </summary>
[TestClass]
public class TextRecognisingTest
{
@@ -14,7 +17,9 @@ namespace NeuralNetwork.Test
ITextRecognisingService TextRecognisingService
= new TextRecognisingTestService();
- if (TextRecognisingService.Work() != -1)
+ byte[] TestImageData = new byte[0];
+
+ if (TextRecognisingService.Work(TestImageData) != -1)
throw new Exception();
}
}
diff --git a/NeuralNetwork/NeuralNetwork.UI.Web/App_Start/BundleConfig.cs b/NeuralNetwork/NeuralNetwork.UI.Web/App_Start/BundleConfig.cs
index 7f5e218..8d49f82 100644
--- a/NeuralNetwork/NeuralNetwork.UI.Web/App_Start/BundleConfig.cs
+++ b/NeuralNetwork/NeuralNetwork.UI.Web/App_Start/BundleConfig.cs
@@ -1,6 +1,8 @@
using System.Web;
using System.Web.Optimization;
+using System.Collections.Generic;
+
namespace NeuralNetwork.UI.Web
{
public class BundleConfig
@@ -24,6 +26,58 @@ namespace NeuralNetwork.UI.Web
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/Site/site.css"));
+
+ SiteContent(bundles);
+ }
+
+
+ /// <summary>
+ /// Инструменты для работы с Canvas
+ /// </summary>
+ /// <param name="bundles"></param>
+ private static void RegisterCanvas(BundleCollection bundles)
+ {
+ bundles.Add(new ScriptBundle("~/bundles/Canvas/JS")
+ .Include("~/Scripts/Site/Tools/Canvas.js"));
+
+ bundles.Add(new StyleBundle("~/bundles/Canvas/CSS")
+ .Include("~/Content/Site/Tools/Canvas.css"));
+ }
+
+ /// <summary>
+ /// JS классы для работы с API
+ /// </summary>
+ /// <param name="bundles"></param>
+ private static void RegisterApi(BundleCollection bundles)
+ {
+ bundles.Add(new ScriptBundle("~/bundles/API")
+ .Include("~/Scripts/Site/API/*.js"));
+ }
+
+ /// <summary>
+ /// JS точка входа для страниц
+ /// </summary>
+ /// <param name="bundles"></param>
+ private static void RegisterPages(BundleCollection bundles)
+ {
+ var pages = new List<string>()
+ {
+ "TestPage",
+ "AppPage"
+ };
+
+ foreach (var elem in pages)
+ {
+ bundles.Add(new ScriptBundle("~/bundles/Pages/" + elem)
+ .Include("~/Scripts/Site/Pages/" + elem + ".js"));
+ }
+ }
+
+ private static void SiteContent(BundleCollection bundles)
+ {
+ RegisterCanvas(bundles);
+ RegisterApi(bundles);
+ RegisterPages(bundles);
}
}
}
diff --git a/NeuralNetwork/NeuralNetwork.UI.Web/App_Start/RouteConfig.cs b/NeuralNetwork/NeuralNetwork.UI.Web/App_Start/RouteConfig.cs
index 7a10c27..320a518 100644
--- a/NeuralNetwork/NeuralNetwork.UI.Web/App_Start/RouteConfig.cs
+++ b/NeuralNetwork/NeuralNetwork.UI.Web/App_Start/RouteConfig.cs
@@ -16,7 +16,7 @@ namespace NeuralNetwork.UI.Web
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
+ defaults: new { controller = "App", action = "Index", id = UrlParameter.Optional }
);
}
}
diff --git a/NeuralNetwork/NeuralNetwork.UI.Web/Content/Site/Tools/Canvas.css b/NeuralNetwork/NeuralNetwork.UI.Web/Content/Site/Tools/Canvas.css
new file mode 100644
index 0000000..b605821
--- /dev/null
+++ b/NeuralNetwork/NeuralNetwork.UI.Web/Content/Site/Tools/Canvas.css
@@ -0,0 +1,8 @@
+
+.CanvasContainer {
+ clear: both;
+}
+
+canvas {
+ border: 1px solid #7B899B;
+}
\ No newline at end of file
diff --git a/NeuralNetwork/NeuralNetwork.UI.Web/Controllers/API/TextRecognisingController.cs b/NeuralNetwork/NeuralNetwork.UI.Web/Controllers/API/TextRecognisingController.cs
new file mode 100644
index 0000000..857d80c
--- /dev/null
+++ b/NeuralNetwork/NeuralNetwork.UI.Web/Controllers/API/TextRecognisingController.cs
@@ -0,0 +1,67 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using System.Web.Mvc;
+
+using NeuralNetwork.Model.Services;
+using NeuralNetwork.UI.Web.Models.Service;
+using NeuralNetwork.UI.Web.Models.ViewModel;
+
+namespace NeuralNetwork.UI.Web.Controllers
+{
+ public class TextRecognisingController : Controller
+ {
+ readonly ITextRecognisingService TextRecognisingService
+ = new TextRecognisingTestService();
+
+ readonly ConvertService ConvertService
+ = new ConvertService();
+
+
+ /// <summary>
+ /// Выполняет распознование цифр на изображении
+ /// </summary>
+ /// <param name="file">Base64 картинка</param>
+ /// <returns></returns>
+ [HttpPost]
+ public JsonResult Recognising(string file)
+ {
+ TextRecognisingResponseModel model = null;
+
+ try
+ {
+ var image_data = ConvertService.Base64ToByte(file);
+
+ if (!TextRecognisingService.TryOpen(image_data))
+ {
+ model = new TextRecognisingResponseModel()
+ {
+ Successe = false,
+ Message = "Ошибка. Не удалось открыть файл как изображение"
+ };
+
+ return Json(model);
+ }
+
+
+ model = new TextRecognisingResponseModel()
+ {
+ Successe = true,
+ Message = "Ok",
+ Result = TextRecognisingService.Work(image_data)
+ };
+ }
+ catch (Exception ex)
+ {
+ model = new TextRecognisingResponseModel()
+ {
+ Successe = false,
+ Message = "Ошибка. В процессе обработки изображения произошла ошибка"
+ };
+ }
+
+ return Json(model);
+ }
+ }
+}
\ No newline at end of file
diff --git a/NeuralNetwork/NeuralNetwork.UI.Web/Models/Services/ConvertService.cs b/NeuralNetwork/NeuralNetwork.UI.Web/Models/Services/ConvertService.cs
new file mode 100644
index 0000000..c4ac78d
--- /dev/null
+++ b/NeuralNetwork/NeuralNetwork.UI.Web/Models/Services/ConvertService.cs
@@ -0,0 +1,47 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+
+using System.IO;
+
+namespace NeuralNetwork.UI.Web.Models.Service
+{
+ /// <summary>
+ /// Сервис для преобразования Base64 в byte
+ /// </summary>
+ public class ConvertService
+ {
+ public MemoryStream Base64ToStream(string base64)
+ {
+ MemoryStream res = new MemoryStream();
+ var data = Base64ToByte(base64);
+
+ using (StreamWriter writer = new StreamWriter(res))
+ {
+ writer.Write(data);
+ }
+
+ res.Position = 0;
+ return res;
+ }
+
+ public byte[] Base64ToByte(string base64)
+ {
+ return Convert.FromBase64String(RemovePrefix(base64));
+ }
+
+
+ /// <summary>
+ /// Удаляет лишний префикс у сообщения
+ /// </summary>
+ /// <param name="base64"></param>
+ /// <returns></returns>
+ private string RemovePrefix(string base64)
+ {
+ var start_data = base64.IndexOf(',');
+ return base64.Substring(start_data + 1);
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/NeuralNetwork/NeuralNetwork.UI.Web/Models/ViewModel/BaseResponseModel.cs b/NeuralNetwork/NeuralNetwork.UI.Web/Models/ViewModel/BaseResponseModel.cs
new file mode 100644
index 0000000..58c6c89
--- /dev/null
+++ b/NeuralNetwork/NeuralNetwork.UI.Web/Models/ViewModel/BaseResponseModel.cs
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+
+namespace NeuralNetwork.UI.Web.Models.ViewModel
+{
+ public class BaseResponseModel
+ {
+ public bool Successe { set; get; }
+ public string Message { set; get; }
+ }
+}
\ No newline at end of file
diff --git a/NeuralNetwork/NeuralNetwork.UI.Web/Models/ViewModel/TextRecognisingResponseModel.cs b/NeuralNetwork/NeuralNetwork.UI.Web/Models/ViewModel/TextRecognisingResponseModel.cs
new file mode 100644
index 0000000..35cd0d7
--- /dev/null
+++ b/NeuralNetwork/NeuralNetwork.UI.Web/Models/ViewModel/TextRecognisingResponseModel.cs
@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+
+namespace NeuralNetwork.UI.Web.Models.ViewModel
+{
+ public class TextRecognisingResponseModel : BaseResponseModel
+ {
+ public int Result { set; get; }
+ }
+}
\ No newline at end of file
diff --git a/NeuralNetwork/NeuralNetwork.UI.Web/NeuralNetwork.UI.Web.csproj b/NeuralNetwork/NeuralNetwork.UI.Web/NeuralNetwork.UI.Web.csproj
index b48a7fd..9bba830 100644
--- a/NeuralNetwork/NeuralNetwork.UI.Web/NeuralNetwork.UI.Web.csproj
+++ b/NeuralNetwork/NeuralNetwork.UI.Web/NeuralNetwork.UI.Web.csproj
@@ -120,11 +120,15 @@
<Compile Include="App_Start\BundleConfig.cs" />
<Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\RouteConfig.cs" />
- <Compile Include="Controllers\HomeController.cs" />
- <Compile Include="Controllers\TextRecognisingController.cs" />
+ <Compile Include="Controllers\AppController.cs" />
+ <Compile Include="Controllers\TestController.cs" />
+ <Compile Include="Controllers\API\TextRecognisingController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
+ <Compile Include="Models\Services\ConvertService.cs" />
+ <Compile Include="Models\ViewModel\BaseResponseModel.cs" />
+ <Compile Include="Models\ViewModel\TextRecognisingResponseModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
@@ -132,6 +136,7 @@
<Content Include="Content\bootstrap-theme.min.css" />
<Content Include="Content\bootstrap.css" />
<Content Include="Content\bootstrap.min.css" />
+ <Content Include="Content\Site\Tools\Canvas.css" />
<Content Include="favicon.ico" />
<Content Include="fonts\glyphicons-halflings-regular.svg" />
<Content Include="Global.asax" />
@@ -149,6 +154,10 @@
<Content Include="Scripts\jquery.validate.unobtrusive.js" />
<Content Include="Scripts\jquery.validate.unobtrusive.min.js" />
<Content Include="Scripts\modernizr-2.8.3.js" />
+ <Content Include="Scripts\Site\Api\API_TextRecognising.js" />
+ <Content Include="Scripts\Site\Pages\AppPage.js" />
+ <Content Include="Scripts\Site\Tools\Canvas.js" />
+ <Content Include="Scripts\Site\Pages\TestPage.js" />
<Content Include="Web.config" />
<Content Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
@@ -160,14 +169,11 @@
<Content Include="Views\_ViewStart.cshtml" />
<Content Include="Views\Shared\Error.cshtml" />
<Content Include="Views\Shared\_Layout.cshtml" />
- <Content Include="Views\Home\About.cshtml" />
- <Content Include="Views\Home\Contact.cshtml" />
- <Content Include="Views\Home\Index.cshtml" />
+ <Content Include="Views\Test\Index.cshtml" />
+ <Content Include="Views\App\Index.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
- <Folder Include="Models\" />
- <Folder Include="Scripts\Site\" />
<Folder Include="Views\TextRecognising\" />
</ItemGroup>
<ItemGroup>
diff --git a/NeuralNetwork/NeuralNetwork.UI.Web/NeuralNetwork.UI.Web.csproj.user b/NeuralNetwork/NeuralNetwork.UI.Web/NeuralNetwork.UI.Web.csproj.user
index 0e80e4f..a7608bd 100644
--- a/NeuralNetwork/NeuralNetwork.UI.Web/NeuralNetwork.UI.Web.csproj.user
+++ b/NeuralNetwork/NeuralNetwork.UI.Web/NeuralNetwork.UI.Web.csproj.user
@@ -14,9 +14,10 @@
<WebStackScaffolding_ControllerDialogWidth>600</WebStackScaffolding_ControllerDialogWidth>
<WebStackScaffolding_IsLayoutPageSelected>True</WebStackScaffolding_IsLayoutPageSelected>
<WebStackScaffolding_IsPartialViewSelected>False</WebStackScaffolding_IsPartialViewSelected>
- <WebStackScaffolding_IsReferencingScriptLibrariesSelected>True</WebStackScaffolding_IsReferencingScriptLibrariesSelected>
+ <WebStackScaffolding_IsReferencingScriptLibrariesSelected>False</WebStackScaffolding_IsReferencingScriptLibrariesSelected>
<WebStackScaffolding_LayoutPageFile />
<WebStackScaffolding_IsAsyncSelected>False</WebStackScaffolding_IsAsyncSelected>
+ <WebStackScaffolding_ViewDialogWidth>600</WebStackScaffolding_ViewDialogWidth>
</PropertyGroup>
<ProjectExtensions>
<VisualStudio>
diff --git a/NeuralNetwork/NeuralNetwork.UI.Web/Views/App/Index.cshtml b/NeuralNetwork/NeuralNetwork.UI.Web/Views/App/Index.cshtml
new file mode 100644
index 0000000..370748f
--- /dev/null
+++ b/NeuralNetwork/NeuralNetwork.UI.Web/Views/App/Index.cshtml
@@ -0,0 +1,27 @@
+
+@{
+ ViewBag.Title = "Index";
+}
+
+@*Canvas*@
+@Styles.Render("~/bundles/Canvas/CSS")
+@Scripts.Render("~/bundles/Canvas/JS")
+@* API *@
+@Scripts.Render("~/bundles/API")
+@* Page script *@
+@Scripts.Render("~/bundles/Pages/AppPage")
+
+<h2>Index</h2>
+
+<div class="CanvasContainer">
+ <canvas id="Canvas" width="500" height="300"></canvas>
+</div>
+
+<button id="Button_Clear" type="button" class="btn btn-secondary">Очистить</button>
+<button id="Button_Recognized" type="button" class="btn btn-secondary">Распознать</button>
+<div class="input-group mb-3">
+ <div class="input-group-prepend">
+ <span class="input-group-text" id="basic-addon1">Result:</span>
+ </div>
+ <input id="Input_Result" type="text" readonly="readonly" class="form-control" aria-describedby="basic-addon1">
+</div>
\ No newline at end of file
diff --git a/NeuralNetwork/NeuralNetwork.UI.Web/Views/Shared/_Layout.cshtml b/NeuralNetwork/NeuralNetwork.UI.Web/Views/Shared/_Layout.cshtml
index 7a6f777..6605d40 100644
--- a/NeuralNetwork/NeuralNetwork.UI.Web/Views/Shared/_Layout.cshtml
+++ b/NeuralNetwork/NeuralNetwork.UI.Web/Views/Shared/_Layout.cshtml
@@ -17,13 +17,15 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
- @Html.ActionLink("Имя приложения", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
+ @Html.ActionLink("TextRecognising", "Index", "App", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
- <li>@Html.ActionLink("Домашняя страница", "Index", "Home")</li>
+ @*<li>@Html.ActionLink("Домашняя страница", "Index", "Home")</li>
<li>@Html.ActionLink("Информация", "About", "Home")</li>
- <li>@Html.ActionLink("Связаться", "Contact", "Home")</li>
+ <li>@Html.ActionLink("Связаться", "Contact", "Home")</li>*@
+ <li>@Html.ActionLink("TextRecognising", "Index", "App")</li>
+ <li>@Html.ActionLink("Test", "Index", "Test")</li>
</ul>
</div>
</div>
diff --git a/NeuralNetwork/NeuralNetwork.UI.Web/Views/Test/Index.cshtml b/NeuralNetwork/NeuralNetwork.UI.Web/Views/Test/Index.cshtml
new file mode 100644
index 0000000..f4fac26
--- /dev/null
+++ b/NeuralNetwork/NeuralNetwork.UI.Web/Views/Test/Index.cshtml
@@ -0,0 +1,19 @@
+
+@{
+ ViewBag.Title = "Index";
+}
+
+@*Canvas*@
+@Styles.Render("~/bundles/Canvas/CSS");
+@Scripts.Render("~/bundles/Canvas/JS")
+
+@Scripts.Render("~/bundles/API")
+@Scripts.Render("~/bundles/Pages/TestPage")
+
+<h2>Index</h2>
+
+<div class="CanvasContainer">
+ <canvas id="Canvas" width="500" height="300"></canvas>
+</div>
+
+