aijinhui
2023-10-24 8a87e4226aa802d6a0e3566c66824fedf68e77da
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
 
window.runDetect = (model, video, canvas) => {
 
    console.time('runDetect');
    // detect objects in the image.
    model.detect(video).then(predictions => {
        console.timeEnd('runDetect');
        console.log('Predictions: ', predictions);
 
        const context = canvas.getContext('2d');
        context.clearRect(0, 0, canvas.width, canvas.height);
        context.font = '10px Arial';
 
        console.log('number of detections: ', predictions.length);
        for (let i = 0; i < predictions.length; i++) {
            context.beginPath();
            context.rect(...predictions[i].bbox);
            context.lineWidth = 2;
            context.strokeStyle = 'green';
            context.fillStyle = 'green';
            context.stroke();
            context.fillText(
                predictions[i].score.toFixed(3) + ' ' + predictions[i].class, predictions[i].bbox[0],
                predictions[i].bbox[1] > 10 ? predictions[i].bbox[1] - 5 : 10);
        }
 
        window.setTimeout(() => { 
            if (model.run) {
                model.run(model, video, canvas); 
             }
        }, 120);
    });
}
 
window.runPosenet = (model, video, canvas) => {
 
    console.time('runPosenet');
    model.estimatePoses(video, { decodingMethod: 'multi-person', maxDetections: 5 }).then(poses => {
        console.timeEnd('runPosenet');
        console.log('Predictions: ', poses);
 
        const ctx = canvas.getContext('2d');
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.font = '10px Arial';
 
        poses.forEach(({ score, keypoints }) => {
            console.log('keypoints: ', keypoints);
            for (let i = 0; i < keypoints.length; i++) {
                const keypoint = keypoints[i];
                if (keypoint.score >= 0.5) {
                    const { y, x } = keypoint.position;
                    console.log(keypoint.part, ' : ', x, "x", y);
                    ctx.beginPath();
                    ctx.arc(x, y, 3, 0, 2 * Math.PI);
                    ctx.fillStyle = 'red';
                    ctx.fill();
                }
            }
            const adjacentKeyPoints = posenet.getAdjacentKeyPoints(keypoints, 0.5);
            console.log('adjacentKeyPoints: ', adjacentKeyPoints);
 
            adjacentKeyPoints.forEach((keypoints) => {
                const org = keypoints[0].position;
                const dest = keypoints[1].position;
                ctx.beginPath();
                ctx.moveTo(org.x,org.y);
                ctx.lineTo(dest.x,dest.y);
                ctx.lineWidth = 2;
                ctx.strokeStyle = 'green';
                ctx.stroke();
            });
 
        });
 
        window.setTimeout(() => { 
            if (model.run) {
                model.run(model, video, canvas); 
             }
        }, 120);
    });
}
 
window.runDeeplab = (model, video, canvas) => {
 
    console.time('runDeeplab');
    model.segment(video).then(deeplabOutput => {
        console.timeEnd('runDeeplab');
        console.log('deeplabOutput: ', deeplabOutput);
        const { legend, height, width, segmentationMap } = deeplabOutput;
 
        const scalecanvas = document.createElement('canvas')
        scalecanvas.width = width;
        scalecanvas.height = height;
        const segmentationMapData = new ImageData(segmentationMap, width, height)
        var imageData = segmentationMapData.data;
        for (var i = 0; i < imageData.length; i += 4) {
            if ((imageData[i] == 0) && (imageData[i + 1] == 0) && (imageData[i + 2] == 0) && (imageData[i + 3] == 255)) {
                imageData[i + 3] = 0;
            } else {
                imageData[i + 3] = 200;
            }
        }
        scalecanvas.getContext('2d').putImageData(segmentationMapData, 0, 0);
 
        const ctx = canvas.getContext('2d');
        ctx.font = '16px Arial';
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(scalecanvas, 0, 0, width, height, 0, 0, canvas.width, canvas.height);
 
        let cnt = 0
        Object.keys(legend).forEach((label) => {
            const [red, green, blue] = legend[label];
 
            ctx.fillStyle = `rgb(${red}, ${green}, ${blue})`;
            ctx.fillRect(0, cnt * 16 + 16, 32, 16);
            ctx.fillText(label, 40, cnt * 16 + 32);
            cnt++
        });
 
        window.setTimeout(() => { 
            if (model.run) {
                model.run(model, video, canvas); 
             }
        }, 120);
    });
}
 
 
window.runbodyPix = (model, video, canvas) => {
 
    console.time('runbodyPix');
    model.segmentMultiPersonParts(video).then(multiPersonPartSegmentation => {
        console.timeEnd('runbodyPix');
 
        console.log('multiPersonPartSegmentation: ', multiPersonPartSegmentation);
        const coloredPartImageData = bodyPix.toColoredPartMask(multiPersonPartSegmentation);
 
        const ctx = canvas.getContext('2d');
        if (coloredPartImageData) {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            bodyPix.drawMask(canvas, video, coloredPartImageData, 0.3);        
        }
 
        window.setTimeout(() => { 
            if (model.run) {
                model.run(model, video, canvas); 
             }
        }, 120);
    });
}
 
window.runblazeface = (model, video, canvas) => {
 
    console.time('runblazeface');
    model.estimateFaces(video, false).then(predictions => {
        console.timeEnd('runblazeface');
 
        console.log('predictions: ', predictions);
 
        const ctx = canvas.getContext('2d');
        context.clearRect(0, 0, canvas.width, canvas.height);
        for (let i = 0; i < predictions.length; i++) {
            const start = predictions[i].topLeft;
            const end = predictions[i].bottomRight;
            const size = [end[0] - start[0], end[1] - start[1]];
      
            ctx.rect(start[0], start[1], size[0], size[1]);
        }
 
        window.setTimeout(() => { 
            if (model.run) {
                model.run(model, video, canvas); 
             }
        }, 120);
    });
}