EntryPointNative.cpp 5.06 KB
#include "EntryPointNative.h"
#include <opencv2/opencv.hpp>
#include "SkeletonCNN2D.hpp"
//
#include "Manomotion.h"
#include "AdapterIOSNative.hpp"
#include "Utils.hpp"
#include "opencv2/opencv.hpp"
#include "ManoTimer.h"
#include <sys/stat.h>
#include <memory>
#include "AdapterBase.h"
#include "IOSUtils.hpp"

std::unique_ptr<AdapterIOSNative> adapter_ios = nullptr;

LicenseStatus licenseStatus;
ManoSettings settings;
ManoTimer mtimer;
Mat mr_frame;
cv::Mat working_frame;
DeviceInformation deviceInfo;

bool save_images = false;
const char *tag_ = "EntryPointNative";

void mlog(string msg) {
    Utils::Logv(tag_, "%s", msg.c_str());
}

void packageAnswer(HandInfo hand_info, Session session_obj, Session session) {
    cout << "inside packageAnswer " << endl;
}

/*
 * Used to intialise the license status
 */
void initLicenseStatus(LicenseStatus * lStatus,ManoSettings mano_settings,DeviceInformation deviceInformation) {

    cout << "inside initLicenseStatus " << endl;
    IOSUtils::setIsPowerSaveMode(deviceInformation.is_power_save_mode_on);

    adapter_ios = std::make_unique<AdapterIOSNative>(); // new
    adapter_ios->initialise(deviceInformation.path, deviceInformation.deviceId, deviceInformation.bundleId, deviceInformation.deviceModel, deviceInformation.deviceBrand,deviceInformation.filepath);

    cout << "from initLicenseStatus " << deviceInformation.path << " " << deviceInformation.deviceId << " " << deviceInformation.bundleId << " " << deviceInformation.deviceModel << " "  << deviceInformation.deviceBrand << endl;

    settings = IOSUtils::getManoSettings(mano_settings);

    LicenseStatus ls = adapter_ios->init(settings);
    *lStatus = IOSUtils::createLicenseStatus(ls);

    bool is_power_save_mode_on = IOSUtils::isPowerSaveMode();
#ifdef _DEBUG_ON_
    Utils::Logv(tag_, "PowerSave Mode %d", is_power_save_mode_on);
#endif
}

int frame_count = 771;

/*
 * Used to save frames into memory
 */

void saveImage(Mat mat, double timestamp,string uri1) {

#ifdef __ANDROID__
    string uri = "/storage/emulated/0/ManoMotionDebug/Right_hand";
#endif

#ifdef __IOS__
    string uri = uri1;
#endif

    int nError = 0;

    const char *str = uri.c_str();

    cout << "saveImage str " << str << endl;
    struct stat st = {0};
    int res = stat(str, &st);

    if (res == -1) {
        nError = mkdir(str, 0777);
        mlog("ManomotionDirectory for debugging in path " + uri + " created");
    }
    string png = ".png";
    uri += "/";
    //uri += to_string(timestamp);
    uri += "image (" + to_string(frame_count)+ ")";
    uri += png;

    cout << endl << "saveImage final URL " << uri << endl;
    imwrite(uri, mat);
}

/*
 * Used to intialise save_images flag
 */

void saveImages(bool val) {
    cout << "inside saveImages" << endl;
    save_images = val;
}

/*
 * Used to pass the MAT frame to the dynamic library and take output of the procesed frame
 */

void process(uchar * data, int width, int height,Session session_obj,HandInfo *hand_info0) {

    //session_obj.orientation = session_obj.orientation == 1 ? 4 : session_obj.orientation ;

    mr_frame = Mat(height,width, CV_8UC4, Scalar(255, 255, 0, 0));
    setResFrameArray(data, width, height, (uchar *)mr_frame.data);
#ifdef _DEBUG_ON_
    double end_pre_process = mtimer.getCurrentTimeStamp();
#endif
    try {
        Utils::setPath(adapter_ios->filepath);
        adapter_ios->processFrame(hand_info0, &session_obj);
    } catch (char const *error) {
#ifdef _DEBUG_ON_
        Utils::Logv(tag_, "%s", error);
#endif
        return;
    }

#ifdef _DEBUG_ON_
    double end_processFrame = mtimer.getCurrentTimeStamp();
    int time_end_processFrame = int(end_processFrame - end_pre_process);
    Utils::Logv(tag_, "***Time for frame %d", time_end_processFrame);
#endif

    frame_count++;

    //saveImage(adapter_ios->working_frame, end_processFrame, adapter_ios->filepath);
}
/*
  * Used to set frame array & MR frame array
  */
void setResFrameArray(uchar * data, int width, int height, uchar * mr_frame_data) {
    adapter_ios->setResolution(width, height);
    adapter_ios->setFrameArray(data); //required to run
    adapter_ios->setMRFrameArray(mr_frame_data);
}

/*
  * Used to set resolution of the MAT frame
  */
void setResolution(int width, int height) {
    adapter_ios->setResolution(width, height);
}

void cleanUp() {
    cout << "inside cleanUp" << endl;
}

int get_license_status_days_left() {
    return licenseStatus.days_left;
}

float get_license_status_version() {
    return licenseStatus.version;
}

LicenseAnswer get_license_status_license_answer() {
    return licenseStatus.license_answer;
}

int get_license_status_machines_left() {
    return licenseStatus.machines_left;
}

/*

 cout << "Debug handside hand_side " << hand_info0->gesture_info.hand_side << endl;
 cout << "Debug handside is_right " << hand_info0->gesture_info.is_right << endl;
 cout << "Debug handside mano_class " << hand_info0->gesture_info.mano_class << endl;
 cout << "Debug handside mano_gesture_continuous " << hand_info0->gesture_info.mano_gesture_continuous << endl;
 cout << "Debug handside mano_gesture_trigger " << hand_info0->gesture_info.mano_gesture_trigger << endl;
 */