提交 1d6152aa authored 作者: blu's avatar blu

yolo

上级 ef4edde0
...@@ -19,16 +19,6 @@ using namespace cv; ...@@ -19,16 +19,6 @@ using namespace cv;
using namespace dnn; using namespace dnn;
using namespace std; using namespace std;
// Remove the bounding boxes with low confidence using non-maxima suppression
int postprocess(Mat& frame, const vector<Mat>& out);
// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame);
// Get the names of the output layers
vector<String> getOutputsNames(const Net& net);
class YoloDectect { class YoloDectect {
private: private:
// Initialize the parameters // Initialize the parameters
...@@ -138,7 +128,7 @@ protected: ...@@ -138,7 +128,7 @@ protected:
// //
public: public:
typedef int (*callback)(vector<tuple<string, double, Rect>>, Mat); typedef int (*callback)(vector<tuple<string, double, Rect>>&, Mat);
YoloDectect(string path = "") YoloDectect(string path = "")
{ {
if(path.empty()) { if(path.empty()) {
...@@ -198,13 +188,17 @@ public: ...@@ -198,13 +188,17 @@ public:
return ret; return ret;
} }
int process(string inVideoUri, string outFile = "processed.jpg", callback cb = nullptr)
int process(string inVideoUri, callback cb = nullptr, string outFile = "processed.jpg")
{ {
if(inVideoUri.empty()) { if(inVideoUri.empty()) {
inVideoUri = "0"; inVideoUri = "0";
} }
if(!cap.open(inVideoUri)) {
spdlog::error("{} failed to open input video {}", selfId, inVideoUri);
return -1;
}
ghc::filesystem::path p(outFile); ghc::filesystem::path p(outFile);
auto dir = p.parent_path(); auto dir = p.parent_path();
...@@ -216,16 +210,11 @@ public: ...@@ -216,16 +210,11 @@ public:
else { else {
bOutputIsImg = false; bOutputIsImg = false;
if(!video.open(outFile, VideoWriter::fourcc('M','J','P','G'), 28, Size(cap.get(CAP_PROP_FRAME_WIDTH), cap.get(CAP_PROP_FRAME_HEIGHT)))) { if(!video.open(outFile, VideoWriter::fourcc('M','J','P','G'), 28, Size(cap.get(CAP_PROP_FRAME_WIDTH), cap.get(CAP_PROP_FRAME_HEIGHT)))) {
spdlog::error("{} failed to open output video {}", selfId, inVideoUri); spdlog::error("{} failed to open output video {}", selfId, outFile);
return -1; return -1;
} }
} }
if(!cap.open(inVideoUri)) {
spdlog::error("{} failed to open input video {}", selfId, inVideoUri);
return -1;
}
spdlog::info("{} try to process video {} to {}", selfId, inVideoUri, outFile); spdlog::info("{} try to process video {} to {}", selfId, inVideoUri, outFile);
long frameCnt = 0; long frameCnt = 0;
...@@ -250,24 +239,27 @@ public: ...@@ -250,24 +239,27 @@ public:
} }
vector<tuple<string, double, Rect>> ret = process(frame, outFrame); vector<tuple<string, double, Rect>> ret = process(frame, outFrame);
if(ret.size() == 0 && bOutputIsImg) { if(cb == nullptr) {
// no detection if(ret.size() == 0 && bOutputIsImg) {
if(skipCnt % 100 == 0) { // no detection
spdlog::info("{} no valid object detected skipped frame count {}", selfId, skipCnt); if(skipCnt % 100 == 0) {
spdlog::info("{} no valid object detected skipped frame count {}", selfId, skipCnt);
}
skipCnt++;
continue;
} }
skipCnt++;
continue;
}
if (bOutputIsImg) { if (bOutputIsImg) {
string ofname = outFileBase + to_string(detCnt) + ".jpg"; string ofname = outFileBase + to_string(detCnt) + ".jpg";
imwrite(ofname, outFrame); imwrite(ofname, outFrame);
detCnt++; detCnt++;
} }
else { else {
video.write(outFrame); video.write(outFrame);
}
}else{
cb(ret, outFrame);
} }
} }
cap.release(); cap.release();
...@@ -280,7 +272,7 @@ public: ...@@ -280,7 +272,7 @@ public:
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
YoloDectect det; YoloDectect det;
det.process("rtsp://admin:ZQEAAI@192.168.0.101:554/h264/ch1/main/av_stream"); det.process("rtsp://admin:ZQEAAI@192.168.0.101:554/h264/ch1/main/av_stream", "a.avi");
return 0; return 0;
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论