Skip to content

Commit

Permalink
enh(faces): Avoid O(n) API calls on faces overview
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed Jul 27, 2023
1 parent 95dd5ea commit 2170379
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 63 deletions.
36 changes: 3 additions & 33 deletions src/components/Faces/FaceCover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
{{ baseName }}
</h2>
</div>
<div v-if="facesFiles[baseName] && !small" class="face-cover__details__second-line">
{{ n('photos', '%n photos', '%n photos', facesFiles[baseName].length,) }}
<div v-if="!small" class="face-cover__details__second-line">
{{ n('photos', '%n photos', '%n photos', face.props['nbItems']) }}
</div>
</div>
</div>
Expand Down Expand Up @@ -107,37 +107,7 @@ export default {
if (!this.cover) return {}
return this.getCoverStyle(this.face.basename)
},
},
async mounted() {
this.waitForVisible(this.$el, (isVisible) => {
if (!this.facesFiles[this.face.basename]) {
this.fetchFiles()
}
})
},
beforeDestroy() {
this.observer.disconnect()
},
methods: {
async fetchFiles() {
await this.fetchFaceContent(this.face.basename)
},
waitForVisible(el, listener) {
this.observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.intersectionRatio > 0) {
listener()
observer.disconnect()
}
})
})
this.observer.observe(el)
},
},
}
}
</script>

Expand Down
34 changes: 4 additions & 30 deletions src/mixins/FaceCoverMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

import { mapGetters } from 'vuex'
import he from "he";

export default {
name: 'FaceCoverMixin',
Expand All @@ -35,32 +36,7 @@ export default {

methods: {
getFaceCover(faceName) {
// Give high scores for faces that intersect with the edge of the picture (with a margin of half the face size)
const scoreFacePosition = (faceDetection) => {
return Math.max(0, -1 * (faceDetection.x - faceDetection.width * 0.5))
+ Math.max(0, -1 * (faceDetection.y - faceDetection.height * 0.5))
+ Math.max(0, -1 * (1 - (faceDetection.x + faceDetection.width) - faceDetection.width * 0.5))
+ Math.max(0, -1 * (1 - (faceDetection.y + faceDetection.height) - faceDetection.height * 0.5))
}

return (this.facesFiles[faceName] || [])
.slice(0, 25)
.map(fileId => this.files[fileId])
// sort larges face first
.sort((a, b) =>
b.faceDetections.find(d => d.title === faceName).width
- a.faceDetections.find(d => d.title === faceName).width
)
// sort fewest face detections first
.sort((a, b) =>
a.faceDetections.length
- b.faceDetections.length
)
// Sort faces that are at the edge last
.sort((a, b) =>
scoreFacePosition(a.faceDetections.find(d => d.title === faceName))
- scoreFacePosition(b.faceDetections.find(d => d.title === faceName))
)[0]
return JSON.parse(he.decode(this.faces[faceName].props['face-preview-image'] || '{}'))
},

/**
Expand All @@ -72,12 +48,10 @@ export default {
*/
getCoverStyle(faceName) {
const cover = this.getFaceCover(faceName)
if (!cover) {
if (!cover || !cover.detection) {
return {}
}
const detections = cover.faceDetections

const detection = detections.find(detection => detection.title === faceName)
const detection = cover.detection

// Zoom into the picture so that the face fills the --photos-face-width box nicely
// if the face is larger than the image, we don't zoom out (reason for the Math.max)
Expand Down
1 change: 1 addition & 0 deletions src/services/DavRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const props = `
<d:getlastmodified />
<d:resourcetype />
<nc:face-detections />
<nc:face-preview-image />
<nc:file-metadata-size />
<nc:has-preview />
<nc:realpath />
Expand Down
4 changes: 4 additions & 0 deletions src/views/FaceContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ export default {
},
},
mounted() {
this.fetchFaceContent(this.faceName)
},
watch: {
face() {
if (this.face) {
Expand Down

0 comments on commit 2170379

Please sign in to comment.