File size: 20,108 Bytes
f5bb0c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#ifndef OPENPOSE_PRIVATE_UTILITIES_RENDER_HU
#define OPENPOSE_PRIVATE_UTILITIES_RENDER_HU

namespace op
{
    __inline__ __device__ void getBoundingBoxPerPerson(
        float* maxPtr, float* minPtr, float* scalePtr,const unsigned int targetWidth, const unsigned int targetHeight,
        const float* const keypointsPtr, const int numberPeople, const int numberParts, const float threshold)
    {
        const unsigned long globalIdx = threadIdx.x;

        // Fill shared parameters
        if (globalIdx < numberPeople)
        {
            float minValueX = (float)targetWidth;
            float minValueY = (float)targetHeight;
            float maxValueX = 0.f;
            float maxValueY = 0.f;
            for (auto part = 0 ; part < numberParts ; part++)
            {
                const unsigned long index = 3u * (globalIdx*numberParts + part);
                const float x = keypointsPtr[index];
                const float y = keypointsPtr[index+1];
                const float score = keypointsPtr[index+2];
                if (score > threshold)
                {
                    if (x < minValueX)
                        minValueX = x;
                    if (x > maxValueX)
                        maxValueX = x;
                    if (y < minValueY)
                        minValueY = y;
                    if (y > maxValueY)
                        maxValueY = y;
                }
            }
            if (maxValueX != 0.f && maxValueY != 0.f)
            {
                const auto averageX = maxValueX - minValueX;
                const auto averageY = maxValueY - minValueY;
                // (averageX + averageY) / 2.f / 400.f
                scalePtr[globalIdx] = fastTruncateCuda((averageX + averageY) / 400.f, 0.33f, 1.f);
                const auto constantToAdd = 50.f;
                maxValueX += constantToAdd;
                maxValueY += constantToAdd;
                minValueX -= constantToAdd;
                minValueY -= constantToAdd;
            }

            // const auto xIndex = 2*globalIdx;
            // const auto yIndex = xIndex+1;
            const auto xIndex = globalIdx;
            const auto yIndex = numberPeople+globalIdx;
            minPtr[xIndex] = minValueX;
            minPtr[yIndex] = minValueY;
            maxPtr[xIndex] = maxValueX;
            maxPtr[yIndex] = maxValueY;
        }
    }

    // Note: renderKeypoints is not working for videos with many people, renderKeypointsOld speed was slightly improved instead
    __inline__ __device__ void renderKeypoints(
        float* targetPtr, float* sharedMaxs, float* sharedMins, float* sharedScaleF, const float* const maxPtr,
        const float* const minPtr, const float* const scalePtr, const int globalIdx, const int x, const int y,
        const unsigned int targetWidth, const unsigned int targetHeight, const float* const keypointsPtr,
        const unsigned int* const partPairsPtr, const int numberPeople, const int numberParts,
        const int numberPartPairs, const float* const rgbColorsPtr, const int numberColors, const float radius,
        const float lineWidth, const float* const keypointScalePtr, const int numberScales, const float threshold,
        const float alphaColorToAdd, const bool blendOriginalFrame = true, const int googlyEye1 = -1,
        const int googlyEye2 = -1)
    {
        // Load shared memory
        if (globalIdx < 2*numberPeople)
        {
            sharedMins[globalIdx] = minPtr[globalIdx];
            sharedMaxs[globalIdx] = maxPtr[globalIdx];
            if (globalIdx < numberPeople)
                sharedScaleF[globalIdx] = scalePtr[globalIdx];
        }
        __syncthreads();

        // Fill each (x,y) target pixel
        if (x < targetWidth && y < targetHeight)
        {
            const unsigned long baseIndex = 3u*(y * (unsigned long)targetWidth + x);
            float b = targetPtr[baseIndex];
            float g = targetPtr[baseIndex+1];
            float r = targetPtr[baseIndex+2];
            if (!blendOriginalFrame)
            {
                b = 0.f;
                g = 0.f;
                r = 0.f;
            }

            const auto lineWidthSquared = lineWidth * lineWidth;
            const auto radiusSquared = radius * radius;
            for (auto person = 0; person < numberPeople; person++)
            {
                // Make sure person x,y in the limits
                // Make sure person is not empty. Assume all joints are below threshold. Then
                // maxs = 0 and mins = width/height. So if statement would be false
                // const auto xIndex = 2*person;
                // const auto yIndex = xIndex+1;
                const auto xIndex = person;
                const auto yIndex = numberPeople+person;
                if (x <= sharedMaxs[xIndex] && x >= sharedMins[xIndex]
                    && y <= sharedMaxs[yIndex] && y >= sharedMins[yIndex])
                {
                    // Part pair connections
                    for (auto partPair = 0; partPair < numberPartPairs; partPair++)
                    {
                        const auto partA = partPairsPtr[2*partPair];
                        const auto partB = partPairsPtr[2*partPair+1];
                        const auto indexA = person*numberParts*3 + partA*3;
                        const auto xA = keypointsPtr[indexA];
                        const auto yA = keypointsPtr[indexA + 1];
                        const auto scoreA = keypointsPtr[indexA + 2];
                        const auto indexB = person*numberParts*3 + partB*3;
                        const auto xB = keypointsPtr[indexB];
                        const auto yB = keypointsPtr[indexB + 1];
                        const auto scoreB = keypointsPtr[indexB + 2];

                        if (scoreA > threshold && scoreB > threshold)
                        {
                            const auto keypointScale = keypointScalePtr[partB%numberScales]
                                                     * keypointScalePtr[partB%numberScales]
                                                     * keypointScalePtr[partB%numberScales];
                            const auto lineWidthScaled = lineWidthSquared * keypointScale;
                            const auto bSqrt = sharedScaleF[person] * sharedScaleF[person] * lineWidthScaled;

                            const auto xP = (xA + xB) / 2.f;
                            const auto yP = (yA + yB) / 2.f;
                            const auto aSqrt = (xA - xP) * (xA - xP) + (yA - yP) * (yA - yP);

                            const auto angle = atan2f(yB - yA, xB - xA);
                            const auto sine = sinf(angle);
                            const auto cosine = cosf(angle);
                            const auto A = cosine * (x - xP) + sine * (y - yP);
                            const auto B = sine * (x - xP) - cosine * (y - yP);

                            const auto judge = A * A / aSqrt + B * B / bSqrt;
                            const auto minV = 0.f;
                            const auto maxV = 1.f;
                            if (minV <= judge && judge <= maxV)
                                // Before used partPair vs partB
                                addColorWeighted(r, g, b, &rgbColorsPtr[(partB%numberColors)*3], alphaColorToAdd);
                        }
                    }

                    // Part circles
                    for (auto part = 0u; part < numberParts; part++)
                    {
                        const auto index = 3 * (person*numberParts + part);
                        const auto localX = keypointsPtr[index];
                        const auto localY = keypointsPtr[index + 1];
                        const auto score = keypointsPtr[index + 2];

                        if (score > threshold)
                        {
                            const auto keypointScale = keypointScalePtr[part%numberScales]
                                                     * keypointScalePtr[part%numberScales]
                                                     * keypointScalePtr[part%numberScales];
                            const auto radiusScaled = radiusSquared * keypointScale;
                            const auto dist2 = (x - localX) * (x - localX) + (y - localY) * (y - localY);
                            // Googly eyes
                            if (googlyEye1 == part || googlyEye2 == part)
                            {
                                const auto eyeRatio = 2.5f * sqrt(radiusScaled);
                                const auto minr2 = sharedScaleF[person] * sharedScaleF[person]
                                                 * (eyeRatio - 2) * (eyeRatio - 2);
                                const auto maxr2 = sharedScaleF[person] * sharedScaleF[person] * eyeRatio * eyeRatio;
                                if (dist2 <= maxr2)
                                {
                                    float colorToAdd [3] = {0., 0., 0.};
                                    if (dist2 <= minr2)
                                        for (auto& color : colorToAdd)
                                            color = {255.f};
                                    if (dist2 <= minr2*0.6f)
                                    {
                                        const auto dist3 = (x-4 - localX)
                                                         * (x-4 - localX) + (y - localY+4) * (y - localY+4);
                                        if (dist3 > 14.0625f) // 3.75f^2
                                            for (auto& color : colorToAdd)
                                                color = {0.f};
                                    }
                                    const auto alphaColorToAdd = 0.9f;
                                    addColorWeighted(r, g, b, colorToAdd, alphaColorToAdd);
                                }
                            }
                            // Other parts
                            else
                            {
                                const auto minr2 = 0.f;
                                const auto maxr2 = sharedScaleF[person] * sharedScaleF[person] * radiusScaled;
                                if (minr2 <= dist2 && dist2 <= maxr2)
                                    addColorWeighted(r, g, b, &rgbColorsPtr[(part%numberColors)*3], alphaColorToAdd);
                            }
                        }
                    }
                }
            }
            targetPtr[baseIndex] = b;
            targetPtr[baseIndex+1] = g;
            targetPtr[baseIndex+2] = r;
        }
    }

    __inline__ __device__ void renderKeypointsOld(
        float* targetPtr, float2* sharedMaxs, float2* sharedMins, float* sharedScaleF, const int globalIdx,
        const int x, const int y, const unsigned int targetWidth, const unsigned int targetHeight, const float* const keypointsPtr,
        const unsigned int* const partPairsPtr, const int numberPeople, const int numberParts,
        const int numberPartPairs, const float* const rgbColorsPtr, const int numberColors, const float radius,
        const float lineWidth, const float* const keypointScalePtr, const int numberScales, const float threshold,
        const float alphaColorToAdd, const bool blendOriginalFrame = true, const int googlyEye1 = -1,
        const int googlyEye2 = -1)
    {
        // Fill shared parameters
        if (globalIdx < numberPeople)
        {
            float minValueX = (float)targetWidth;
            float minValueY = (float)targetHeight;
            float maxValueX = 0.f;
            float maxValueY = 0.f;
            for (auto part = 0 ; part < numberParts ; part++)
            {
                const unsigned long index = 3u * (((unsigned long)globalIdx)*numberParts + part);
                const float x = keypointsPtr[index];
                const float y = keypointsPtr[index+1];
                const float score = keypointsPtr[index+2];
                if (score > threshold)
                {
                    if (x < minValueX)
                        minValueX = x;
                    if (x > maxValueX)
                        maxValueX = x;
                    if (y < minValueY)
                        minValueY = y;
                    if (y > maxValueY)
                        maxValueY = y;
                }
            }
            if (maxValueX != 0.f && maxValueY != 0.f)
            {
                const auto averageX = maxValueX - minValueX;
                const auto averageY = maxValueY - minValueY;
                // (averageX + averageY) / 2.f / 400.f
                sharedScaleF[globalIdx] = fastTruncateCuda((averageX + averageY) / 400.f, 0.33f, 1.f);
                const auto constantToAdd = 50.f;
                maxValueX += constantToAdd;
                maxValueY += constantToAdd;
                minValueX -= constantToAdd;
                minValueY -= constantToAdd;
            }

            sharedMins[globalIdx].x = minValueX;
            sharedMins[globalIdx].y = minValueY;
            sharedMaxs[globalIdx].x = maxValueX;
            sharedMaxs[globalIdx].y = maxValueY;
        }
        __syncthreads();

        // Fill each (x,y) target pixel
        if (x < targetWidth && y < targetHeight)
        {
            const unsigned long baseIndex = 3u*(y * (unsigned long)targetWidth + x);
            float b = targetPtr[baseIndex];
            float g = targetPtr[baseIndex+1];
            float r = targetPtr[baseIndex+2];
            if (!blendOriginalFrame)
            {
                b = 0.f;
                g = 0.f;
                r = 0.f;
            }

            const auto lineWidthSquared = lineWidth * lineWidth;
            const auto radiusSquared = radius * radius;
            for (auto person = 0; person < numberPeople; person++)
            {
                // Make sure person x,y in the limits
                // Make sure person is not empty. Assume all joints are below threshold. Then
                // maxs = 0 and mins = width/height. So if statement would be false
                if (x <= sharedMaxs[person].x && x >= sharedMins[person].x
                    && y <= sharedMaxs[person].y && y >= sharedMins[person].y)
                {
                    // Part pair connections
                    for (auto partPair = 0; partPair < numberPartPairs; partPair++)
                    {
                        const auto partA = partPairsPtr[2*partPair];
                        const auto partB = partPairsPtr[2*partPair+1];
                        const auto indexA = person*numberParts*3 + partA*3;
                        const auto xA = keypointsPtr[indexA];
                        const auto yA = keypointsPtr[indexA + 1];
                        const auto scoreA = keypointsPtr[indexA + 2];
                        const auto indexB = person*numberParts*3 + partB*3;
                        const auto xB = keypointsPtr[indexB];
                        const auto yB = keypointsPtr[indexB + 1];
                        const auto scoreB = keypointsPtr[indexB + 2];

                        if (scoreA > threshold && scoreB > threshold)
                        {
                            const auto keypointScale = keypointScalePtr[partB%numberScales]
                                                     * keypointScalePtr[partB%numberScales]
                                                     * keypointScalePtr[partB%numberScales];
                            const auto lineWidthScaled = lineWidthSquared * keypointScale;
                            const auto bSqrt = sharedScaleF[person] * sharedScaleF[person] * lineWidthScaled;

                            const auto xP = (xA + xB) / 2.f;
                            const auto yP = (yA + yB) / 2.f;
                            const auto aSqrt = (xA - xP) * (xA - xP) + (yA - yP) * (yA - yP);

                            const auto angle = atan2f(yB - yA, xB - xA);
                            const auto sine = sinf(angle);
                            const auto cosine = cosf(angle);
                            const auto A = cosine * (x - xP) + sine * (y - yP);
                            const auto B = sine * (x - xP) - cosine * (y - yP);

                            const auto judge = A * A / aSqrt + B * B / bSqrt;
                            const auto minV = 0.f;
                            const auto maxV = 1.f;
                            if (minV <= judge && judge <= maxV)
                                // Before used partPair vs partB
                                addColorWeighted(r, g, b, &rgbColorsPtr[(partB%numberColors)*3], alphaColorToAdd);
                        }
                    }

                    // Part circles
                    for (auto part = 0u; part < numberParts; part++)
                    {
                        const auto index = 3 * (person*numberParts + part);
                        const auto localX = keypointsPtr[index];
                        const auto localY = keypointsPtr[index + 1];
                        const auto score = keypointsPtr[index + 2];

                        if (score > threshold)
                        {
                            const auto keypointScale = keypointScalePtr[part%numberScales]
                                                     * keypointScalePtr[part%numberScales]
                                                     * keypointScalePtr[part%numberScales];
                            const auto radiusScaled = radiusSquared * keypointScale;
                            const auto dist2 = (x - localX) * (x - localX) + (y - localY) * (y - localY);
                            // Googly eyes
                            if (googlyEye1 == part || googlyEye2 == part)
                            {
                                const auto eyeRatio = 2.5f * sqrt(radiusScaled);
                                const auto minr2 = sharedScaleF[person] * sharedScaleF[person]
                                                 * (eyeRatio - 2) * (eyeRatio - 2);
                                const auto maxr2 = sharedScaleF[person] * sharedScaleF[person] * eyeRatio * eyeRatio;
                                if (dist2 <= maxr2)
                                {
                                    float colorToAdd [3] = {0., 0., 0.};
                                    if (dist2 <= minr2)
                                        for (auto& color : colorToAdd)
                                            color = {255.f};
                                    if (dist2 <= minr2*0.6f)
                                    {
                                        const auto dist3 = (x-4 - localX)
                                                         * (x-4 - localX) + (y - localY+4) * (y - localY+4);
                                        if (dist3 > 14.0625f) // 3.75f^2
                                            for (auto& color : colorToAdd)
                                                color = {0.f};
                                    }
                                    const auto alphaColorToAdd = 0.9f;
                                    addColorWeighted(r, g, b, colorToAdd, alphaColorToAdd);
                                }
                            }
                            // Other parts
                            else
                            {
                                const auto minr2 = 0.f;
                                const auto maxr2 = sharedScaleF[person] * sharedScaleF[person] * radiusScaled;
                                if (minr2 <= dist2 && dist2 <= maxr2)
                                    addColorWeighted(r, g, b, &rgbColorsPtr[(part%numberColors)*3], alphaColorToAdd);
                            }
                        }
                    }
                }
            }
            targetPtr[baseIndex] = b;
            targetPtr[baseIndex+1] = g;
            targetPtr[baseIndex+2] = r;
        }
    }
}


#endif // OPENPOSE_PRIVATE_UTILITIES_RENDER_HU