PicAnalysis.cpp 6.72 KB
#include "PicAnalysis.h"
#include "./utils/logger.hpp"


PicAnalysis::PicAnalysis(/* args */)
{
    aclInit(nullptr);
}

PicAnalysis::~PicAnalysis()
{
    release();
    aclFinalize();
}

int PicAnalysis::init(int dev_id) {

    int ret = SY_FAILED;

    ret = m_vehicle_analysis.init(dev_id, 16);
    if(0 != ret){
        return -1;
    }

    head_tail_param ht_param;
    ht_param.devId = dev_id;
    ht_param.max_batch = 16;
    ret = m_head_tail_algorithm.init(ht_param);
    if(0 != ret){
        return -1;
    }

    ret = m_clothes_algorithm.init(dev_id);
    if(0 != ret){
        return -1;
    }

    ret = m_human_algorithm.init(dev_id);
    if(0 != ret){
        return -1;
    }

    ret = m_human_car_algorithm.init(dev_id);
    if(0 != ret){
        return -1;
    }

    ret = m_motor_rainshed_algorithm.init(dev_id);
    if(0 != ret){
        return -1;
    }

    ret = m_motor_phone_algorithm.init(dev_id);
    if(0 != ret){
        return -1;
    }

    ret = m_road_seg_algorithm.init(dev_id);
    if(0 != ret){
        return -1;
    }

    ret = m_crop_util.init(dev_id);
    if(0 != ret){
        return -1;
    }

    ACL_CALL(aclrtCreateContext(&m_ctx, 0), ACL_SUCCESS, SY_FAILED);
	ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_SUCCESS, SY_FAILED);
	ACL_CALL(aclrtCreateStream(&stream), ACL_SUCCESS, SY_FAILED);
    m_dvpp = new DvppProcess();
	m_dvpp->InitResource(stream);

    return 0;
}

int PicAnalysis::analysis_sync(vector<string> vec_file_path){

    ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_SUCCESS, SY_FAILED);

    const int batch_size = vec_file_path.size();

    vector<sy_img> vec_img;

    int ret = SY_FAILED;

    // ImageData 内部是智能指针,分析未处理完成前不得释放
    ImageData dvpp_data[batch_size];
    for (size_t i = 0; i < vec_file_path.size(); i++)
    {
        string file_path = vec_file_path[i];
        ImageData src;
        ret = Utils::ReadImageFile(src, file_path); //将二进制图像读入内存,并读取宽高信息
        if(ret != SY_SUCCESS){
            LOG_ERROR("ReadImageFile failed!");
            return -1;
        }

        ret = m_dvpp->CvtJpegToYuv420sp(dvpp_data[i], src); //解码
        if(ret != SY_SUCCESS){
            LOG_ERROR("CvtJpegToYuv420sp failed!");
            return -1;
        }
        
        sy_img img;
        img.w_ = dvpp_data[i].width;
        img.h_ = dvpp_data[i].height;
        img.data_ = dvpp_data[i].data.get();

        vec_img.push_back(img);
    }
    
    va_result* result = m_vehicle_analysis.detect(vec_img);

    m_road_seg_algorithm.detect(vec_img);

    for (int b = 0; b < batch_size; b++)
    {
        ImageData src = dvpp_data[b];
        for(int c=0;c<result[b].count;c++)
        {
            vehicle_info result_info = result[b].info[c];
            int shot_type=result[b].info[c].type;

            // 行人
            if (6 == shot_type)
            {
                sy_rect human_rect = result_info.vehicle_body_detect_res.rect;
                ImageData* human_data = m_crop_util.crop(src, human_rect.left_, human_rect.top_, human_rect.left_ + human_rect.width_, human_rect.top_ + human_rect.height_);

                sy_img img;
                img.w_ = human_data->alignWidth;
                img.h_ = human_data->alignHeight;
                img.data_ = human_data->data_naked;

                vector<sy_img> vec_human_img;
                vec_human_img.push_back(img);

                human_analysis(vec_human_img);

                delete human_data;
                human_data = nullptr;
            }

            // 司乘
            int vpd_num = result_info.vehicle_pendant_det_res.count;
            vector<sy_img> vec_human_img;
            for(int p=0; p<vpd_num; p++)
            {
                int index = result_info.vehicle_pendant_det_res.vpd_res[p].index;
                if(index == 0){
                    sy_rect human_rect = result_info.vehicle_pendant_det_res.vpd_res[p].rect;
                    ImageData* human_data = m_crop_util.crop(src, human_rect.left_, human_rect.top_, human_rect.left_ + human_rect.width_, human_rect.top_ + human_rect.height_);

                    sy_img img;
                    img.w_ = human_data->alignWidth;
                    img.h_ = human_data->alignHeight;
                    img.data_ = human_data->data_naked;

                    vec_human_img.push_back(img);
                }
            }

            m_clothes_algorithm.detect(vec_human_img);

            for(int p=0; p<vpd_num; p++)
            {
                int index = result_info.vehicle_pendant_det_res.vpd_res[p].index;
                if(index == 0){
                    sy_rect human_rect = result_info.vehicle_pendant_det_res.vpd_res[p].rect;
                }
            }

            // 摩托车
            if(shot_type==2)//摩托车
            {
                //摩托车驾驶人是否戴安全帽
                // analysis_result.motor_helmeted = result_info.mta_res.motor_driver_helmeted.status;
                // float score=result_info.mta_res.motor_driver_helmeted.confidence;

                sy_rect motor_rect = result_info.vehicle_body_detect_res.rect;
                ImageData* motor_data = m_crop_util.crop(src, motor_rect.left_, motor_rect.top_, motor_rect.left_ + motor_rect.width_, motor_rect.top_ + motor_rect.height_);

                sy_img img;
                img.w_ = motor_data->alignWidth;
                img.h_ = motor_data->alignHeight;
                img.data_ = motor_data->data_naked;

                vector<sy_img> vec_motor_img;
                vec_motor_img.push_back(img);

                m_motor_rainshed_algorithm.detect(vec_motor_img);

                m_motor_phone_algorithm.detect(vec_motor_img);

                // 
                

                delete motor_data;
                motor_data = nullptr;
            }
            
        }
    }

    vector<HeadTailResult> head_tail_result;
    ret = m_head_tail_algorithm.detect(vec_img, head_tail_result);
    if (0 != ret) {
        LOG_ERROR("m_head_tail_algorithm failed!");
        head_tail_result.clear();
    }

    
    
    LOG_INFO("analysis_sync finished!");

    return 0;
}

int PicAnalysis::release() {

    ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_SUCCESS, SY_FAILED);

    delete m_dvpp;
    m_dvpp = nullptr;

    if (stream != nullptr) {
        int ret = aclrtDestroyStream(stream);
        if (ret != ACL_SUCCESS) {
            LOG_ERROR("destroy stream failed");
        }
        stream = nullptr;
    }

    aclrtDestroyContext(m_ctx);

    return 0;
}

int PicAnalysis::human_analysis(vector<sy_img> vec_img) {
    vector<BodyColorInfo> vec_body_color = m_human_algorithm.detect(vec_img);
}

int PicAnalysis::check_motor_retrograde_motion(vector<sy_img> vec_motor_img) {
    m_human_car_algorithm.detect(vec_motor_img);
}