-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New Components - ocrspace #15311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
New Components - ocrspace #15311
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
184b9d9
ocrspace init
luancazarine c92f9e5
[Components] ocrspace #15148
luancazarine ceb186a
pnpm update
luancazarine 5f17db7
some adjusts
luancazarine 9545167
pnpm update
luancazarine 39ec3bc
fix file field name
luancazarine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { ConfigurationError } from "@pipedream/platform"; | ||
import FormData from "form-data"; | ||
import { getUrlOrFile } from "../../common/utils.mjs"; | ||
import ocrspace from "../../ocrspace.app.mjs"; | ||
|
||
export default { | ||
props: { | ||
ocrspace, | ||
language: { | ||
propDefinition: [ | ||
ocrspace, | ||
"language", | ||
], | ||
}, | ||
isOverlayRequired: { | ||
propDefinition: [ | ||
ocrspace, | ||
"isOverlayRequired", | ||
], | ||
}, | ||
detectOrientation: { | ||
propDefinition: [ | ||
ocrspace, | ||
"detectOrientation", | ||
], | ||
}, | ||
scale: { | ||
propDefinition: [ | ||
ocrspace, | ||
"scale", | ||
], | ||
}, | ||
isTable: { | ||
propDefinition: [ | ||
ocrspace, | ||
"isTable", | ||
], | ||
}, | ||
ocrEngine: { | ||
propDefinition: [ | ||
ocrspace, | ||
"ocrEngine", | ||
], | ||
}, | ||
}, | ||
async run({ $ }) { | ||
const data = new FormData(); | ||
const { | ||
url, file, | ||
} = getUrlOrFile(this.file); | ||
|
||
if (url) data.append("url", url); | ||
if (file) data.append("base64Image", file); | ||
if (this.imageLanguage) data.append("language", this.imageLanguage); | ||
luancazarine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (this.isOverlayRequired) data.append("isOverlayRequired", `${this.isOverlayRequired}`); | ||
if (this.filetype) data.append("filetype", this.filetype); | ||
luancazarine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (this.detectOrientation) data.append("detectOrientation", `${this.detectOrientation}`); | ||
if (this.scale) data.append("scale", `${this.scale}`); | ||
if (this.isTable) data.append("isTable", `${this.isTable}`); | ||
if (this.ocrEngine) data.append("OCREngine", this.ocrEngine); | ||
|
||
const response = await this.ocrspace.processImage({ | ||
$, | ||
data, | ||
headers: data.getHeaders(), | ||
}); | ||
luancazarine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$.export("$summary", this.getSummary()); | ||
|
||
if (response.ErrorMessage) { | ||
throw new ConfigurationError(response.ErrorMessage[0]); | ||
} | ||
|
||
return response; | ||
}, | ||
}; |
30 changes: 30 additions & 0 deletions
30
components/ocrspace/actions/process-image/process-image.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import common from "../common/process-base.mjs"; | ||
|
||
export default { | ||
...common, | ||
key: "ocrspace-process-image", | ||
name: "Process Image", | ||
description: "Submits an image file for OCR processing using OCR.space. [See the documentation](https://ocr.space/ocrapi)", | ||
version: "0.0.1", | ||
type: "action", | ||
props: { | ||
...common.props, | ||
file: { | ||
propDefinition: [ | ||
common.props.ocrspace, | ||
"file", | ||
], | ||
}, | ||
filetype: { | ||
propDefinition: [ | ||
common.props.ocrspace, | ||
"filetype", | ||
], | ||
}, | ||
}, | ||
methods: { | ||
getSummary() { | ||
return "Image submitted for OCR processing."; | ||
}, | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import common from "../common/process-base.mjs"; | ||
|
||
export default { | ||
...common, | ||
key: "ocrspace-process-pdf", | ||
name: "Process PDF for OCR", | ||
description: "Submit a PDF for OCR processing. [See the documentation](https://ocr.space/ocrapi)", | ||
version: "0.0.1", | ||
type: "action", | ||
props: { | ||
...common.props, | ||
file: { | ||
propDefinition: [ | ||
common.props.ocrspace, | ||
"file", | ||
], | ||
label: "PDF File", | ||
description: "The URL of the PDF file or the path to the file saved to the `/tmp` directory (e.g. `/tmp/example.pdf`) to process. [See the documentation](https://pipedream.com/docs/workflows/steps/code/nodejs/working-with-files/#the-tmp-directory).", | ||
}, | ||
luancazarine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
methods: { | ||
getSummary() { | ||
return "Submitted PDF for OCR processing."; | ||
}, | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
export const LANGUAGE_OPTIONS = [ | ||
{ | ||
label: "Arabic", | ||
value: "ara", | ||
}, | ||
{ | ||
label: "Bulgarian", | ||
value: "bul", | ||
}, | ||
{ | ||
label: "Chinese (Simplified)", | ||
value: "chs", | ||
}, | ||
{ | ||
label: "Chinese (Traditional)", | ||
value: "cht", | ||
}, | ||
{ | ||
label: "Croatian", | ||
value: "hrv", | ||
}, | ||
{ | ||
label: "Czech", | ||
value: "cze", | ||
}, | ||
{ | ||
label: "Danish", | ||
value: "dan", | ||
}, | ||
{ | ||
label: "Dutch", | ||
value: "dut", | ||
}, | ||
{ | ||
label: "English", | ||
value: "eng", | ||
}, | ||
{ | ||
label: "Finnish", | ||
value: "fin", | ||
}, | ||
{ | ||
label: "French", | ||
value: "fre", | ||
}, | ||
{ | ||
label: "German", | ||
value: "ger", | ||
}, | ||
{ | ||
label: "Greek", | ||
value: "gre", | ||
}, | ||
{ | ||
label: "Hungarian", | ||
value: "hun", | ||
}, | ||
{ | ||
label: "Korean", | ||
value: "kor", | ||
}, | ||
{ | ||
label: "Italian", | ||
value: "ita", | ||
}, | ||
{ | ||
label: "Japanese", | ||
value: "jpn", | ||
}, | ||
{ | ||
label: "Polish", | ||
value: "pol", | ||
}, | ||
{ | ||
label: "Portuguese", | ||
value: "por", | ||
}, | ||
{ | ||
label: "Russian", | ||
value: "rus", | ||
}, | ||
{ | ||
label: "Slovenian", | ||
value: "slv", | ||
}, | ||
{ | ||
label: "Spanish", | ||
value: "spa", | ||
}, | ||
{ | ||
label: "Swedish", | ||
value: "swe", | ||
}, | ||
{ | ||
label: "Turkish", | ||
value: "tur", | ||
}, | ||
]; | ||
luancazarine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export const IMAGE_FILETYPE_OPTIONS = [ | ||
"GIF", | ||
"PNG", | ||
"JPG", | ||
"TIF", | ||
"BMP", | ||
]; | ||
|
||
export const OCR_ENGINE_OPTIONS = [ | ||
{ | ||
label: "OCR Engine 1", | ||
value: "1", | ||
}, | ||
{ | ||
label: "OCR Engine 2", | ||
value: "2", | ||
}, | ||
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import fs from "fs"; | ||
import mime from "mime"; | ||
|
||
export const isValidUrl = (urlString) => { | ||
var urlPattern = new RegExp("^(https?:\\/\\/)?" + // validate protocol | ||
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // validate domain name | ||
"((\\d{1,3}\\.){3}\\d{1,3}))" + // validate OR ip (v4) address | ||
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // validate port and path | ||
"(\\?[;&a-z\\d%_.~+=-]*)?" + // validate query string | ||
"(\\#[-a-z\\d_]*)?$", "i"); // validate fragment locator | ||
return !!urlPattern.test(urlString); | ||
}; | ||
|
||
export const checkTmp = (filename) => { | ||
if (filename.indexOf("/tmp") === -1) { | ||
return `/tmp/${filename}`; | ||
} | ||
return filename; | ||
}; | ||
|
||
export const getUrlOrFile = (url) => { | ||
if (!isValidUrl(url)) { | ||
const filePath = checkTmp(url); | ||
const data = fs.readFileSync(filePath); | ||
const mimeType = mime.getType(filePath); | ||
const base64Image = Buffer.from(data, "binary").toString("base64"); | ||
return { | ||
file: `data:${mimeType};base64,${base64Image}`, | ||
}; | ||
} | ||
return { | ||
url, | ||
}; | ||
}; | ||
luancazarine marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,90 @@ | ||
import { axios } from "@pipedream/platform"; | ||
import { | ||
IMAGE_FILETYPE_OPTIONS, | ||
LANGUAGE_OPTIONS, | ||
OCR_ENGINE_OPTIONS, | ||
} from "./common/constants.mjs"; | ||
|
||
export default { | ||
type: "app", | ||
app: "ocrspace", | ||
propDefinitions: {}, | ||
propDefinitions: { | ||
file: { | ||
type: "string", | ||
label: "Image", | ||
description: "The URL of the image or the path to the file saved to the `/tmp` directory (e.g. `/tmp/example.jpg`) to process. [See the documentation](https://pipedream.com/docs/workflows/steps/code/nodejs/working-with-files/#the-tmp-directory).", | ||
}, | ||
language: { | ||
type: "string", | ||
label: "Language", | ||
description: "Language setting for image OCR processing.", | ||
options: LANGUAGE_OPTIONS, | ||
optional: true, | ||
}, | ||
isOverlayRequired: { | ||
type: "boolean", | ||
label: "Is Overlay Required", | ||
description: "If true, returns the coordinates of the bounding boxes for each word. If false, the OCR'ed text is returned only as a text block (this makes the JSON reponse smaller). Overlay data can be used, for example, to show [text over the image](https://ocr.space/english).", | ||
optional: true, | ||
}, | ||
filetype: { | ||
type: "string", | ||
label: "File Type", | ||
description: "Overwrites the automatic file type detection based on content-type. Supported image file formats are png, jpg (jpeg), gif, tif (tiff) and bmp. For document ocr, the api supports the Adobe PDF format. Multi-page TIFF files are supported.", | ||
options: IMAGE_FILETYPE_OPTIONS, | ||
optional: true, | ||
}, | ||
detectOrientation: { | ||
type: "boolean", | ||
label: "Detect Orientation", | ||
description: "If set to true, the api autorotates the image correctly and sets the TextOrientation parameter in the JSON response. If the image is not rotated, then TextOrientation=0, otherwise it is the degree of the rotation, e. g. \"270\".", | ||
optional: true, | ||
}, | ||
scale: { | ||
type: "boolean", | ||
label: "Scale", | ||
description: "If set to true, the api does some internal upscaling. This can improve the OCR result significantly, especially for low-resolution PDF scans. Note that the front page demo uses scale=true, but the API uses scale=false by default. See also this OCR forum post.", | ||
optional: true, | ||
}, | ||
isTable: { | ||
type: "boolean", | ||
label: "Is Table", | ||
description: "If set to true, the OCR logic makes sure that the parsed text result is always returned line by line. This switch is recommended for [table OCR](https://ocr.space/tablerecognition), [receipt OCR](https://ocr.space/receiptscanning), invoice processing and all other type of input documents that have a table like structure.", | ||
optional: true, | ||
}, | ||
ocrEngine: { | ||
type: "string", | ||
label: "OCR Engine", | ||
description: "Engine 1 is default. [See OCR Engines](https://ocr.space/OCRAPI#ocrengine).", | ||
options: OCR_ENGINE_OPTIONS, | ||
optional: true, | ||
}, | ||
}, | ||
methods: { | ||
// this.$auth contains connected account data | ||
authKeys() { | ||
console.log(Object.keys(this.$auth)); | ||
_baseUrl() { | ||
return "https://api.ocr.space"; | ||
}, | ||
_headers(headers = {}) { | ||
return { | ||
"apikey": this.$auth.apikey, | ||
...headers, | ||
}; | ||
}, | ||
_makeRequest({ | ||
$ = this, path, headers, ...opts | ||
}) { | ||
return axios($, { | ||
url: this._baseUrl() + path, | ||
headers: this._headers(headers), | ||
...opts, | ||
}); | ||
}, | ||
processImage(opts = {}) { | ||
return this._makeRequest({ | ||
method: "POST", | ||
path: "/parse/image", | ||
...opts, | ||
}); | ||
luancazarine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.