Skip to content

Commit

Permalink
Merge pull request #475 from photonia-io/labels
Browse files Browse the repository at this point in the history
Labels association for Photos
  • Loading branch information
janosrusiczki authored Apr 4, 2023
2 parents 13ae863 + 81ae706 commit 6d454ae
Show file tree
Hide file tree
Showing 23 changed files with 429 additions and 171 deletions.
2 changes: 1 addition & 1 deletion app/graphql/graphql_query_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class GraphqlQueryCollection
id
name
}
labelInstances {
labels {
id
name: sequencedName
confidence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module Types
# GraphQL Label Instance Type
class LabelInstanceType < Types::BaseObject
description 'A label instance'
class LabelType < Types::BaseObject
description 'A label'

field :bounding_box, BoundingBoxType, 'Bounding box of the label instance', null: false
field :confidence, Float, 'Confidence of the label instance', null: false
Expand Down
6 changes: 3 additions & 3 deletions app/graphql/types/photo_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PhotoType < Types::BaseObject
field :id, String, 'ID of the photo', null: false
field :imported_at, GraphQL::Types::ISO8601DateTime, 'Datetime the photo was imported', null: true
field :intelligent_thumbnail, IntelligentThumbnailType, 'Intelligent thumbnail', null: true
field :label_instances, [LabelInstanceType], 'Label instances', null: true
field :labels, [LabelType], 'Labels', null: true
field :license, String, 'License type of the photo', null: true
field :machine_tags, [TagType], 'Machine (Rekognition) tags', null: true
field :name, String, 'Title of the photo', null: false
Expand All @@ -33,8 +33,8 @@ def description
@object.description || ''
end

def label_instances
@object.label_instance_collection&.add_sequenced_names.label_instances
def labels
@object.labels.load.add_sequenced_names
end

def user_tags
Expand Down
2 changes: 0 additions & 2 deletions app/javascript/entrypoints/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ document.addEventListener('DOMContentLoaded', () => {
})
}

console.log(import.meta.env.MODE)

// go for Vue!

const app = createApp({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
<div
class="label"
:style="'\
top: ' + labelInstance.boundingBox.top * 100 + '%; \
left: ' + labelInstance.boundingBox.left * 100 + '%; \
width: ' + labelInstance.boundingBox.width * 100 + '%; \
height: ' + labelInstance.boundingBox.height * 100 + '%;'
top: ' + label.boundingBox.top * 100 + '%; \
left: ' + label.boundingBox.left * 100 + '%; \
width: ' + label.boundingBox.width * 100 + '%; \
height: ' + label.boundingBox.height * 100 + '%;'
"
>
<p>{{ labelInstance.name }}</p>
<p>{{ label.name }}</p>
</div>
</template>

<script setup>
import { ref, toRef, watch } from 'vue'
const props = defineProps({
labelInstance: {
label: {
type: Object,
required: true
},
Expand Down
14 changes: 7 additions & 7 deletions app/javascript/photos/display.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
>
<img :src="photo.largeImageUrl" />
<div
v-if="photo.labelInstances"
v-if="photo.labels"
class="labels"
>
<DisplayLabelInstance
v-for="labelInstance in photo.labelInstances"
:labelInstance="labelInstance"
:highlighted="labelHighlights[labelInstance.id]"
:key="labelInstance.id"
<DisplayLabel
v-for="label in photo.labels"
:label="label"
:highlighted="labelHighlights[label.id]"
:key="label.id"
/>
<div v-if="photo.intelligentThumbnail">
<div :style="' \
Expand All @@ -39,7 +39,7 @@
</template>

<script setup>
import DisplayLabelInstance from './display-label-instance.vue'
import DisplayLabel from './display-label.vue'
const props = defineProps({
photo: {
Expand Down
16 changes: 8 additions & 8 deletions app/javascript/photos/show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,20 @@
</div>

<SidebarHeader
v-if="photo.labelInstances?.length > 0"
v-if="photo.labels?.length > 0"
icon="far fa-square"
title="Labels"
/>
<div
v-if="photo.labelInstances?.length > 0"
v-if="photo.labels?.length > 0"
class="tags"
>
<SidebarLabelInstance
v-for="labelInstance in photo.labelInstances"
<SidebarLabel
v-for="label in photo.labels"
@highlight-label="highlightLabel"
@un-highlight-label="unHighlightLabel"
:labelInstance="labelInstance"
:key="labelInstance.id"
:label="label"
:key="label.id"
/>
</div>

Expand Down Expand Up @@ -203,7 +203,7 @@
import SmallNavigationButton from '@/photos/small-navigation-button.vue'
import Display from './display.vue'
import SidebarHeader from './sidebar-header.vue'
import SidebarLabelInstance from '@/photos/sidebar-label-instance.vue'
import SidebarLabel from '@/photos/sidebar-label.vue'
import Tag from '@/tags/tag.vue'
import Empty from '@/empty.vue'
Expand All @@ -219,7 +219,7 @@
albums: [],
tags: [],
rekognitionTags: [],
labelInstances: null,
labels: null,
intelligentThumbnail: null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
@mouseover="hovered(true)"
@mouseout="hovered(false)"
>
<span class="icon"><i class="far fa-square"></i></span>{{ labelInstance.name }} ({{ Math.ceil(labelInstance.confidence) }}%)
<span class="icon"><i class="far fa-square"></i></span>{{ label.name }} ({{ Math.ceil(label.confidence) }}%)
</div>
</template>

<script setup>
const props = defineProps({
labelInstance: {
label: {
type: Object,
required: true
}
Expand All @@ -20,9 +20,9 @@
const hovered = (state) => {
if (state) {
emit('highlightLabel', props.labelInstance)
emit('highlightLabel', props.label)
} else {
emit('unHighlightLabel', props.labelInstance)
emit('unHighlightLabel', props.label)
}
}
</script>
Expand Down
1 change: 1 addition & 0 deletions app/jobs/rekognition_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class RekognitionJob < ApplicationJob
def perform(photo_id)
photo = Photo.find(photo_id)
RekognitionTagger.new.tag(photo)
PhotoLabeler.new(photo.reload).add_labels_from_rekognition_response
AddIntelligentDerivativesJob.perform_later(photo_id)
end
end
63 changes: 63 additions & 0 deletions app/models/label.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: labels
#
# id :bigint not null, primary key
# confidence :float
# height :float
# left :float
# name :string
# top :float
# width :float
# created_at :datetime not null
# updated_at :datetime not null
# photo_id :bigint not null
#
# Indexes
#
# index_labels_on_photo_id (photo_id)
#
# Foreign Keys
#
# fk_rails_... (photo_id => photos.id)
#
class Label < ApplicationRecord
belongs_to :photo

attr_accessor :sequenced_name

def center
Photo::Point.new(
top + (height / 2),
left + (width / 2)
)
end

def area
100 * (top + height) * (left + width)
end

def person?
name == 'Person'
end

def bounding_box
Photo::BoundingBox.new(top: top, left: left, width: width, height: height)
end

# class methods

def self.find_or_create_from_rekognition_label_instance(photo, label_name, label_instance)
Label.find_or_create_by(
photo:,
name: label_name,
confidence: label_instance['confidence'],
top: label_instance['bounding_box']['top'],
left: label_instance['bounding_box']['left'],
width: label_instance['bounding_box']['width'],
height: label_instance['bounding_box']['height']
)
end
end
35 changes: 0 additions & 35 deletions app/models/label/instance.rb

This file was deleted.

76 changes: 0 additions & 76 deletions app/models/label/instance/collection.rb

This file was deleted.

Loading

0 comments on commit 6d454ae

Please sign in to comment.