python读取excle中的数据后,将其中1,3,5列转化为转化为一个列表
时间: 2024-02-11 13:09:22 浏览: 150
查询数据库并导出到excle表格.py
IRuntime* runtime = createInferRuntime(gLogger);
engine = runtime->deserializeCudaEngine(
engineStream.str().data(), engineStream.str().size(), nullptr);
context = engine->createExecutionContext();
}
void loadModel(IBuilder*& builder, INetworkDefinition*& network) {
IParser* parser = createParser(*network, gLogger);
parser->parseFromFile(MODEL_FILE_PATH, static_cast<int>(Logger::Severity::kWARNING));
}
```
2. 创建输入和输出缓冲区
```c++
const int INPUT_H = 224;
const int INPUT_W = 224;
const int INPUT_C = 3;
const int BATCH_SIZE = 1;
void* inputBuffers[1];
void* outputBuffers[1];
void createInputOutputBuffer(IExecutionContext* context) {
int inputIndex = context->getEngine().getBindingIndex("input");
int outputIndex = context->getEngine().getBindingIndex("output");
cudaMalloc(&inputBuffers[inputIndex], BATCH_SIZE * INPUT_C * INPUT_H * INPUT_W * sizeof(float));
cudaMalloc(&outputBuffers[outputIndex], BATCH_SIZE * sizeof(float));
}
```
3. 加载输入照片到输入缓冲区
```c++
void loadInputImage(float* input, string imagePath) {
cv::Mat image = cv::imread(imagePath, cv::IMREAD_COLOR);
cv::Mat resizedImage;
cv::resize(image, resizedImage, cv::Size(INPUT_W, INPUT_H));
resizedImage.convertTo(resizedImage, CV_32FC3, 1.0f / 255.0f);
float* data = (float*)resizedImage.data;
int channels = resizedImage.channels();
for (int i = 0; i < INPUT_H; i++) {
for (int j = 0; j < INPUT_W; j++) {
for (int k = 0; k < channels; k++) {
input[k * INPUT_H * INPUT_W + i * INPUT_W + j] = *data++;
}
}
}
}
void loadInputImageToBuffer(IExecutionContext* context, string imagePath) {
int inputIndex = context->getEngine().getBindingIndex("input");
float* input = (float*)inputBuffers[inputIndex];
loadInputImage(input, imagePath);
}
```
4. 执行推理
```c++
void doInference(IExecutionContext* context) {
context->enqueue(BATCH_SIZE, inputBuffers, cudaStream_t{ 0 }, nullptr);
cudaStreamSynchronize(cudaStream_t{ 0 });
}
float* getOutput(IExecutionContext* context) {
int outputIndex = context->getEngine().getBindingIndex("output");
return (float*)outputBuffers[outputIndex];
}
```
5. 输出推理结果
```c++
void printOutput(float* output) {
cout << "Output: " << *output << endl;
}
```
完整的代码如下:
```c++
#include <iostream>
#include <fstream>
#include <sstream>
#include <cuda_runtime_api.h>
#include <NvInfer.h>
#include <NvOnnxParser.h>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace nvinfer1;
using namespace nvonnxparser;
static Logger gLogger;
const char* ENGINE_FILE_PATH = "path/to/engine/file";
const char* MODEL_FILE_PATH = "path/to/model好的,可以使用Python中的pandas库来读取Excel文件并提取指定列的数据,然后将这/file";
void loadEngineAndModel(IExecutionContext*& context, ICudaEngine*& engine) {
ifstream engineFile(ENGINE_FILE_PATH, ios些数据存储到一个列表中。下面是代码示例:
```python
import pandas as pd
# 读取Excel::binary);
stringstream engineStream;
engineStream << engineFile.rdbuf();
engineFile.close();
IRuntime* runtime =文件
df = pd.read_excel('your_file_path.xlsx')
# 提取指定列的数据
col1 = df.iloc[:, createInferRuntime(gLogger);
engine = runtime->deserializeCudaEngine(
engineStream.str().data(), engineStream.str().size(), nullptr);
context = engine->createExecutionContext();
}
void loadModel(IBuilder*& builder, INetworkDefinition*& network) {
0].tolist() # 第1列数据
col3 = df.iloc[:, 2].tolist() # 第3列数据
col5 = df.iloc[:, 4].tolist() # 第5列数据
# 将这些数据存储到一个列表中 IParser* parser = createParser(*network, gLogger);
parser->parseFromFile(MODEL_FILE_PATH, static_cast<int>(
result_list = [col1, col3, col5]
```
其中,`your_file_path.xlsx`是你要读取Logger::Severity::kWARNING));
}
const int INPUT_H = 224;
const int INPUT_W = 224;
const int INPUT_C = 3;
const int BATCH_SIZE = 1;
void* inputBuffers[1];
void* outputBuffers[1];
void createInputOutput的Excel文件的路径。`iloc`函数可以用来提取指定列的数据,`:,0`表示提取第1列的数据,`:,2`表示提取第3列的数据,`:,4`表示提取第5列的Buffer(IExecutionContext* context) {
int inputIndex = context->getEngine().getBindingIndex("input");
int outputIndex =数据。`tolist()`函数可以将提取的数据转化为列表。最后,将这些列表存储到一个列表 context->getEngine().getBindingIndex("output");
cudaMalloc(&inputBuffers[inputIndex], BATCH_SIZE * INPUT_C * INPUT_H中,即可得到所需的结果。
阅读全文