diff --git a/components/platerecognizer/actions/run-recognition/run-recognition.mjs b/components/platerecognizer/actions/run-recognition/run-recognition.mjs index 54f0ddbb2da61..e424d3541f8bf 100644 --- a/components/platerecognizer/actions/run-recognition/run-recognition.mjs +++ b/components/platerecognizer/actions/run-recognition/run-recognition.mjs @@ -1,5 +1,6 @@ +import fs from "fs"; +import { checkTmp } from "../../common/utils.mjs"; import platerecognizer from "../../platerecognizer.app.mjs"; -import { axios } from "@pipedream/platform"; export default { key: "platerecognizer-run-recognition", @@ -9,35 +10,55 @@ export default { type: "action", props: { platerecognizer, - imageFileOrUrl: platerecognizer.propDefinitions.imageFileOrUrl, - regions: platerecognizer.propDefinitions.regions, - cameraId: platerecognizer.propDefinitions.cameraId, - mmc: platerecognizer.propDefinitions.mmc, - config: platerecognizer.propDefinitions.config, + imageFileOrUrl: { + type: "string", + label: "Image File or URL", + description: "The image file or URL to be recognized.", + }, + regions: { + type: "string[]", + label: "Regions", + description: "Regions to select specific license plate patterns. [See further details here](https://guides.platerecognizer.com/docs/other/country-codes/#country-codes)", + optional: true, + }, + cameraId: { + type: "string", + label: "Camera ID", + description: "The ID of the camera that took the image.", + optional: true, + }, + mmc: { + type: "boolean", + label: "MMC", + description: "Whether to detect vehicle make, model, and color.", + optional: true, + }, + config: { + type: "object", + label: "Config", + description: "Additional configuration. [See further details here](https://guides.platerecognizer.com/docs/snapshot/api-reference/#engine-configuration)", + optional: true, + }, }, async run({ $ }) { - const formData = new FormData(); + const fileObj = {}; + if (this.imageFileOrUrl.startsWith("http")) { - formData.append("upload", this.imageFileOrUrl); + fileObj.upload_url = this.imageFileOrUrl; } else { - const fs = require("fs"); - formData.append("upload", fs.createReadStream(this.imageFileOrUrl)); + const file = fs.readFileSync(checkTmp(this.imageFileOrUrl)); + fileObj.upload = Buffer(file).toString("base64"); } - if (this.regions) formData.append("regions", this.regions.join(",")); - if (this.cameraId) formData.append("camera_id", this.cameraId); - if (this.mmc !== undefined) formData.append("mmc", this.mmc); - if (this.config) formData.append("config", this.config); - const response = await axios(this, { - method: "POST", - url: `${this.platerecognizer._baseUrl()}/plate-reader/`, - headers: { - "Authorization": `Token ${this.platerecognizer.$auth.api_key}`, - "Content-Type": "multipart/form-data", + const response = await this.platerecognizer.runRecognition({ + $, + data: { + ...fileObj, + regions: this.regions, + camera_id: this.cameraId, + mmc: this.mmc, + config: this.config, }, - data: formData, - maxContentLength: Infinity, - maxBodyLength: Infinity, }); $.export("$summary", "Recognition process triggered successfully"); diff --git a/components/platerecognizer/common/utils.mjs b/components/platerecognizer/common/utils.mjs new file mode 100644 index 0000000000000..1a5e36f32a603 --- /dev/null +++ b/components/platerecognizer/common/utils.mjs @@ -0,0 +1,6 @@ +export const checkTmp = (filename) => { + if (!filename.startsWith("/tmp")) { + return `/tmp/${filename}`; + } + return filename; +}; diff --git a/components/platerecognizer/package.json b/components/platerecognizer/package.json index 137711b33dcfb..763385296dc5f 100644 --- a/components/platerecognizer/package.json +++ b/components/platerecognizer/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/platerecognizer", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Plate Recognizer Components", "main": "platerecognizer.app.mjs", "keywords": [ @@ -11,5 +11,9 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^1.6.5" } } + diff --git a/components/platerecognizer/platerecognizer.app.mjs b/components/platerecognizer/platerecognizer.app.mjs index 55739ef9964f1..5c83ad58b5f5e 100644 --- a/components/platerecognizer/platerecognizer.app.mjs +++ b/components/platerecognizer/platerecognizer.app.mjs @@ -3,71 +3,30 @@ import { axios } from "@pipedream/platform"; export default { type: "app", app: "platerecognizer", - propDefinitions: { - imageFileOrUrl: { - type: "string", - label: "Image File or URL", - description: "The image file or URL to be recognized.", - }, - regions: { - type: "string[]", - label: "Regions", - description: "Regions to select specific license plate patterns. Use comma-separated values for multiple regions (e.g., us-ca,us-ny).", - optional: true, - }, - cameraId: { - type: "string", - label: "Camera ID", - description: "The ID of the camera that took the image.", - optional: true, - }, - mmc: { - type: "boolean", - label: "MMC", - description: "Whether to detect vehicle make, model, and color.", - optional: true, - }, - config: { - type: "string", - label: "Config", - description: "Additional configuration in JSON format.", - optional: true, - }, - }, methods: { - authKeys() { - console.log(Object.keys(this.$auth)); - }, _baseUrl() { return "https://api.platerecognizer.com/v1"; }, - async triggerRecognition({ - imageFileOrUrl, regions, cameraId, mmc, config, + _headers(headers) { + return { + "Authorization": `Token ${this.$auth.api_token}`, + ...headers, + }; + }, + _makeRequest({ + $ = this, path, headers = {}, ...opts }) { - const formData = new FormData(); - if (imageFileOrUrl.startsWith("http")) { - formData.append("upload", imageFileOrUrl); - } else { - // Assuming the use of Node.js environment, fs should be used for local files - // And since the file needs to be read as a stream, ensure fs is properly imported - const fs = require("fs"); - formData.append("upload", fs.createReadStream(imageFileOrUrl)); - } - if (regions) formData.append("regions", regions.join(",")); - if (cameraId) formData.append("camera_id", cameraId); - if (mmc !== undefined) formData.append("mmc", mmc); - if (config) formData.append("config", config); - - return axios(this, { + return axios($, { + url: `${this._baseUrl()}${path}`, + headers: this._headers(headers), + ...opts, + }); + }, + runRecognition(opts = {}) { + return this._makeRequest({ method: "POST", - url: `${this._baseUrl()}/plate-reader/`, - headers: { - "Authorization": `Token ${this.$auth.api_key}`, - "Content-Type": "multipart/form-data", - }, - data: formData, - maxContentLength: Infinity, - maxBodyLength: Infinity, + path: "/plate-reader/", + ...opts, }); }, }, diff --git a/components/platerecognizer/sources/new-recognition-instant/new-recognition-instant.mjs b/components/platerecognizer/sources/new-recognition-instant/new-recognition-instant.mjs deleted file mode 100644 index dc1e853bc1c55..0000000000000 --- a/components/platerecognizer/sources/new-recognition-instant/new-recognition-instant.mjs +++ /dev/null @@ -1,56 +0,0 @@ -import { axios } from "@pipedream/platform"; -import platerecognizer from "../../platerecognizer.app.mjs"; - -export default { - key: "platerecognizer-new-recognition-instant", - name: "New Recognition Instant", - description: "Emits new event when a cloud webhook event is broadcasted.", - version: "0.0.{{ts}}", - type: "source", - dedupe: "unique", - props: { - platerecognizer, - db: "$.service.db", - http: { - type: "$.interface.http", - customResponse: true, - }, - }, - hooks: { - async deploy() { - console.log("Deploying new recognition instant source"); - }, - async activate() { - console.log("Activating new recognition instant source"); - }, - async deactivate() { - console.log("Deactivating new recognition instant source"); - }, - }, - async run(event) { - const body = event.body; - if (!body) { - this.http.respond({ - status: 400, - body: "No data received", - }); - return; - } - - this.http.respond({ - status: 200, - body: "Success", - }); - - const eventData = { - hook: body.hook, - data: body.data, - }; - - this.$emit(eventData, { - id: eventData.data.timestamp, - summary: `New Recognition: ${eventData.data.results.map((result) => result.plate).join(", ")}`, - ts: Date.parse(eventData.data.timestamp), - }); - }, -};