xuepengqiang
2020-01-09 26d46c6b81c6936b89e1d3ab1e212417dbb23712
update
2个文件已添加
3个文件已修改
106 ■■■■■ 已修改文件
reid_feature.cpp 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
reid_feature.h 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
reid_utils.cpp 65 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
reid_utils.h 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test.cpp 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
reid_feature.cpp
@@ -34,7 +34,7 @@
}
bool ReID_Feature::ReID_extractor(unsigned char *pBuf, float *pFeature)
bool ReID_Feature::ReID_extractor(float *pBuf, float *pFeature)
{
    auto input_tensor = torch::from_blob(pBuf, {1, 256, 128, 3});
    input_tensor = input_tensor.permute({0, 3, 1, 2});
reid_feature.h
@@ -23,7 +23,7 @@
    bool ReID_init(int gpu_id);
    int ReID_size();
//    static unsigned char * extractor(unsigned char *pBuf, unsigned char *pFeature);
    bool ReID_extractor(unsigned char *pBuf, float * pFeature);
    bool ReID_extractor(float *pBuf, float * pFeature);
    float ReID_Compare(float *pFeature1, float *pFeature2);
    void ReID_Release();
};
reid_utils.cpp
New file
@@ -0,0 +1,65 @@
//
// Created by Scheaven on 2020/1/3.
//
#include "reid_utils.h"
//import  torch
//
//a=torch.Tensor([[1,2,3],[4,5,6]])
//b=torch.Tensor([[7,8,9],[10,11,12]])
//d=torch.stack( (a,b) ,dim = 1)
//
//print(d)
//template<typename T>
//unsigned char * ReID_Utils::T2bytes(T u)
//{
//    int n = sizeof(T);
//    unsigned char* b = new unsigned char[n];
//    memcpy(b, &u, n);
//    return b;
//}
//
//template<typename T>
//T ReID_Utils::bytes2T(unsigned char *bytes)
//{
//    T res = 0;
//    int n = sizeof(T);
//    memcpy(&res, bytes, n);
//    return res;
//}
unsigned char * ReID_Utils::T2bytes(float u)
{
    int n = sizeof(u);
    unsigned char* b = new unsigned char[n];
    memcpy(b, &u, n);
    return b;
}
float ReID_Utils::bytes2T(unsigned char *bytes)
{
    float res = 0;
    int n = sizeof(res);
    memcpy(&res, bytes, n);
    return res;
}
void *ReID_Utils::normalize(unsigned char *vsrc, int w, int h, int chan){
    float *data = (float*)malloc(h*w*chan*sizeof(float));
    int size = w*h;
    int size2 = size*2;
    unsigned char *srcData = (unsigned char*)vsrc;
    for(int i = 0;i<size;i++){
        *(data) = *(srcData + 2) /255.0f;
        *(data+size) = *(srcData + 1) /255.0f;
        *(data+size2) = *(srcData) /255.0f;
        data++;
        srcData+=3;
    }
    return data;
}
reid_utils.h
New file
@@ -0,0 +1,22 @@
//
// Created by Scheaven on 2020/1/3.
//
#ifndef INC_03_REID_STRONG_BASELINE_REID_UTILS_H
#define INC_03_REID_STRONG_BASELINE_REID_UTILS_H
#include <torch/script.h>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdlib.h>
#include <vector>
#include <iostream>
using namespace std;
struct ReID_Utils{
    public:
        float *normalize(unsigned char *vsrc, int w, int h, int chan);
};
#endif //INC_03_REID_STRONG_BASELINE_REID_UTILS_H
test.cpp
@@ -33,6 +33,7 @@
    int gpu_id = 0;
    ReID_Feature R_Feater;
    bool n_flog = R_Feater.ReID_init(0);
    ReID_Utils r_util;
//    ReID_Feature R_Feater(gpu_id);
    /*opencv加载图片信息*/
@@ -48,17 +49,19 @@
        try {
            float pFeature1[2048];
            /*转义图片信息格式*/
            cv::cvtColor(human_img, human_img, cv::COLOR_RGB2BGR);
            human_img.convertTo(human_img, CV_32FC3, 1.0f / 255.0f);
            bool ex_flag1 = R_Feater.ReID_extractor(human_img.data, pFeature1);
            // cv::cvtColor(human_img, human_img, cv::COLOR_RGB2BGR);
            // human_img.convertTo(human_img, CV_32FC3, 1.0f / 255.0f);
            float *my_img_data = r_util.normalize(human_img.data, human_img.cols, human_img.rows, 3);
            bool ex_flag1 = R_Feater.ReID_extractor(my_img_data, pFeature1);
//            for (int k = 0; k < 20; ++k) {
//                cout << "-----11111111111------" <<pFeature1[k+2000]<< endl;
//            }
            float pFeature2[2048];
            cv::cvtColor(human_img2, human_img2, cv::COLOR_RGB2BGR);
            human_img2.convertTo(human_img2, CV_32FC3, 1.0f / 255.0f);
            bool ex_flag2 = R_Feater.ReID_extractor(human_img2.data, pFeature2);
            // cv::cvtColor(human_img2, human_img2, cv::COLOR_RGB2BGR);
            // human_img2.convertTo(human_img2, CV_32FC3, 1.0f / 255.0f);
            float *my_img_data2 = r_util.normalize(human_img.data, human_img.cols, human_img.rows, 3);
            bool ex_flag2 = R_Feater.ReID_extractor(my_img_data2, pFeature2);
//            for (int k = 0; k < 20; ++k) {
//                cout << "-----2222222222------" <<pFeature2[k+2000]<< endl;
//            }