Skip to content

Instantly share code, notes, and snippets.

@jauntybrain
Created August 24, 2023 01:30
Show Gist options
  • Save jauntybrain/ca68f50e377abfd89860c76175a506ad to your computer and use it in GitHub Desktop.
Save jauntybrain/ca68f50e377abfd89860c76175a506ad to your computer and use it in GitHub Desktop.
Firestore Trigger on object detection
const CLOUD_FUNCTIONS_REGION = 'us-central1'; // Your functions region here
const COLLECTION_NAME = 'detectedObjects'; // Your specified collection here
export const processDetectedObjects = functions
.region(CLOUD_FUNCTIONS_REGION)
.firestore.document(`${COLLECTION_NAME}/{documentID}`)
.onCreate(async (snapshot) => {
try {
const objectsData = snapshot.data();
// Return an error if no objects were detected
if (!objectsData.objects || objectsData.objects.length === 0) {
throw new functions.https.HttpsError(
'unavailable',
'No objects detected'
);
}
// Get userID from the image path.
// NOTE: This is an example for gs://[project-id]/users/[userID]/image.jpg
const userID = objectsData.file.split('gs://')[1].split('/')[2];
// Process objects - BASIC information mode
const objectNames = objectsData.objects;
// Process objects - FULL information mode
const objectNamesWithPredictions = objectsData.objects.map(
(object) => `${object.name}: ${object.score}`
);
// Your code here...
} catch (error) {
throw new functions.https.HttpsError(
'internal',
'Error processing detected objects'
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment