From 26d46c6b81c6936b89e1d3ab1e212417dbb23712 Mon Sep 17 00:00:00 2001 From: xuepengqiang <506321815@qq.com> Date: 星期四, 09 一月 2020 09:36:08 +0800 Subject: [PATCH] update --- test.cpp | 15 ++++--- reid_feature.cpp | 2 reid_feature.h | 2 reid_utils.cpp | 65 ++++++++++++++++++++++++++++++++ reid_utils.h | 22 +++++++++++ 5 files changed, 98 insertions(+), 8 deletions(-) diff --git a/reid_feature.cpp b/reid_feature.cpp index 4bb847e..fdf1e88 100644 --- a/reid_feature.cpp +++ b/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}); diff --git a/reid_feature.h b/reid_feature.h index 782ffda..8a98bf2 100644 --- a/reid_feature.h +++ b/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(); }; diff --git a/reid_utils.cpp b/reid_utils.cpp new file mode 100644 index 0000000..4208152 --- /dev/null +++ b/reid_utils.cpp @@ -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; +} diff --git a/reid_utils.h b/reid_utils.h new file mode 100644 index 0000000..47c249b --- /dev/null +++ b/reid_utils.h @@ -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 diff --git a/test.cpp b/test.cpp index d3f2eda..72640b9 100644 --- a/test.cpp +++ b/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; // } -- Gitblit v1.8.0