Created
June 15, 2024 12:24
-
-
Save boly38/08bcbd87119c7f5f77749303e88cd41e to your computer and use it in GitHub Desktop.
ReST API to detect X JPG files of bird using bird v2 public dataset from roboflow.com - from study : https://github.com/boly38/botEnSky/issues/57
This file contains 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
// from: https://github.com/boly38/botEnSky/issues/57 | |
// ---------------------------- | |
// ReST API to detect bird - from blog : https://blog.roboflow.com/bird-detection-api/ | |
// *** this file is doing identification of a target folder containing 1..N files having "JPG" as extension | |
import axios from 'axios'; | |
import fs from 'fs'; | |
import path from 'path'; | |
import {fileURLToPath} from 'url'; | |
const __filename = fileURLToPath(import.meta.url); | |
const __dirname = path.dirname(__filename); | |
// API_KEY - REQUIRED - export ROBOFLOW_API_KEY=xxxx | |
// https://app.roboflow.com / use public plan free and get your api key under profile | |
const api_key = process.env["ROBOFLOW_API_KEY"] || "unauthorized"; | |
const url = "https://detect.roboflow.com/bird-v2/2"; | |
const processImages = async (directory) => { | |
try { | |
const files = fs.readdirSync(directory); | |
const jpgFiles = files.filter(file => path.extname(file).toLowerCase() === '.jpg'); | |
for (const jpgFile of jpgFiles) { | |
const imagePath = path.join(directory, jpgFile); | |
const txtFile = path.join(directory, path.basename(jpgFile, '.jpg') + '.txt'); | |
const image = fs.readFileSync(imagePath, {encoding: "base64"}); | |
const response = await axios({ | |
method: "POST", | |
url, | |
params: {api_key}, | |
data: image, | |
headers: { | |
"Content-Type": "application/x-www-form-urlencoded" | |
} | |
}).catch(err => { | |
console.error(`Error processing ${jpgFile}: ${err.message}`); | |
return null; | |
}); | |
if (response) { | |
fs.writeFileSync(txtFile, JSON.stringify(response.data, null, 2)); | |
console.log(`Processed ${jpgFile}, result written to ${txtFile}`); | |
} | |
} | |
} catch (error) { | |
console.error(`Error reading directory: ${error.message}`); | |
} | |
}; | |
const directory = process.argv[2] || path.join(__dirname, 'input'); | |
processImages(directory); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment