aboutsummaryrefslogtreecommitdiff
path: root/src/client/cognitive_services
diff options
context:
space:
mode:
authorSam Wilkins <samwilkins333@gmail.com>2019-07-23 22:07:13 -0400
committerSam Wilkins <samwilkins333@gmail.com>2019-07-23 22:07:13 -0400
commitcb95d4b43238538f59babcaa0b0d9dabb9b393b7 (patch)
tree4e4128919d0a839bedb9e81dc6a74245ed6e183d /src/client/cognitive_services
parentd880e4b2fcb4e7bab3ee25d63209b173efcf37c0 (diff)
Ink recognition and presentation view toggle
Diffstat (limited to 'src/client/cognitive_services')
-rw-r--r--src/client/cognitive_services/CognitiveServices.ts86
1 files changed, 85 insertions, 1 deletions
diff --git a/src/client/cognitive_services/CognitiveServices.ts b/src/client/cognitive_services/CognitiveServices.ts
index d4085cf76..71e4276dc 100644
--- a/src/client/cognitive_services/CognitiveServices.ts
+++ b/src/client/cognitive_services/CognitiveServices.ts
@@ -8,10 +8,12 @@ import { RouteStore } from "../../server/RouteStore";
import { Utils } from "../../Utils";
import { CompileScript } from "../util/Scripting";
import { ComputedField } from "../../new_fields/ScriptField";
+import { InkData } from "../../new_fields/InkField";
export enum Services {
ComputerVision = "vision",
- Face = "face"
+ Face = "face",
+ Handwriting = "handwriting"
}
export enum Confidence {
@@ -132,4 +134,86 @@ export namespace CognitiveServices {
}
+ export namespace Inking {
+
+ export interface AzureStrokeData {
+ id: number;
+ points: string;
+ language?: string;
+ }
+
+ export interface HandwritingUnit {
+ version: number;
+ language: string;
+ unit: string;
+ strokes: AzureStrokeData[];
+ }
+
+ export const analyze = async (inkData: InkData, target: Doc) => {
+ return fetch(Utils.prepend(`${RouteStore.cognitiveServices}/${Services.Handwriting}`)).then(async response => {
+ let apiKey = await response.text();
+ if (!apiKey) {
+ return undefined;
+ }
+
+ let xhttp = new XMLHttpRequest();
+ let serverAddress = "https://api.cognitive.microsoft.com";
+ let endpoint = serverAddress + "/inkrecognizer/v1.0-preview/recognize";
+
+ let results = await new Promise<any>((resolve, reject) => {
+ let result: any;
+ xhttp.onreadystatechange = function () {
+ if (this.readyState === 4) {
+ try {
+ result = JSON.parse(xhttp.responseText);
+ } catch (e) {
+ return reject(e);
+ }
+ switch (this.status) {
+ case 200:
+ return resolve(result);
+ case 400:
+ default:
+ return reject(result);
+ }
+ }
+ };
+
+ xhttp.open("PUT", endpoint, true);
+ xhttp.setRequestHeader('Ocp-Apim-Subscription-Key', apiKey);
+ xhttp.setRequestHeader('Content-Type', 'application/json');
+ xhttp.send(JSON.stringify(toHandwritingUnit(inkData)));
+ });
+
+ let recognizedText = results.recognitionUnits.map((unit: any) => unit.recognizedText);
+ let individualWords = recognizedText.filter((text: string) => text && text.split(" ").length === 1);
+ target.inkAnalysis = Docs.Get.DocumentHierarchyFromJson(results.recognitionUnits, "Ink Analysis");
+ target.handwriting = individualWords.join(" ");
+ });
+ };
+
+ const toHandwritingUnit = (inkData: InkData): HandwritingUnit => {
+ let entries = inkData.entries(), next = entries.next();
+ let strokes: AzureStrokeData[] = [];
+ let id = 0;
+ while (!next.done) {
+ let entry = next.value;
+ let data = {
+ id: id++,
+ points: entry[1].pathData.map(point => `${point.x},${point.y}`).join(","),
+ language: "en-US"
+ };
+ strokes.push(data);
+ next = entries.next();
+ }
+ return {
+ version: 1,
+ language: "en-US",
+ unit: "mm",
+ strokes: strokes
+ };
+ };
+
+ }
+
} \ No newline at end of file