Skip to content

Instantly share code, notes, and snippets.

@jakewarrenblack
Created May 9, 2023 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakewarrenblack/f5e032e1fe08b162d1c7763234d00081 to your computer and use it in GitHub Desktop.
Save jakewarrenblack/f5e032e1fe08b162d1c7763234d00081 to your computer and use it in GitHub Desktop.
const uploadImage = async (filepath, projectUrl, apiKey, extraOptions) => {
const filename = path.basename(filepath);
// console.log(filename, split)
const formData = new FormData();
formData.append("name", filename);
formData.append("file", fs.createReadStream(filepath));
formData.append("split", extraOptions.split);
try {
const response = await axios({
method: "POST",
url: `https://api.roboflow.com/dataset/${projectUrl}/upload`, // maybe if I pass ?overwrite=true for this, it's breaking annotation?
params: {
api_key: process.env.ROBOFLOW_API_KEY,
},
data: formData,
headers: formData.getHeaders(),
});
console.log(response.data);
return response.data;
} catch (e) {
if (e.response) {
return e.response.data;
}
throw e;
}
};
// throws a 404 error when we try to annotate an image that already exists, it seems
const uploadAnnotation = async (imageID, annotationFile, projectUrl) => {
return new Promise(async (resolve, reject) => {
// gets the actual file name, given a full path
const filename = path.basename(annotationFile);
// reading the contents of the XML file
const annotationData = fs.readFileSync(annotationFile, "utf-8");
await axios({
method: "POST",
url: `https://api.roboflow.com/dataset/${projectUrl.toString()}/annotate/${imageID}?overwrite=true`,
params: {
api_key: process.env.ROBOFLOW_API_KEY,
name: filename,
},
data: annotationData,
headers: {
"Content-Type": "text/plain",
},
})
.then((res) => {
resolve(res.data);
})
.catch((e) => {
console.log("Error uploading annotation: ", e.response.data.error);
reject(e.response.data.error);
});
});
};
const uploadWithAnnotation = async (
fileName,
annotationFilename,
projectUrl,
apiKey,
extraOptions
) => {
const uploadPromise = uploadImage(fileName, projectUrl, apiKey, extraOptions);
// uploadAnnotation requires imageId from uploadImage, so I have to wait for that to complete first
const annotationPromise = await uploadPromise.then(async (uploadResult) => {
const imageId = uploadResult.id;
if (annotationFilename.includes("[filename]")) {
annotationFilename = annotationFilename.replace(
"[filename]",
path.parse(fileName).name
);
}
if (fs.existsSync(annotationFilename)) {
return await uploadAnnotation(
imageId,
annotationFilename,
projectUrl,
apiKey
)
.then((annotationResult) => {
return { uploadResult, annotationResult };
})
.catch((e) => {
console.log(e);
});
}
});
// resolve if both uploadImage and uploadAnnotation succeed, but reject if either fail
return Promise.all([uploadPromise, annotationPromise])
.then(([uploadResult, { annotationResult } = {}]) => {
return { uploadResult, annotationResult };
})
.catch((error) => {
console.error(` Error uploading ${fileName}: `, error);
throw error;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment