| | |
| | | |
| | | } |
| | | |
| | | 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}); |
| | |
| | | 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(); |
| | | }; |
New file |
| | |
| | | // |
| | | // 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; |
| | | } |
New file |
| | |
| | | // |
| | | // 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 |
| | |
| | | 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加载图片信息*/ |
| | |
| | | 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; |
| | | // } |