Object detection
Hi, I am trying to implement an object detection algorithm with the camera-stream-callback-sample.cpp sample code. However, I encountered an error while trying to call the trained model. Does anyhow have any experience with it and how do I get it to work?
Thanks.
These are my codes that I have added:
#include "dji_vehicle.hpp"
#include "dji_linux_helpers.hpp"
#include <iostream>
#include <pthread.h>
#ifdef OPEN_CV_INSTALLED
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/dnn/all_layers.hpp"
#include "opencv2/dnn.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace dnn;
#endif
using namespace DJI::OSDK;
using namespace std;
void show_rgb(CameraRGBImage img, void *p)
{
const char winName[]="My Camera";
std::vector<std::string> class_names;
ifstream ifs(string("/home/dji/Desktop/Object_Detection/Labels.txt").c_str());
string line;
while (getline(ifs, line))
{
class_names.push_back(line);
}
auto model = readNetFromTensorflow("/home/mobileNet/ssd_mobilenet_v2_coco_2018_03_29/frozen_inference_graph.pb", "/home/mobileNet/ssd_mobilenet_v2_coco_2018_03_29.pbtxt.txt");
#ifdef OPEN_CV_INSTALLED
Mat mat(img.height, img.width, CV_8UC3, img.rawData.data(), img.width*3);
Mat blob = blobFromImage(mat, 1.0, Size(300, 300), Scalar(127.5, 127.5, 127.5), true, false);
model.setInput(blob);
Mat output = model.forward();
Mat detectionMat(output.size[2], output.size[3], CV_32F, output.ptr<float>());
for (int i = 0; i < detectionMat.rows; i++)
{
int class_id = detectionMat.at<float>(i, 1);
float confidence = detectionMat.at<float>(i, 2);
if(confidence > 0.5)
{
int box_x = static_cast<int>(detectionMat.at<float>(i, 3) * mat.cols);
int box_y = static_cast<int>(detectionMat.at<float>(i, 4) * mat.rows);
int box_width = static_cast<int>(detectionMat.at<float>(i, 5) * mat.cols - box_x);
int box_height = static_cast<int>(detectionMat.at<float>(i, 6) * mat.rows - box_y);
rectangle(mat, Point(box_x, box_y), Point((box_x + box_width), (box_y+box_height)), Scalar(255, 255, 255), 2);
putText(mat, class_names[class_id - 1].c_str(), Point(box_x, (box_y-5)), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 255), 1);
}
}
cvtColor(mat, mat, COLOR_RGB2BGR);
imshow(winName,mat);
cv::waitKey(1);
#endif
}
and the errors I have encountered:
OpenCV Error: Unspecified error (FAILED: fs.is_open(). Can't open "/home/mobileNet/ssd_mobilenet_v2_coco_2018_03_29/frozen_inference_graph.pb") in ReadProtoFromBinaryFile, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/dnn/src/caffe/caffe_io.cpp, line 1126
terminate called after throwing an instance of 'cv::Exception'
what(): /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/dnn/src/caffe/caffe_io.cpp:1126: error: (-2) FAILED: fs.is_open(). Can't open "/home/mobileNet/ssd_mobilenet_v2_coco_2018_03_29/frozen_inference_graph.pb" in function ReadProtoFromBinaryFile
Agent comment from DJI SDK in Zendesk ticket #53872:
尊敬的开发者,感谢您联系DJI 大疆创新
由于github不是我们主要的咨询渠道,您的问题可能跟进不及时。我们建议您通过填写表单( https://djisdksupport.zendesk.com/hc/zh-cn/requests/new )向我们反馈问题。或者您也可以在论坛发帖,与其它开发者交流。论坛链接:https://djisdksupport.zendesk.com/hc/zh-cn/community/topics
Dear developer, thank you for contacting DJI.
Since github is not our main consultation channel, your questions may not be followed up in time. We recommend that you fill in the form (https://djisdksupport.zendesk.com/hc/en-us/requests/new) to report problems to us. Or you can post in the forum to communicate with other developers. Forum link: https://djisdksupport.zendesk.com/hc/zh-cn/community/topics
°°°