Skip to content

Commit

Permalink
[Components] platerecognizer #11727
Browse files Browse the repository at this point in the history
Actions
 - Run Recognition
  • Loading branch information
luancazarine committed May 14, 2024
1 parent 39a6c25 commit 06dc8c6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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");
Expand Down
6 changes: 6 additions & 0 deletions components/platerecognizer/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const checkTmp = (filename) => {
if (!filename.startsWith("/tmp")) {
return `/tmp/${filename}`;
}
return filename;
};
6 changes: 5 additions & 1 deletion components/platerecognizer/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -11,5 +11,9 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^1.6.5"
}
}

77 changes: 18 additions & 59 deletions components/platerecognizer/platerecognizer.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
},
},
Expand Down

This file was deleted.

0 comments on commit 06dc8c6

Please sign in to comment.