Skip to content

Commit

Permalink
update sequences schema for test passing, start on making sequences c…
Browse files Browse the repository at this point in the history
…ode its own class

ref #10
  • Loading branch information
maxgrossman committed Aug 6, 2018
1 parent f542572 commit 4ef4feb
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 17 deletions.
114 changes: 114 additions & 0 deletions adapters/sequence/sequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
'use strict';

Promise = require('bluebird')

const readdir = require('fs-extra').readdir;
const path = require('path');
const ExifTool = require('exiftool-vendored');
const exif = new ExifTool();
const uuidv4 = require('uuid/v4');
const dayjs = require('dayjs');

class Sequence {
constructor () {}

build (source, type, minCutDist, maxCutDist, maxDelta, seqSize, userId) {
return fromSource(source, type).then(images => {
const params = {
maxDist: maxCutDist,
minDist: minCutDist,
maxDelta: maxDelta,
size: seqSize
}
return this.cut(flatten(images), params).then(sequences => {
return sequences.map(sequence => {
sequence.userId = userId;
return sequence;
})
.catch((err) => { throw err; });
})
.catch((err) => { throw err; })
})
.catch((err) => { throw err; })
}

cut(images, params) {
return Promise
.map(images, (image) => meta(image))
.then(metas => split(metas, params))
}

fromSource(source, type) {
if (type === 'directory') {
return Promise.map(source, (dir) => {
return readdir(dir)
.then(files => files.map(f => path.join(p, f)))
})
}
}

makeDate() {
return dayjs(tags.GPSDateTime.toString());
}

makeLoc(tags) {
return {
lon: Number(tags.GPSLongitude),
lat: Number(tags.GPSLatitude)
}
}

meta(image) {
return exif.read(image).then(tags => {
return {
image: image,
loc: makeLoc(tags),
timestamp: makeDate(tags),
id: uuidv4()
}
})
.catch(err => { throw err; });
}

split(metas, params) {
const sortedMetas = metas.sort((a, b) => a.timestamp - b.timestamp);
const pelIndex = sortedMetas.length - 2;
const sequences = [];
const maxDist = params.maxDist;
const maxDelta = params.maxDelta;
const maxSize = params.maxSize;
const minDist = params.minDist;
let currentSequence = [];

sortedMetas.slice(0 , pelIndex).forEach((meta, i) => {
const partnerMeta = sortedMetas[i + 1],
distance = calcDistance(meta.loc, partnerMeta.loc),
tooClose = distance < minDist;

// ... if image is not too close to its partner, add it to a sequence.
if (!tooClose) {
// ... if the current sequence length matches the maximum size,
// or images are too far apart (in space or time),
// add the current sequence to the sequence map, then make a new sequence.
const delta = calcDelta(meta.timestamp, partnerMeta.timestamp),
needNewSequence = currentSequence.length === maxSize || distance > maxDist || delta > maxDelta;

if (needNewSequence) {
addSequence(sequences, currentSequence);
currentSequence = [];

}

// add a uuid then add it to the sequence!
meta.id = uuidv4();
currentSequence.push(meta);

}
})

if (currentSequence.length > 0) addSequence(sequences, currentSequence);
return sequences;
}
}

module.exports = Sequence;
11 changes: 6 additions & 5 deletions schema/sequences.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const Joi = require('joi');
const metadata = require('./metadata');

module.exports = Joi
.object()
.pattern(
// uuid regex;
/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
Joi.array().items(metadata)
.array()
.items(
Joi.object().keys({
sequenceId: Joi.string().regex(/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i),
sequence: Joi.array().items(metadata)
})
);
17 changes: 9 additions & 8 deletions test/sequence/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,27 @@ Promise = require('bluebird');
describe('sequence', () => {
it('meta reads then selializes an image\'s exif metadata', async () => {
try {
const image = './testData/exif-gps-samples/DSCN0010.JPG',
metadata = await meta(image),
validation = Joi.validate(metadata, metadataSchema);
const image = './testData/exif-gps-samples/DSCN0010.JPG';
const metadata = await meta(image)
const validation = Joi.validate(metadata, metadataSchema);

expect(validation.value).to.be.eql(metadata);
expect(validation.error).to.be.null;

return;
} catch (e) {
console.error(e);

}
})
it ('given a path of images, generates a list of sequence objects', async () => {
try {
const paths = ['/testData/exif-gps-samples', '/testData/danbjoseph'].map(p => process.cwd() + p),
sequences = await sequenceAdapter(paths),
validation = Joi.validate(sequences, sequencesSchema);
const paths = ['/testData/exif-gps-samples', '/testData/danbjoseph'].map(p => process.cwd() + p);
const sequences = await sequenceAdapter(paths);
const validation = Joi.validate(sequences, sequencesSchema);

expect(validation.value).to.be.eql(sequences)
expect(validation.error).to.be.null;

return;
} catch (e) {
console.error(e);

Expand Down
8 changes: 4 additions & 4 deletions test/sequence/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const uuidv4 = require('uuid/v4');

const server = require('../server');
const mergeDefaults = require('../helpers').mergeDefaults;
const oldUserPayload = require('../../testData/payloads').postUser;
const users = require('../../db/seeds/users');
const danbjoseph = users[0];
const routes = [
// require('../../routes/sequence').get,
require('../../routes/sequence').post
Expand All @@ -20,11 +21,10 @@ before(async () => await server.liftOff(routes))
describe('post', () => {
it('replies 200 when sequence post is successful', async () => {
try {
const userId = (await db('Users').select('id').where({ name: 'danbjoseph' }))[0].id,
request = mergeDefaults({
const request = mergeDefaults({
method: 'POST',
payload: ['/testData/danbjoseph'].map(p => process.cwd() + p),
url: `/sequence?userId=${userId}`
url: `/sequence?userId=${danbjoseph.id}`
}),
r = await server.inject(request),
statusCode = r.statusCode;
Expand Down

0 comments on commit 4ef4feb

Please sign in to comment.