Created
November 25, 2020 02:39
-
-
Save brianjychan/fe1f69be69246187b3e168a7919234af to your computer and use it in GitHub Desktop.
motion detection w Jimp, detection w google vision API
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
import vision from '@google-cloud/vision' | |
import Jimp from 'jimp'; | |
import { unlinkSync } from 'fs'; | |
const PHOTOS_DIR = './photos' | |
const MOTION_PERCENT_THRESHOLD = .005 | |
// Maybe put this in some timed loop as imagse come in | |
// Camera Data | |
let currPhotoBuffer = {} as Buffer // your photo in a node buffer | |
let currPhotoJimp = {} as Jimp // can hold a Jimp object between iterations of the loop | |
const checkMotionDetection: () => Promise<boolean | null> = async () => { | |
/*----------- Take Photo and Motion Detection ---------*/ | |
const previousPhotoJimp = currPhotoJimp | |
const photoMs = Date.now() | |
const photoJpgFilename = photoMs + '.jpg' | |
try { | |
// Read with Jimp | |
currPhotoJimp = await Jimp.read(currPhotoBuffer) | |
// Remove photo | |
unlinkSync(PHOTOS_DIR + photoJpgFilename) | |
} catch (e) { | |
console.error(e) | |
} | |
// Calc motion | |
const motionPercent = Jimp.diff(currPhotoJimp, previousPhotoJimp, .2).percent | |
const hasNewMotion = motionPercent > MOTION_PERCENT_THRESHOLD | |
if (hasNewMotion) { | |
return true | |
} | |
return false | |
} | |
const analyzePhoto: () => Promise<void> = async () => { | |
const visionClient = new vision.ImageAnnotatorClient() | |
/*----------- Object Detection ---------*/ | |
try { | |
const request = { | |
image: { | |
content: currPhotoBuffer | |
} | |
} | |
if (!visionClient.objectLocalization) { // appease the TS linter | |
throw Error('This should not be thrown') | |
} | |
const [result] = await visionClient.objectLocalization(request) | |
if (!result.localizedObjectAnnotations) { | |
throw Error('Did not receive results') | |
} | |
const detections = result.localizedObjectAnnotations | |
const detectedTagString = detections.map(detection => detection.name + ': ' + detection.score).join(', ') | |
console.log('analyzed pic. Detected: ' + detectedTagString) | |
} catch (e) { | |
console.error(e) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment