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 <signal.h>
|
||||
#include <stdlib.h> /* srand, rand */
|
||||
#include <stdlib.h> /* srand, rand */
|
||||
#include <unistd.h>
|
||||
#include <mutex>
|
||||
#include <https_stream.h> //https_stream
|
||||
|
||||
#include "CenternetDetection.h"
|
||||
#include "MobilenetDetection.h"
|
||||
@@ -11,61 +12,65 @@
|
||||
bool gRun;
|
||||
bool SAVE_RESULT = false;
|
||||
|
||||
void sig_handler(int signo) {
|
||||
std::cout<<"request gateway stop\n";
|
||||
void sig_handler(int signo)
|
||||
{
|
||||
std::cout << "request gateway stop\n";
|
||||
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);
|
||||
|
||||
|
||||
std::string net = "yolo3_berkeley.rt";
|
||||
if(argc > 1)
|
||||
net = argv[1];
|
||||
if (argc > 1)
|
||||
net = argv[1];
|
||||
std::string input = "../demo/yolo_test.mp4";
|
||||
if(argc > 2)
|
||||
input = argv[2];
|
||||
if (argc > 2)
|
||||
input = argv[2];
|
||||
char ntype = 'y';
|
||||
if(argc > 3)
|
||||
ntype = argv[3][0];
|
||||
if (argc > 3)
|
||||
ntype = argv[3][0];
|
||||
int n_classes = 80;
|
||||
if(argc > 4)
|
||||
n_classes = atoi(argv[4]);
|
||||
if (argc > 4)
|
||||
n_classes = atoi(argv[4]);
|
||||
int n_batch = 1;
|
||||
if(argc > 5)
|
||||
n_batch = atoi(argv[5]);
|
||||
if (argc > 5)
|
||||
n_batch = atoi(argv[5]);
|
||||
bool show = true;
|
||||
if(argc > 6)
|
||||
show = atoi(argv[6]);
|
||||
if (argc > 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");
|
||||
|
||||
if(!show)
|
||||
if (!show)
|
||||
SAVE_RESULT = true;
|
||||
|
||||
tk::dnn::Yolo3Detection yolo;
|
||||
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':
|
||||
detNN = &yolo;
|
||||
break;
|
||||
case 'c':
|
||||
detNN = &cnet;
|
||||
break;
|
||||
case 'm':
|
||||
detNN = &mbnet;
|
||||
n_classes++;
|
||||
break;
|
||||
default:
|
||||
case 'y':
|
||||
detNN = &yolo;
|
||||
break;
|
||||
case 'c':
|
||||
detNN = &cnet;
|
||||
break;
|
||||
case 'm':
|
||||
detNN = &mbnet;
|
||||
n_classes++;
|
||||
break;
|
||||
default:
|
||||
FatalError("Network type not allowed (3rd parameter)\n");
|
||||
}
|
||||
|
||||
@@ -74,66 +79,83 @@ int main(int argc, char *argv[]) {
|
||||
gRun = true;
|
||||
|
||||
cv::VideoCapture cap(input);
|
||||
if(!cap.isOpened())
|
||||
gRun = false;
|
||||
if (!cap.isOpened())
|
||||
gRun = false;
|
||||
else
|
||||
std::cout<<"camera started\n";
|
||||
std::cout << "camera started\n";
|
||||
|
||||
cv::VideoWriter resultVideo;
|
||||
if(SAVE_RESULT) {
|
||||
if (SAVE_RESULT)
|
||||
{
|
||||
int w = cap.get(cv::CAP_PROP_FRAME_WIDTH);
|
||||
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;
|
||||
if(show)
|
||||
if (show)
|
||||
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_dnn_input;
|
||||
|
||||
while(gRun) {
|
||||
while (gRun)
|
||||
{
|
||||
batch_dnn_input.clear();
|
||||
batch_frame.clear();
|
||||
|
||||
for(int bi=0; bi< n_batch; ++bi){
|
||||
cap >> frame;
|
||||
if(!frame.data)
|
||||
|
||||
for (int bi = 0; bi < n_batch; ++bi)
|
||||
{
|
||||
cap >> frame;
|
||||
if (!frame.data)
|
||||
break;
|
||||
|
||||
|
||||
batch_frame.push_back(frame);
|
||||
|
||||
// this will be resized to the net format
|
||||
batch_dnn_input.push_back(frame.clone());
|
||||
}
|
||||
if(!frame.data)
|
||||
}
|
||||
if (!frame.data)
|
||||
break;
|
||||
|
||||
|
||||
//inference
|
||||
detNN->update(batch_dnn_input, n_batch);
|
||||
detNN->draw(batch_frame);
|
||||
|
||||
if(show){
|
||||
for(int bi=0; bi< n_batch; ++bi){
|
||||
if (show)
|
||||
{
|
||||
for (int bi = 0; bi < n_batch; ++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;
|
||||
|
||||
if (port > 0)
|
||||
{
|
||||
send_mjpeg(batch_frame[0], port, 400000, 40);
|
||||
}
|
||||
}
|
||||
|
||||
std::cout<<"detection end\n";
|
||||
double mean = 0;
|
||||
|
||||
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<<"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();
|
||||
std::cout<<"Avg: "<<mean/n_batch<<" ms\t"<<1000/(mean/n_batch)<<" FPS\n"<<COL_END;
|
||||
|
||||
std::cout << "detection end\n";
|
||||
double mean = 0;
|
||||
|
||||
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 << "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();
|
||||
std::cout << "Avg: " << mean / n_batch << " ms\t" << 1000 / (mean / n_batch) << " FPS\n"
|
||||
<< COL_END;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user