提交 b5bb0ed3 authored 作者: blu's avatar blu

object detection

上级 8fab9d1d
...@@ -13,5 +13,8 @@ all: $(PROG) $(PROG2) ...@@ -13,5 +13,8 @@ all: $(PROG) $(PROG2)
$(PROG):$(SRCS) $(PROG):$(SRCS)
$(CC) $(CFLAGS) -o $(PROG) $(SRCS) $(LIBS) $(CC) $(CFLAGS) -o $(PROG) $(SRCS) $(LIBS)
$(PROG2):$(SRCS2) $(PROG2):$(SRCS2) yolo.hpp
$(CC) $(CFLAGS) -o $(PROG2) $(SRCS2) $(LIBS) -I../opencv-motion-detect/inc -I../opencv-motion-detect/vendor/include $(CC) $(CFLAGS) -o $(PROG2) $(SRCS2) $(LIBS) -I../opencv-motion-detect/inc -I../opencv-motion-detect/vendor/include
clean:
rm -fr main yolo
\ No newline at end of file
差异被折叠。
...@@ -28,8 +28,8 @@ public: ...@@ -28,8 +28,8 @@ public:
private: private:
// Initialize the parameters // Initialize the parameters
const string selfId = "YoloDetector"; const string selfId = "YoloDetector";
float confThreshold = 0.5; // Confidence threshold float confThreshold = 0.1; // Confidence threshold
float nmsThreshold = 0.4; // Non-maximum suppression threshold float nmsThreshold = 0.2; // Non-maximum suppression threshold
int inpWidth = 416; // Width of network's input image int inpWidth = 416; // Width of network's input image
int inpHeight = 416; // Height of network's input image int inpHeight = 416; // Height of network's input image
vector<string> classes; vector<string> classes;
...@@ -38,10 +38,14 @@ private: ...@@ -38,10 +38,14 @@ private:
VideoCapture cap; VideoCapture cap;
VideoWriter video; VideoWriter video;
bool bOutputIsImg = false; bool bOutputIsImg = false;
bool bInputIsImage = true;
string outFileBase; string outFileBase;
bool cmdStop = false; bool cmdStop = false;
unsigned int wrapNum = 0; unsigned int wrapNum = 0;
unsigned int numLogSkip = 0; unsigned int numLogSkip = 0;
bool bHumanOnly = false;
bool bContinue = true;
int cameNo = -1;
// Get the names of the output layers // Get the names of the output layers
vector<String> getOutputsNames(const Net& net) vector<String> getOutputsNames(const Net& net)
...@@ -109,6 +113,12 @@ private: ...@@ -109,6 +113,12 @@ private:
int left = centerX - width / 2; int left = centerX - width / 2;
int top = centerY - height / 2; int top = centerY - height / 2;
if(bHumanOnly){
if(classes[classIdPoint.x] != "person"){
continue;
}
}
classIds.push_back(classIdPoint.x); classIds.push_back(classIdPoint.x);
confidences.push_back((float)confidence); confidences.push_back((float)confidence);
boxes.push_back(Rect(left, top, width, height)); boxes.push_back(Rect(left, top, width, height));
...@@ -137,12 +147,17 @@ protected: ...@@ -137,12 +147,17 @@ protected:
// //
public: public:
typedef int (*callback)(vector<tuple<string, double, Rect>>&, Mat); typedef int (*callback)(vector<tuple<string, double, Rect>>&, Mat);
YoloDectect(string path = ".", unsigned int _wrapNum = 10, unsigned int _numLogSkip = 380) YoloDectect(string path = ".", bool _humanOnly = false, float confThresh = 0.1, bool _bContinue = true, unsigned int _wrapNum = 10, unsigned int _numLogSkip = 380)
{ {
if(path.empty()) { if(path.empty()) {
path = "."; path = ".";
} }
bHumanOnly = _humanOnly;
bContinue = _bContinue;
confThreshold = confThresh;
wrapNum = _wrapNum; wrapNum = _wrapNum;
numLogSkip = _numLogSkip; numLogSkip = _numLogSkip;
...@@ -167,7 +182,7 @@ public: ...@@ -167,7 +182,7 @@ public:
net = readNetFromDarknet(modCfg, modWeights); net = readNetFromDarknet(modCfg, modWeights);
net.setPreferableBackend(DNN_BACKEND_OPENCV); net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU); net.setPreferableTarget(DNN_TARGET_CPU);
spdlog::info("{} inited", selfId); spdlog::debug("{} inited", selfId);
} }
vector<tuple<string, double, Rect>> process(Mat &inFrame, Mat* pOutFrame, bool bModify = false) vector<tuple<string, double, Rect>> process(Mat &inFrame, Mat* pOutFrame, bool bModify = false)
...@@ -194,7 +209,7 @@ public: ...@@ -194,7 +209,7 @@ public:
if(numLogSkip == 0 || numFrameProcessed % numLogSkip == 0) { if(numLogSkip == 0 || numFrameProcessed % numLogSkip == 0) {
double freq = getTickFrequency() / 1000; double freq = getTickFrequency() / 1000;
double t = net.getPerfProfile(layersTimes) / freq; double t = net.getPerfProfile(layersTimes) / freq;
spdlog::info("{} infer time: {} ms", selfId, t); spdlog::debug("{} infer time: {} ms", selfId, t);
} }
if(pOutFrame != nullptr){ if(pOutFrame != nullptr){
inFrame.convertTo(*pOutFrame, CV_8U); inFrame.convertTo(*pOutFrame, CV_8U);
...@@ -204,15 +219,27 @@ public: ...@@ -204,15 +219,27 @@ public:
return ret; return ret;
} }
int process(string inVideoUri, string outFile = "processed.jpg", bool bHumanExit = false, callback cb = nullptr) int process(string inVideoUri, string outFile = "processed.jpg", callback cb = nullptr)
{ {
if(inVideoUri.empty()) { if(inVideoUri.empty()) {
inVideoUri = "0"; inVideoUri = "0";
} }
if(!cap.open(inVideoUri)) { try{
if(inVideoUri.substr(inVideoUri.find_last_of(".") + 1) == "mp4"||(cameNo = stoi(inVideoUri)) >= 0) {
bInputIsImage = false;
}
}catch(Exception &e) {
}
if(!bInputIsImage) {
if((cameNo == -1 && !cap.open(inVideoUri))|| (cameNo != -1 && !cap.open(cameNo)))
{
spdlog::error("{} failed to open input video {}", selfId, inVideoUri); spdlog::error("{} failed to open input video {}", selfId, inVideoUri);
return -1; exit(1);
}
} }
ghc::filesystem::path p(outFile); ghc::filesystem::path p(outFile);
...@@ -221,9 +248,14 @@ public: ...@@ -221,9 +248,14 @@ public:
if((outFile.substr(outFile.find_last_of(".") + 1) == "jpg")) { if((outFile.substr(outFile.find_last_of(".") + 1) == "jpg")) {
bOutputIsImg = true; bOutputIsImg = true;
outFileBase = string(dir / p.stem()); outFileBase = string(dir / p.stem());
spdlog::info("{} outFileBase {}", selfId, outFileBase); spdlog::debug("{} outFileBase {}", selfId, outFileBase);
} }
else { else {
if(bInputIsImage) {
spdlog::error("{} can't output image {} as video {}, invalid params combination", selfId, inVideoUri, outFile);
exit(1);
}
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, outFile); spdlog::error("{} failed to open output video {}", selfId, outFile);
...@@ -231,7 +263,7 @@ public: ...@@ -231,7 +263,7 @@ public:
} }
} }
spdlog::info("{} try to process video {} to {}", selfId, inVideoUri, outFile); spdlog::debug("{} try to process video {} to {}", selfId, inVideoUri, outFile);
unsigned long frameCnt = 0; unsigned long frameCnt = 0;
unsigned long detCnt = 0, skipCnt = 0; unsigned long detCnt = 0, skipCnt = 0;
...@@ -242,14 +274,22 @@ public: ...@@ -242,14 +274,22 @@ public:
break; break;
} }
if(bInputIsImage){
frame = imread(inVideoUri);
if(!frame.data){
spdlog::error("{} failed to read image {}", selfId, inVideoUri);
exit(1);
}
cmdStop = true;
}
else{
if(!cap.read(frame)) { if(!cap.read(frame)) {
spdlog::info("{} done reading frame from {}", selfId, inVideoUri);
break; break;
} }
frameCnt++; frameCnt++;
if(frameCnt %100 == 0) if(frameCnt %100 == 0)
spdlog::info("framecnt {}", frameCnt); spdlog::debug("framecnt {}", frameCnt);
if(frameCnt % 30 != 0 ){ if(frameCnt % 30 != 0 ){
continue; continue;
...@@ -259,38 +299,42 @@ public: ...@@ -259,38 +299,42 @@ public:
if (frame.empty()) { if (frame.empty()) {
continue; continue;
} }
}
vector<tuple<string, double, Rect>> ret = process(frame, &outFrame, true); vector<tuple<string, double, Rect>> ret = process(frame, &outFrame, true);
if(cb == nullptr) { if(cb == nullptr) {
if(ret.size() == 0 && bOutputIsImg) { if(ret.size() == 0 && bOutputIsImg) {
// no detection // no detection
if(numLogSkip == 0|| skipCnt % numLogSkip == 0) { if(numLogSkip == 0|| skipCnt % numLogSkip == 0) {
spdlog::info("{} no valid object detected skipped frame count {}", selfId, skipCnt); spdlog::debug("{} no valid object detected skipped frame count {}", selfId, skipCnt);
} }
skipCnt++; skipCnt++;
continue; continue;
} }
if(bHumanExit){
if (bOutputIsImg) {
if(bHumanOnly){
for(auto &[s, c, r]:ret) { for(auto &[s, c, r]:ret) {
if (s == "person"){ if (s == "person"){
string ofname = outFileBase + "_person.jpg"; auto ms = chrono::duration_cast<chrono::milliseconds >(chrono::system_clock::now().time_since_epoch()).count();
string ofname = outFileBase + "_person_" + to_string(ms) + ".jpg";
imwrite(ofname, outFrame); imwrite(ofname, outFrame);
spdlog::info("found human {} x: {}, y: {}, w: {}, h: {}", c, r.x, r.y, r.width, r.height); spdlog::info("found human {} x: {}, y: {}, w: {}, h: {}", c, r.x, r.y, r.width, r.height);
if(!bContinue){
cmdStop = true; cmdStop = true;
break; break;
} }
} }
} }
}else{
if (bOutputIsImg) {
if(wrapNum > 0) { if(wrapNum > 0) {
detCnt = detCnt % wrapNum; detCnt = detCnt % wrapNum;
} }
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);
} }
...@@ -299,6 +343,7 @@ public: ...@@ -299,6 +343,7 @@ public:
} }
} }
spdlog::info("{} done processing {}", selfId, inVideoUri);
cap.release(); cap.release();
if(!bOutputIsImg) video.release(); if(!bOutputIsImg) video.release();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论