Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added HTTP STREAM mjpeg server #93

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,18 @@ Once you have succesfully created your rt file, run the demo:
```
In general the demo program takes 6 parameters:
```
./demo <network-rt-file> <path-to-video> <kind-of-network> <number-of-classes> <n-batches> <show-flag>
./demo <network-rt-file> <path-to-video> <kind-of-network> <number-of-classes> <n-batches> <show-flag> <save-flag> <http-stream-port>
```
where
* ```<network-rt-file>``` is the rt file generated by a test
* ```<<path-to-video>``` is the path to a video file or a camera input
* ```<kind-of-network>``` is the type of network. Thee types are currently supported: ```y``` (YOLO family), ```c``` (CenterNet family) and ```m``` (MobileNet-SSD family)
* ```<number-of-classes>```is the number of classes the network is trained on
* ```<n-batches>``` number of batches to use in inference (N.B. you should first export TKDNN_BATCHSIZE to the required n_batches and create again the rt file for the network).
* ```<show-flag>``` if set to 0 the demo will not show the visualization but save the video into result.mp4 (if n-batches ==1)
* ```<show-flag>``` if set to 0 the demo will not show the visualization.
* ```<save-flag>``` if set to 0 the results will be saved into result.mp4 (if n-batches ==1) and if set to 1 then the results will not be saved.
* ```<http-stream-port>``` if set to 0 then http stream will be off but if port number is given eg: 8090 then the stream can seen the results in ```https://localhost:8090```
* ```<extyolo flag>``` if set to 1 then yolo like coordinates of the bounding boxes will be printed on the terminal

N.b. By default it is used FP32 inference

Expand Down
156 changes: 90 additions & 66 deletions demo/demo/demo.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -11,61 +12,68 @@
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(n_batch < 1 || n_batch > 64)
if (argc > 6)
show = atoi(argv[6]);
if (argc > 7)
SAVE_RESULT = atoi(argv[7]);
int port = 0;
if (argc > 8)
port = atoi(argv[8]);
bool extyolo=false;
if(argc >9)
extyolo = atoi(argv[9]);

if (n_batch < 1 || n_batch > 64)
FatalError("Batch dim not supported");

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");
}

Expand All @@ -74,66 +82,82 @@ 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);
detNN->draw(batch_frame,extyolo);

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;
}

3 changes: 1 addition & 2 deletions demo/demo/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ int main(int argc, char *argv[])
//inference
detected_bbox.clear();
detNN->update(batch_dnn_input,1,write_res_on_file, &times, write_coco_json);
detNN->draw(batch_frames);
detNN->draw(batch_frames,true);
detected_bbox = detNN->detected;

if(write_coco_json)
Expand Down Expand Up @@ -232,4 +232,3 @@ int main(int argc, char *argv[])

return 0;
}

Loading