From 39a6c25934908ba1b140330ebcad780e5d682f17 Mon Sep 17 00:00:00 2001 From: Luan Cazarine Date: Tue, 14 May 2024 13:46:54 -0300 Subject: [PATCH] platerecognizer init --- .../run-recognition/run-recognition.mjs | 46 +++++++++++++ components/platerecognizer/package.json | 2 +- .../platerecognizer/platerecognizer.app.mjs | 69 ++++++++++++++++++- .../new-recognition-instant.mjs | 56 +++++++++++++++ 4 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 components/platerecognizer/actions/run-recognition/run-recognition.mjs create mode 100644 components/platerecognizer/sources/new-recognition-instant/new-recognition-instant.mjs diff --git a/components/platerecognizer/actions/run-recognition/run-recognition.mjs b/components/platerecognizer/actions/run-recognition/run-recognition.mjs new file mode 100644 index 0000000000000..54f0ddbb2da61 --- /dev/null +++ b/components/platerecognizer/actions/run-recognition/run-recognition.mjs @@ -0,0 +1,46 @@ +import platerecognizer from "../../platerecognizer.app.mjs"; +import { axios } from "@pipedream/platform"; + +export default { + key: "platerecognizer-run-recognition", + name: "Run Recognition", + description: "Triggers a recognition process using the Plate Recognizer SDK.", + version: "0.0.1", + type: "action", + props: { + platerecognizer, + imageFileOrUrl: platerecognizer.propDefinitions.imageFileOrUrl, + regions: platerecognizer.propDefinitions.regions, + cameraId: platerecognizer.propDefinitions.cameraId, + mmc: platerecognizer.propDefinitions.mmc, + config: platerecognizer.propDefinitions.config, + }, + async run({ $ }) { + const formData = new FormData(); + if (this.imageFileOrUrl.startsWith("http")) { + formData.append("upload", this.imageFileOrUrl); + } else { + const fs = require("fs"); + formData.append("upload", fs.createReadStream(this.imageFileOrUrl)); + } + 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", + }, + data: formData, + maxContentLength: Infinity, + maxBodyLength: Infinity, + }); + + $.export("$summary", "Recognition process triggered successfully"); + return response; + }, +}; diff --git a/components/platerecognizer/package.json b/components/platerecognizer/package.json index 61e75bdaccd64..137711b33dcfb 100644 --- a/components/platerecognizer/package.json +++ b/components/platerecognizer/package.json @@ -12,4 +12,4 @@ "publishConfig": { "access": "public" } -} \ No newline at end of file +} diff --git a/components/platerecognizer/platerecognizer.app.mjs b/components/platerecognizer/platerecognizer.app.mjs index eb436c25ff5aa..55739ef9964f1 100644 --- a/components/platerecognizer/platerecognizer.app.mjs +++ b/components/platerecognizer/platerecognizer.app.mjs @@ -1,11 +1,74 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "platerecognizer", - propDefinitions: {}, + 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: { - // this.$auth contains connected account data authKeys() { console.log(Object.keys(this.$auth)); }, + _baseUrl() { + return "https://api.platerecognizer.com/v1"; + }, + async triggerRecognition({ + imageFileOrUrl, regions, cameraId, mmc, config, + }) { + 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, { + 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, + }); + }, }, -}; \ No newline at end of file +}; diff --git a/components/platerecognizer/sources/new-recognition-instant/new-recognition-instant.mjs b/components/platerecognizer/sources/new-recognition-instant/new-recognition-instant.mjs new file mode 100644 index 0000000000000..dc1e853bc1c55 --- /dev/null +++ b/components/platerecognizer/sources/new-recognition-instant/new-recognition-instant.mjs @@ -0,0 +1,56 @@ +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), + }); + }, +};