-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFormRecognition.cshtml.cs
83 lines (66 loc) · 2.46 KB
/
FormRecognition.cshtml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.IO;
using System.Threading.Tasks;
using Azure.AI.FormRecognizer;
using Azure.AI.FormRecognizer.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace CogServicesVisionSamples_202107.Pages
{
public class FormRecognitionModel : PageModel
{
[BindProperty]
//Input from web page
public IFormFile ImageFile { get; set; }
//Output to web page
public string ImageFileUrl { get; set; }
public string Result { get; set; }
public RecognizedForm RecognizedForm { get; set; }
// Setting for using FormRecognizer
private const string frKey = "YOUR_FORMRECOGNIZER_KEY";
private const string frEndpoint = "https://YOUR_LOCATION.api.cognitive.microsoft.com";
private const string frModelId = "YOUR_FORMRECOGNIZER_MODELID";
// Setting for FileIO
private readonly IWebHostEnvironment _host;
public FormRecognitionModel(IWebHostEnvironment host)
{
_host = host;
}
//public void OnGet()
//{
//}
public async Task<IActionResult> OnPostAsync()
{
// Show image on web page
var imageFilePath = Path.Combine(_host.WebRootPath, "images\\uploadedImage.jpg");
using (var fileStream = new FileStream(imageFilePath, FileMode.OpenOrCreate))
{
await ImageFile.CopyToAsync(fileStream);
}
ImageFileUrl = "/images/uploadedImage.jpg";
// Post image to Form Recognizer, get resut and show on web page
var frClient = new FormRecognizerClient(new Uri(frEndpoint), new Azure.AzureKeyCredential(frKey));
try
{
var frOperation = await frClient.StartRecognizeCustomFormsAsync(frModelId, ImageFile.OpenReadStream());
var frResponse = await frOperation.WaitForCompletionAsync();
if (frResponse.Value != null)
{
Result = "結果:";
RecognizedForm = frResponse.Value[0];
}
else
{
Result = "判定できませんでした";
}
}
catch (ApplicationException e)
{
Result = "エラー: " + e.Message;
}
return Page();
}
}
}