Skip to content

Commit

Permalink
wip mergeConsecutiveNullCrops
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlevy0 committed Dec 2, 2023
1 parent 9423790 commit 5814c81
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion amplify/backend/function/S3Trigger52a3ac74/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const conf = {
width: 1920,
height: 1080,
highConfidenceThreshold: 90.0,
paddingFactorBase: 0.8,
paddingFactorBase: 0.3, // de 0.1 à 0.3 ?
significantMovementThreshold: 0.8,
jobCheckDelay: 5000,
minShotDuration: 0.5,
Expand Down
2 changes: 1 addition & 1 deletion amplify/backend/function/S3Trigger52a3ac74/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const handler: Handler = async (event: LambdaS3Event) => {
const { tmpPath, outputPath, newKey, key, bucket } = await getData(event)
await download(bucket, key, tmpPath)
const shots = await analyzeVideo(key, bucket)
console.log({ shots })
// console.log({ shots })
await processVideo(tmpPath, outputPath, shots)
await upload(outputPath, bucket, newKey)
await cleanTempFiles(tmpPath, outputPath)
Expand Down
50 changes: 31 additions & 19 deletions amplify/backend/function/S3Trigger52a3ac74/lib/rekognition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,31 +157,35 @@ function isCropChangeSignificant(
// Comparing the distance with the tolerance (adjusted for the size of the last crop)
const adjustedTolerance = Math.sqrt((tolerance * lastCrop.w) ** 2 + (tolerance * lastCrop.h) ** 2)

console.log(`isCropChangeSignificant : ${distance > adjustedTolerance}`)
console.log({ distance }, { adjustedTolerance })

return distance > adjustedTolerance
}

export function mergeConsecutiveNullCrops(shots) {
return shots.reduce((acc, shot) => {
if (shot.crop === null) {
if (acc.length > 0 && acc[acc.length - 1].crop === null) {
acc[acc.length - 1].ts_end = shot.ts_end
} else {
acc.push({ ...shot })
}
} else {
acc.push(shot)
}
return acc
}, [])
}

function createOrUpdateShots(shots, timestamp, crop, label, minShotDuration) {
if (shots.length > 0) {
let lastShot = shots[shots.length - 1]

// Fusionner si le label est le même et que les deux crops sont null
if (lastShot.label === label && lastShot.crop === null && crop === null) {
lastShot.ts_end = Math.max(lastShot.ts_end, timestamp)
} else if (lastShot.label !== label || lastShot.crop !== null || crop !== null) {
// Créer un nouveau shot si le label est différent ou si l'un des crops n'est pas null
const newTsStart = Math.max(lastShot.ts_end, timestamp - minShotDuration)
shots.push({
ts_start: newTsStart,
ts_end: timestamp,
crop,
label,
})
}
const newTsStart = Math.max(lastShot.ts_end, timestamp - minShotDuration)
shots.push({
ts_start: newTsStart,
ts_end: timestamp,
crop,
label,
})
} else {
// Créer le premier shot
shots.push({
ts_start: 0,
ts_end: timestamp,
Expand Down Expand Up @@ -260,12 +264,20 @@ export async function analyzeVideo(
lastFacePosition = face ? BoundingBox : lastFacePosition
prevEmotions = face ? Emotions : prevEmotions
})

// Assurer que le dernier shot a une durée minimale de minShotDuration
if (shots.length > 0) {
let lastShot = shots[shots.length - 1]
if (lastShot.ts_end - lastShot.ts_start < minShotDuration) {
lastShot.ts_end = lastShot.ts_start + minShotDuration
}
}
return shots.filter(s => s.ts_end !== s.ts_start)
const rawShots = shots
console.log('rawShots ->', rawShots.length)
const filteredShots = rawShots.filter(s => s.ts_end !== s.ts_start)
console.log('filteredShots ->', filteredShots.length)
const mergedShots = mergeConsecutiveNullCrops(filteredShots)
console.log('mergedShots ->', mergedShots.length)
console.log({ mergedShots })
return mergedShots
}

0 comments on commit 5814c81

Please sign in to comment.