File size: 5,113 Bytes
b5ba7a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<template>
    <a-collapse-panel>
        <template #header>
            <VisibleSwitch v-model:visible="object.visible" @update:visible="onVisibleChange" />
            <GroupSwitch v-model:grouped="object.grouped" />
            <LockSwitch v-model:locked="object.locked"/>
            <span :class="{ hidden: !object.visible }">{{ display_name }}</span>
            <fire-outlined @click.stop="unjamInvalidKeypoints" v-if="object.hasInvalidKeypoints()"
                title="Move all invalid keypoints to visible canvas for edit." class="unjam-button" />
            <close-outlined @click="removeObject" class="close-icon" />
        </template>
        <slot name="extra-control"></slot>
        <a-list size="small">
            <a-list-item v-for="keypoint in object.keypoints" :key="keypoint.id"
                :class="{ 'keypoint-selected': keypoint.selected }">
                <VisibleSwitch v-model:visible="keypoint._visible" @update:visible="onVisibleChange" />
                <span :class="{ hidden: !keypoint._visible }">{{ keypoint.name }}</span>
                <a-space class="coords-group">
                    <a-input-number :value="keypoint.x" @change="onKeypointXChange($event, keypoint)" addon-before="x"
                        :disabled="keypoint.selected_in_group" :precision="2" />
                    <a-input-number :value="keypoint.y" @change="onKeypointYChange($event, keypoint)" addon-before="y"
                        :disabled="keypoint.selected_in_group" :precision="2" />
                </a-space>
            </a-list-item>
        </a-list>
    </a-collapse-panel>
</template>

<script lang="ts">
import { OpenposeKeypoint2D, OpenposeObject } from '../Openpose';
import VisibleSwitch from './VisibleSwitch.vue';
import LockSwitch from './LockSwitch.vue';
import GroupSwitch from './GroupSwitch.vue';
import { CloseOutlined, FireOutlined } from '@ant-design/icons-vue';

const IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];

export default {
    props: {
        object: {
            type: OpenposeObject,
            required: true,
        },
        display_name: {
            type: String,
            required: true,
        },
    },
    watch: {
        'object.visible': {
            handler(newValue) {
                this.updateKeypointsVisibility(newValue);
            },
            immediate: true,
        },
    },
    methods: {
        updateKeypointsVisibility(visible: boolean) {
            if (this.object.grouped) {
                this.object.group!.set({
                    visible: visible
                });
            }
            let all_invisible = true;
            for (const keypoint of this.object.keypoints) {
                if (keypoint._visible) all_invisible = false;
                keypoint._visible &&= visible;
            }

            // Only when all keypoints invisible, we want to turn the them
            // all to visible.
            if (visible && all_invisible) {
                this.object.keypoints.forEach(p => p._visible = true);
            }
        },
        removeObject() {
            this.$emit("removeObject", this.object);
        },
        onVisibleChange(visible: boolean) {
            this.$nextTick(() => {
                // Now, call renderAll() after the visibility updates are done
                this.object.canvas?.renderAll();
            });
        },
        onCoordsChange(keypoint: OpenposeKeypoint2D) {
            keypoint.updateConnections(IDENTITY_MATRIX);
            keypoint.setCoords();
            keypoint.canvas?.renderAll();
        },
        onKeypointXChange(x: number, keypoint: OpenposeKeypoint2D) {
            keypoint.x = x;
            this.onCoordsChange(keypoint);
        },
        onKeypointYChange(y: number, keypoint: OpenposeKeypoint2D) {
            keypoint.y = y;
            this.onCoordsChange(keypoint);
        },
        /**
         * Move all invalid keypoints to visible canvas for edit.
         */
        unjamInvalidKeypoints() {
            const canvasWidth = this.object.canvas!.width!;
            const canvasHeight = this.object.canvas!.height!;

            this.object.grouped = false;

            let xOffset = 10;
            let yOffset = 10;
            const OFFSET_STEP = Math.min(canvasHeight, canvasWidth) / 10;

            console.log(this.object.invalidKeypoints());

            this.object.invalidKeypoints().forEach((keypoint) => {
                keypoint.x = xOffset;
                keypoint.y = yOffset;

                // Increase the offset for the next invalid point
                if (xOffset + OFFSET_STEP > canvasWidth) {
                    yOffset += OFFSET_STEP;
                    xOffset = 0;
                } else {
                    xOffset += OFFSET_STEP;
                }

                keypoint._visible = true;
                this.onCoordsChange(keypoint);
            });

            this.object.canvas?.renderAll();
        }
    },
    components: {
        VisibleSwitch,
        GroupSwitch,
        LockSwitch,
        CloseOutlined,
        FireOutlined,
    }
};
</script>