Update demo.cpp
Added HTTP stream over multi-client MJPEG sever, added ESC Key quit and resizable cv window
This commit is contained in:
+85
-63
@@ -1,8 +1,9 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdlib.h> /* srand, rand */
|
#include <stdlib.h> /* srand, rand */
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <https_stream.h> //https_stream
|
||||||
|
|
||||||
#include "CenternetDetection.h"
|
#include "CenternetDetection.h"
|
||||||
#include "MobilenetDetection.h"
|
#include "MobilenetDetection.h"
|
||||||
@@ -11,61 +12,65 @@
|
|||||||
bool gRun;
|
bool gRun;
|
||||||
bool SAVE_RESULT = false;
|
bool SAVE_RESULT = false;
|
||||||
|
|
||||||
void sig_handler(int signo) {
|
void sig_handler(int signo)
|
||||||
std::cout<<"request gateway stop\n";
|
{
|
||||||
|
std::cout << "request gateway stop\n";
|
||||||
gRun = false;
|
gRun = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
|
||||||
std::cout<<"detection\n";
|
std::cout << "detection\n";
|
||||||
signal(SIGINT, sig_handler);
|
signal(SIGINT, sig_handler);
|
||||||
|
|
||||||
|
|
||||||
std::string net = "yolo3_berkeley.rt";
|
std::string net = "yolo3_berkeley.rt";
|
||||||
if(argc > 1)
|
if (argc > 1)
|
||||||
net = argv[1];
|
net = argv[1];
|
||||||
std::string input = "../demo/yolo_test.mp4";
|
std::string input = "../demo/yolo_test.mp4";
|
||||||
if(argc > 2)
|
if (argc > 2)
|
||||||
input = argv[2];
|
input = argv[2];
|
||||||
char ntype = 'y';
|
char ntype = 'y';
|
||||||
if(argc > 3)
|
if (argc > 3)
|
||||||
ntype = argv[3][0];
|
ntype = argv[3][0];
|
||||||
int n_classes = 80;
|
int n_classes = 80;
|
||||||
if(argc > 4)
|
if (argc > 4)
|
||||||
n_classes = atoi(argv[4]);
|
n_classes = atoi(argv[4]);
|
||||||
int n_batch = 1;
|
int n_batch = 1;
|
||||||
if(argc > 5)
|
if (argc > 5)
|
||||||
n_batch = atoi(argv[5]);
|
n_batch = atoi(argv[5]);
|
||||||
bool show = true;
|
bool show = true;
|
||||||
if(argc > 6)
|
if (argc > 6)
|
||||||
show = atoi(argv[6]);
|
show = atoi(argv[6]);
|
||||||
|
int port = 0;
|
||||||
|
if (argc > 7)
|
||||||
|
port = atoi(argv[7]);
|
||||||
|
|
||||||
if(n_batch < 1 || n_batch > 64)
|
if (n_batch < 1 || n_batch > 64)
|
||||||
FatalError("Batch dim not supported");
|
FatalError("Batch dim not supported");
|
||||||
|
|
||||||
if(!show)
|
if (!show)
|
||||||
SAVE_RESULT = true;
|
SAVE_RESULT = true;
|
||||||
|
|
||||||
tk::dnn::Yolo3Detection yolo;
|
tk::dnn::Yolo3Detection yolo;
|
||||||
tk::dnn::CenternetDetection cnet;
|
tk::dnn::CenternetDetection cnet;
|
||||||
tk::dnn::MobilenetDetection mbnet;
|
tk::dnn::MobilenetDetection mbnet;
|
||||||
|
|
||||||
tk::dnn::DetectionNN *detNN;
|
tk::dnn::DetectionNN *detNN;
|
||||||
|
|
||||||
switch(ntype)
|
switch (ntype)
|
||||||
{
|
{
|
||||||
case 'y':
|
case 'y':
|
||||||
detNN = &yolo;
|
detNN = &yolo;
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
detNN = &cnet;
|
detNN = &cnet;
|
||||||
break;
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
detNN = &mbnet;
|
detNN = &mbnet;
|
||||||
n_classes++;
|
n_classes++;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
FatalError("Network type not allowed (3rd parameter)\n");
|
FatalError("Network type not allowed (3rd parameter)\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,66 +79,83 @@ int main(int argc, char *argv[]) {
|
|||||||
gRun = true;
|
gRun = true;
|
||||||
|
|
||||||
cv::VideoCapture cap(input);
|
cv::VideoCapture cap(input);
|
||||||
if(!cap.isOpened())
|
if (!cap.isOpened())
|
||||||
gRun = false;
|
gRun = false;
|
||||||
else
|
else
|
||||||
std::cout<<"camera started\n";
|
std::cout << "camera started\n";
|
||||||
|
|
||||||
cv::VideoWriter resultVideo;
|
cv::VideoWriter resultVideo;
|
||||||
if(SAVE_RESULT) {
|
if (SAVE_RESULT)
|
||||||
|
{
|
||||||
int w = cap.get(cv::CAP_PROP_FRAME_WIDTH);
|
int w = cap.get(cv::CAP_PROP_FRAME_WIDTH);
|
||||||
int h = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
|
int h = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
|
||||||
resultVideo.open("result.mp4", cv::VideoWriter::fourcc('M','P','4','V'), 30, cv::Size(w, h));
|
resultVideo.open("result.mp4", cv::VideoWriter::fourcc('M', 'P', '4', 'V'), 30, cv::Size(w, h));
|
||||||
}
|
}
|
||||||
|
|
||||||
cv::Mat frame;
|
cv::Mat frame;
|
||||||
if(show)
|
if (show)
|
||||||
cv::namedWindow("detection", cv::WINDOW_NORMAL);
|
cv::namedWindow("detection", cv::WINDOW_NORMAL);
|
||||||
|
cv::moveWindow("detection", 0, 0);
|
||||||
|
cv::resizeWindow("detection", 1352, 1013);
|
||||||
|
|
||||||
std::vector<cv::Mat> batch_frame;
|
std::vector<cv::Mat> batch_frame;
|
||||||
std::vector<cv::Mat> batch_dnn_input;
|
std::vector<cv::Mat> batch_dnn_input;
|
||||||
|
|
||||||
while(gRun) {
|
while (gRun)
|
||||||
|
{
|
||||||
batch_dnn_input.clear();
|
batch_dnn_input.clear();
|
||||||
batch_frame.clear();
|
batch_frame.clear();
|
||||||
|
|
||||||
for(int bi=0; bi< n_batch; ++bi){
|
for (int bi = 0; bi < n_batch; ++bi)
|
||||||
cap >> frame;
|
{
|
||||||
if(!frame.data)
|
cap >> frame;
|
||||||
|
if (!frame.data)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
batch_frame.push_back(frame);
|
batch_frame.push_back(frame);
|
||||||
|
|
||||||
// this will be resized to the net format
|
// this will be resized to the net format
|
||||||
batch_dnn_input.push_back(frame.clone());
|
batch_dnn_input.push_back(frame.clone());
|
||||||
}
|
}
|
||||||
if(!frame.data)
|
if (!frame.data)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//inference
|
//inference
|
||||||
detNN->update(batch_dnn_input, n_batch);
|
detNN->update(batch_dnn_input, n_batch);
|
||||||
detNN->draw(batch_frame);
|
detNN->draw(batch_frame);
|
||||||
|
|
||||||
if(show){
|
if (show)
|
||||||
for(int bi=0; bi< n_batch; ++bi){
|
{
|
||||||
|
for (int bi = 0; bi < n_batch; ++bi)
|
||||||
|
{
|
||||||
cv::imshow("detection", batch_frame[bi]);
|
cv::imshow("detection", batch_frame[bi]);
|
||||||
cv::waitKey(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(n_batch == 1 && SAVE_RESULT)
|
if (cv::waitKey(1) == 27)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (n_batch == 1 && SAVE_RESULT)
|
||||||
resultVideo << frame;
|
resultVideo << frame;
|
||||||
|
|
||||||
|
if (port > 0)
|
||||||
|
{
|
||||||
|
send_mjpeg(batch_frame[0], port, 400000, 40);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout<<"detection end\n";
|
std::cout << "detection end\n";
|
||||||
double mean = 0;
|
double mean = 0;
|
||||||
|
|
||||||
std::cout<<COL_GREENB<<"\n\nTime stats:\n";
|
std::cout << COL_GREENB << "\n\nTime stats:\n";
|
||||||
std::cout<<"Min: "<<*std::min_element(detNN->stats.begin(), detNN->stats.end())/n_batch<<" ms\n";
|
std::cout << "Min: " << *std::min_element(detNN->stats.begin(), detNN->stats.end()) / n_batch << " ms\n";
|
||||||
std::cout<<"Max: "<<*std::max_element(detNN->stats.begin(), detNN->stats.end())/n_batch<<" ms\n";
|
std::cout << "Max: " << *std::max_element(detNN->stats.begin(), detNN->stats.end()) / n_batch << " ms\n";
|
||||||
for(int i=0; i<detNN->stats.size(); i++) mean += detNN->stats[i]; mean /= detNN->stats.size();
|
for (int i = 0; i < detNN->stats.size(); i++)
|
||||||
std::cout<<"Avg: "<<mean/n_batch<<" ms\t"<<1000/(mean/n_batch)<<" FPS\n"<<COL_END;
|
mean += detNN->stats[i];
|
||||||
|
mean /= detNN->stats.size();
|
||||||
|
std::cout << "Avg: " << mean / n_batch << " ms\t" << 1000 / (mean / n_batch) << " FPS\n"
|
||||||
|
<< COL_END;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user