'ov::Exception,位于内存位置 0x00000001000FF650 处',局部变量ie显示<无可用信息,未为openvino.dll加载任何符号>
#include
#include
#include //include openvino runtime header files
#include //opencv header file
/* --------- Please modify the path of yolov5 model and image -----------*/
std::string model_file = "yolov5s.xml";
std::string image_file = "zidane.jpg";
std::vector colors = { cv::Scalar(0, 0, 255) , cv::Scalar(0, 255, 0) , cv::Scalar(255, 0, 0) ,
cv::Scalar(255, 255, 0) , cv::Scalar(0, 255, 255) , cv::Scalar(255, 0, 255) };
const std::vector class_names = {
"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
"fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
"elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
"skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
"tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
"sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
"potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
"microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear",
"hair drier", "toothbrush" };
cv::Mat letterbox(cv::Mat& img, std::vector<float>& paddings, std::vector<int> new_shape = { 640, 640 })
{
// Get current image shape [height, width]
// Refer to https://github.com/ultralytics/yolov5/blob/master/utils/augmentations.py#L111
int img_h = img.rows;
int img_w = img.cols;
// Compute scale ratio(new / old) and target resized shape
float scale = std::min(new_shape[1] * 1.0 / img_h, new_shape[0] * 1.0 / img_w);
int resize_h = int(round(img_h * scale));
int resize_w = int(round(img_w * scale));
paddings[0] = scale;
// Compute padding
int pad_h = new_shape[1] - resize_h;
int pad_w = new_shape[0] - resize_w;
// Resize and pad image while meeting stride-multiple constraints
cv::Mat resized_img;
cv::resize(img, resized_img, cv::Size(resize_w, resize_h));
// divide padding into 2 sides
float half_h = pad_h * 1.0 / 2;
float half_w = pad_w * 1.0 / 2;
paddings[1] = half_h;
paddings[2] = half_w;
// Compute padding boarder
int top = int(round(half_h - 0.1));
int bottom = int(round(half_h + 0.1));
int left = int(round(half_w - 0.1));
int right = int(round(half_w + 0.1));
// Add border
cv::copyMakeBorder(resized_img, resized_img, top, bottom, left, right, 0, cv::Scalar(114, 114, 114));
return resized_img;
}
int main(int argc, char* argv[]) {
// -------- Get OpenVINO runtime version --------
std::cout << ov::get_openvino_version().description << ':' << ov::get_openvino_version().buildNumber << std::endl;
// -------- Step 1. Initialize OpenVINO Runtime Core --------
ov::Core core;
// -------- Step 2. Compile the Model --------
auto compiled_model = core.compile_model(model_file, "GPU.1"); //GPU.1 is dGPU A770
// -------- Step 3. Create an Inference Request --------
ov::InferRequest infer_request = compiled_model.create_infer_request();
// -------- Step 4. Read a picture file and do the preprocess --------
cv::Mat img = cv::imread(image_file); //Load a picture into memory
std::vector<float> paddings(3); //scale, half_h, half_w
cv::Mat resized_img = letterbox(img, paddings); //resize to (640,640) by letterbox
// BGR->RGB, u8(0-255)->f32(0.0-1.0), HWC->NCHW
cv::Mat blob = cv::dnn::blobFromImage(resized_img, 1 / 255.0, cv::Size(640, 640), cv::Scalar(0, 0, 0), true);
// -------- Step 5. Feed the blob into the input node of YOLOv5 -------
// Get input port for model with one input
auto input_port = compiled_model.input();
// Create tensor from external memory
ov::Tensor input_tensor(input_port.get_element_type(), input_port.get_shape(), blob.ptr(0));
// Set input tensor for model with one input
infer_request.set_input_tensor(input_tensor);
// -------- Step 6. Start inference --------
infer_request.infer();
// -------- Step 7. Get the inference result --------
auto output = infer_request.get_output_tensor(0);
auto output_shape = output.get_shape();
std::cout << "The shape of output tensor:" << output_shape << std::endl;
// 25200 x 85 Matrix
cv::Mat output_buffer(output_shape[1], output_shape[2], CV_32F, output.data());
// -------- Step 8. Post-process the inference result -----------
float conf_threshold = 0.25;
float nms_threshold = 0.5;
std::vector boxes;
std::vector<int> class_ids;
std::vector<float> class_scores;
std::vector<float> confidences;
// cx,cy,w,h,confidence,c1,c2,...c80
for (int i = 0; i < output_buffer.rows; i++) {
float confidence = output_buffer.at<float>(i, 4);
if (confidence < conf_threshold) {
continue;
}
cv::Mat classes_scores = output_buffer.row(i).colRange(5, 85);
cv::Point class_id;
double score;
cv::minMaxLoc(classes_scores, NULL, &score, NULL, &class_id);
// class score: 0~1
if (score > 0.25)
{
float cx = output_buffer.at<float>(i, 0);
float cy = output_buffer.at<float>(i, 1);
float w = output_buffer.at<float>(i, 2);
float h = output_buffer.at<float>(i, 3);
int left = static_cast<int>((cx - 0.5 * w - paddings[2]) / paddings[0]);
int top = static_cast<int>((cy - 0.5 * h - paddings[1]) / paddings[0]);
int width = static_cast<int>(w / paddings[0]);
int height = static_cast<int>(h / paddings[0]);
cv::Rect box;
box.x = left;
box.y = top;
box.width = width;
box.height = height;
boxes.push_back(box);
class_ids.push_back(class_id.x);
class_scores.push_back(score);
confidences.push_back(confidence);
}
}
// NMS
std::vector<int> indices;
cv::dnn::NMSBoxes(boxes, confidences, conf_threshold, nms_threshold, indices);
// -------- Step 8. Visualize the detection results -----------
for (size_t i = 0; i < indices.size(); i++) {
int index = indices[i];
int class_id = class_ids[index];
cv::rectangle(img, boxes[index], colors[class_id % 6], 2, 8);
std::string label = class_names[class_id] + ":" + std::to_string(class_scores[index]);
cv::putText(img, label, cv::Point(boxes[index].tl().x, boxes[index].tl().y - 10), cv::FONT_HERSHEY_SIMPLEX, .5, colors[class_id % 6]);
}
cv::namedWindow("YOLOv5 OpenVINO Inference C++ Demo", cv::WINDOW_AUTOSIZE);
cv::imshow("YOLOv5 OpenVINO Inference C++ Demo", img);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
这个问题是由于OpenVINO推理时发生异常引起的,具体原因需要查看OpenVINO异常的信息。
方法一:可以在运行代码时,直接让代码输出OpenVINO异常的信息,代码修改如下:
try {
infer_request.infer();
} catch (const ov::Exception& e) {
std::cout << "Inference error : " << e.what() << std::endl;
return -1;
}
方法二:也可以在OpenVINO编译模型时,设置异常处理回调函数,代码修改如下:
// Define callback function for exceptions
void exception_callback(const char* msg) {
std::cerr << "Exception raised: " << msg << std::endl;
}
int main(int argc, char* argv[]) {
// -------- Step 1. Initialize OpenVINO Runtime Core --------
ov::Core core;
// Set exception callback function
core.set_exception_callback(exception_callback);
...
}
这样能够方便地获取OpenVINO推理过程中发生异常的详细信息,以便排查问题。
另外,由于OpenVINO的版本和硬件环境等因素的影响,可能需要调整以下几个参数:
core.compile_model(model_file, "GPU.1")
的第二个参数(例如需要改成 "CPU");inference_engine_cpu_extension
函数的参数,可以根据不同的硬件环境进行调整以获得更好的性能表现;letterbox
函数中的 new_shape 参数和 paddings 数组,可以根据具体的模型进行调整以满足输入尺寸的要求。希望能够帮到你!
如果我的回答解决了您的问题,请采纳!
1.yolov5s.xml不存在或路径不正确;
2.zidane.jpg不存在或路径不正确;
3.加载模型文件失败;
你加try-catch块来捕获ov::Exception异常看具体啥问题
这个异常信息是由OpenVINO库抛出的,可能是因为程序在运行中发生了错误。从错误信息来看,似乎有一个名为ie的局部变量,在抛出异常时没有显示任何符号信息。这可能意味着您的程序中没有正确加载OpenVINO库或者OpenVINO库版本与您编译的程序不兼容。
以下是一些可能有用的解决方案:
确认程序正确加载了OpenVINO库。可以检查代码中是否有正确的引用和初始化OpenVINO库的语句。
检查您使用的OpenVINO库版本是否与您编译程序时使用的版本匹配。如果版本不匹配,则可能会导致类似的异常。
建议采用最新版本的OpenVINO库,以确保兼容性和稳定性。
估计是你的dll在当前调试环境下找不到了,
要么你把openvino.dll的路径放到系统库的环境变量里,要么你把他放到当前调试环境中/X64/Release或Debug模式下,如下,不过这种方式并不建议采用,除非需要打包可执行程序
该回答通过自己思路及引用到搜索引擎搜索,得到如下:
这个错误发生在内存位置0x00000001000FF650,是由openvino.dll引起的。局部变量ie没有可用的信息,因为没有为openvino.dll加载任何符号。可能是OpenVINO Runtime Core的版本与编译的模型不兼容导致的。建议检查OpenVINO Runtime Core的版本和编译的模型是否匹配,并尝试更新或重新编译模型。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢
以下内容部分参考ChatGPT模型:
这个问题可能是由于OpenVINO库没有正确加载导致的,建议您检查您的代码是否正确配置了OpenVINO库路径和版本,并且库文件是否存在。您可以尝试重新安装OpenVINO并重新配置环境变量,然后重新编译和运行代码。如果问题仍然存在,请尝试在代码中添加错误处理代码以获取更多详细信息,例如:
try {
// your code
} catch (const std::exception& error) {
std::cerr << "Error: " << error.what() << std::endl;
}
这样可以捕获OpenVINO库的错误信息,帮助您更好地找出问题所在。
如果我的建议对您有帮助、请点击采纳、祝您生活愉快