wPeopleJsonSaver.hpp 4.1 KB
Newer Older
1 2
#ifndef OPENPOSE_FILESTREAM_W_PEOPLE_JSON_SAVER_HPP
#define OPENPOSE_FILESTREAM_W_PEOPLE_JSON_SAVER_HPP
3

4
#include <openpose/core/common.hpp>
5
#include <openpose/filestream/peopleJsonSaver.hpp>
6 7 8 9 10
#include <openpose/thread/workerConsumer.hpp>

namespace op
{
    template<typename TDatums>
11
    class WPeopleJsonSaver : public WorkerConsumer<TDatums>
12 13
    {
    public:
14
        explicit WPeopleJsonSaver(const std::shared_ptr<PeopleJsonSaver>& peopleJsonSaver);
15

16 17
        virtual ~WPeopleJsonSaver();

18 19 20 21 22
        void initializationOnThread();

        void workConsumer(const TDatums& tDatums);

    private:
23
        const std::shared_ptr<PeopleJsonSaver> spPeopleJsonSaver;
24

25
        DELETE_COPY(WPeopleJsonSaver);
26 27 28 29 30 31 32 33 34 35 36 37
    };
}





// Implementation
#include <openpose/utilities/pointerContainer.hpp>
namespace op
{
    template<typename TDatums>
38 39
    WPeopleJsonSaver<TDatums>::WPeopleJsonSaver(const std::shared_ptr<PeopleJsonSaver>& peopleJsonSaver) :
        spPeopleJsonSaver{peopleJsonSaver}
40 41 42
    {
    }

43 44 45 46 47
    template<typename TDatums>
    WPeopleJsonSaver<TDatums>::~WPeopleJsonSaver()
    {
    }

48
    template<typename TDatums>
49
    void WPeopleJsonSaver<TDatums>::initializationOnThread()
50 51 52 53
    {
    }

    template<typename TDatums>
54
    void WPeopleJsonSaver<TDatums>::workConsumer(const TDatums& tDatums)
55 56 57 58 59 60 61 62 63 64
    {
        try
        {
            if (checkNoNullNorEmpty(tDatums))
            {
                // Debugging log
                dLog("", Priority::Low, __LINE__, __FUNCTION__, __FILE__);
                // Profiling speed
                const auto profilerKey = Profiler::timerInit(__LINE__, __FUNCTION__, __FILE__);
                // Save body/face/hand keypoints to JSON file
G
gineshidalgo99 已提交
65 66 67
                const auto& tDatumFirstPtr = (*tDatums)[0];
                const auto baseFileName = (!tDatumFirstPtr->name.empty() ? tDatumFirstPtr->name
                                            : std::to_string(tDatumFirstPtr->id)) + "_keypoints";
68
                const bool humanReadable = false;
G
gineshidalgo99 已提交
69
                for (auto i = 0u ; i < tDatums->size() ; i++)
70
                {
G
gineshidalgo99 已提交
71
                    const auto& tDatumPtr = (*tDatums)[i];
72 73 74
                    // const auto fileName = baseFileName;
                    const auto fileName = baseFileName + (i != 0 ? "_" + std::to_string(i) : "");

75 76 77
                    // Pose IDs from long long to float
                    Array<float> poseIds{tDatumPtr->poseIds};

78
                    const std::vector<std::pair<Array<float>, std::string>> keypointVector{
79 80
                        // Pose IDs
                        std::make_pair(poseIds, "person_id"),
81
                        // 2D
G
gineshidalgo99 已提交
82 83 84 85
                        std::make_pair(tDatumPtr->poseKeypoints, "pose_keypoints_2d"),
                        std::make_pair(tDatumPtr->faceKeypoints, "face_keypoints_2d"),
                        std::make_pair(tDatumPtr->handKeypoints[0], "hand_left_keypoints_2d"),
                        std::make_pair(tDatumPtr->handKeypoints[1], "hand_right_keypoints_2d"),
86
                        // 3D
G
gineshidalgo99 已提交
87 88 89 90
                        std::make_pair(tDatumPtr->poseKeypoints3D, "pose_keypoints_3d"),
                        std::make_pair(tDatumPtr->faceKeypoints3D, "face_keypoints_3d"),
                        std::make_pair(tDatumPtr->handKeypoints3D[0], "hand_left_keypoints_3d"),
                        std::make_pair(tDatumPtr->handKeypoints3D[1], "hand_right_keypoints_3d")
91 92
                    };
                    // Save keypoints
93 94
                    spPeopleJsonSaver->save(
                        keypointVector, tDatumPtr->poseCandidates, fileName, humanReadable);
95 96 97
                }
                // Profiling speed
                Profiler::timerEnd(profilerKey);
98
                Profiler::printAveragedTimeMsOnIterationX(profilerKey, __LINE__, __FUNCTION__, __FILE__);
99 100 101 102 103 104 105 106 107 108 109
                // Debugging log
                dLog("", Priority::Low, __LINE__, __FUNCTION__, __FILE__);
            }
        }
        catch (const std::exception& e)
        {
            this->stop();
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }

110
    COMPILE_TEMPLATE_DATUM(WPeopleJsonSaver);
111 112
}

113
#endif // OPENPOSE_FILESTREAM_W_PEOPLE_JSON_SAVER_HPP