diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +node_modules diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..1a80af9 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,27 @@ +{ + "arrowParens": "always", + "bracketSpacing": true, + "cursorOffset": -1, + "embeddedLanguageFormatting": "auto", + "endOfLine": "auto", + "filepath": "", + "htmlWhitespaceSensitivity": "css", + "insertPragma": false, + "jsxBracketSameLine": false, + "jsxSingleQuote": false, + "overrides": [], + "plugins": [], + "pluginSearchDirs": [], + "printWidth": 80, + "proseWrap": "preserve", + "quoteProps": "preserve", + "rangeEnd": -1, + "rangeStart": 0, + "requirePragma": false, + "semi": true, + "singleQuote": false, + "tabWidth": 4, + "trailingComma": "none", + "useTabs": true, + "vueIndentScriptAndStyle": false +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5d4ca13 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 NanoNet Technologies Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..60b731e --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# nanonets + +[NanoNets](https://nanonets.com)' Optical Character Recognition and Image Classification Node.js SDK. + +NOTE: This SDK is under development. **Please do not use in production.** + +Example usage: + +```javascript +import { OpticalCharacterRecognition } from "./nanonets/index.js"; + +const apiKey = ""; +const modelId = ""; +const urlArray = ["", ""]; +const startInterval = 18917; +const endInterval = 18919; +const fileId = ""; + +const ocr = new OpticalCharacterRecognition(apiKey, modelId); + +console.log(await ocr.getModelDetails()); +console.log(await ocr.getAllPredictedFileData(startInterval, endInterval)); +console.log(await ocr.getPredictedFileDataById(fileId)); +console.log(await ocr.predictUsingUrls(urlArray)); +console.log(await ocr.predictUsingUrlsAsync(urlArray)); +``` diff --git a/index.cjs b/index.cjs new file mode 100644 index 0000000..e69de29 diff --git a/index.js b/index.js new file mode 100644 index 0000000..2fd3532 --- /dev/null +++ b/index.js @@ -0,0 +1,107 @@ +import fetch from "node-fetch"; + +if (!globalThis.fetch) { + globalThis.fetch = fetch; +} + +export class OpticalCharacterRecognition { + constructor(apiKey, modelId) { + this.apiKey = apiKey; + this.modelId = modelId; + this.authHeaderVal = + "Basic " + Buffer.from(`${this.apiKey}:`).toString("base64"); + } + + async getModelDetails() { + const response = await fetch( + `https://app.nanonets.com/api/v2/OCR/Model/${this.modelId}`, + { + headers: { + "Authorization": this.authHeaderVal, + "Accept": "application/json" + } + } + ); + const data = response.json(); + + return data; + } + + async getAllPredictedFileData(startInterval, endInterval) { + const response = await fetch( + `https://app.nanonets.com/api/v2/Inferences/Model/${this.modelId}/ImageLevelInferences/?start_day_interval=${startInterval}¤t_batch_day=${endInterval}`, + { + headers: { + "Authorization": this.authHeaderVal, + "Accept": "application/json" + } + } + ); + const data = response.json(); + + return data; + } + + async getPredictedFileDataById(fileId) { + const response = await fetch( + `https://app.nanonets.com/api/v2/Inferences/Model/${this.modelId}/ImageLevelInferences/${fileId}`, + { + headers: { + "Authorization": this.authHeaderVal, + "Accept": "application/json" + } + } + ); + const data = response.json(); + + return data; + } + + async predictUsingUrls(urlArray) { + let encodedUrls = new URLSearchParams(); + for (let i = 0; i < urlArray.length; i++) { + encodedUrls.append("urls", urlArray[i]); + } + encodedUrls = encodedUrls.toString(); + + const response = await fetch( + `https://app.nanonets.com/api/v2/OCR/Model/${this.modelId}/LabelUrls`, + { + method: "POST", + headers: { + "Authorization": this.authHeaderVal, + "Content-Type": "application/x-www-form-urlencoded", + "Accept": "application/json" + }, + body: encodedUrls + } + ); + const data = response.json(); + + return data; + } + + async predictUsingUrlsAsync(urlArray) { + let encodedUrls = new URLSearchParams(); + for (let i = 0; i < urlArray.length; i++) { + encodedUrls.append("urls", urlArray[i]); + } + encodedUrls = encodedUrls.toString(); + + const response = await fetch( + `https://app.nanonets.com/api/v2/OCR/Model/${this.modelId}/LabelUrls/?async=true`, + { + method: "POST", + headers: { + "Authorization": this.authHeaderVal, + "Content-Type": "application/x-www-form-urlencoded", + "Accept": "application/json" + }, + body: encodedUrls + } + ); + const data = response.json(); + + return data; + } +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..bfb8feb --- /dev/null +++ b/package-lock.json @@ -0,0 +1,97 @@ +{ + "name": "nanonets", + "version": "0.0.1", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "version": "0.0.1", + "license": "MIT", + "dependencies": { + "node-fetch": "^3.0.0" + } + }, + "node_modules/data-uri-to-buffer": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-3.0.1.tgz", + "integrity": "sha512-WboRycPNsVw3B3TL559F7kuBUM4d8CgMEvk6xEJlOp7OBPjt6G7z8WMWlD2rOFZLk6OYfFIUGsCOWzcQH9K2og==", + "engines": { + "node": ">= 6" + } + }, + "node_modules/fetch-blob": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.1.2.tgz", + "integrity": "sha512-hunJbvy/6OLjCD0uuhLdp0mMPzP/yd2ssd1t2FCJsaA7wkWhpbp9xfuNVpv7Ll4jFhzp6T4LAupSiV9uOeg0VQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "paypal", + "url": "https://paypal.me/jimmywarting" + } + ], + "dependencies": { + "web-streams-polyfill": "^3.0.3" + }, + "engines": { + "node": "^12.20 || >= 14.13" + } + }, + "node_modules/node-fetch": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.0.0.tgz", + "integrity": "sha512-bKMI+C7/T/SPU1lKnbQbwxptpCrG9ashG+VkytmXCPZyuM9jB6VU+hY0oi4lC8LxTtAeWdckNCTa3nrGsAdA3Q==", + "dependencies": { + "data-uri-to-buffer": "^3.0.1", + "fetch-blob": "^3.1.2" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-fetch" + } + }, + "node_modules/web-streams-polyfill": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.1.1.tgz", + "integrity": "sha512-Czi3fG883e96T4DLEPRvufrF2ydhOOW1+1a6c3gNjH2aIh50DNFBdfwh2AKoOf1rXvpvavAoA11Qdq9+BKjE0Q==", + "engines": { + "node": ">= 8" + } + } + }, + "dependencies": { + "data-uri-to-buffer": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-3.0.1.tgz", + "integrity": "sha512-WboRycPNsVw3B3TL559F7kuBUM4d8CgMEvk6xEJlOp7OBPjt6G7z8WMWlD2rOFZLk6OYfFIUGsCOWzcQH9K2og==" + }, + "fetch-blob": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.1.2.tgz", + "integrity": "sha512-hunJbvy/6OLjCD0uuhLdp0mMPzP/yd2ssd1t2FCJsaA7wkWhpbp9xfuNVpv7Ll4jFhzp6T4LAupSiV9uOeg0VQ==", + "requires": { + "web-streams-polyfill": "^3.0.3" + } + }, + "node-fetch": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.0.0.tgz", + "integrity": "sha512-bKMI+C7/T/SPU1lKnbQbwxptpCrG9ashG+VkytmXCPZyuM9jB6VU+hY0oi4lC8LxTtAeWdckNCTa3nrGsAdA3Q==", + "requires": { + "data-uri-to-buffer": "^3.0.1", + "fetch-blob": "^3.1.2" + } + }, + "web-streams-polyfill": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.1.1.tgz", + "integrity": "sha512-Czi3fG883e96T4DLEPRvufrF2ydhOOW1+1a6c3gNjH2aIh50DNFBdfwh2AKoOf1rXvpvavAoA11Qdq9+BKjE0Q==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..5562faa --- /dev/null +++ b/package.json @@ -0,0 +1,35 @@ +{ + "name": "nanonets", + "version": "0.0.1", + "description": "NanoNets' Optical Character Recognition and Image Classification Node.js SDK.", + "main": "index.js", + "exports": { + "import": "./index.js", + "require": "./index.cjs", + "default": "./index.js" + }, + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/HarshKapadia2/nanonets.git" + }, + "keywords": [ + "nanonets", + "sdk", + "ocr", + "image-classification", + "nodejs" + ], + "author": "Harsh Kapadia ", + "license": "MIT", + "bugs": { + "url": "https://github.com/HarshKapadia2/nanonets/issues" + }, + "homepage": "https://github.com/HarshKapadia2/nanonets#readme", + "dependencies": { + "node-fetch": "^3.0.0" + } +}