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

Add RTMP stream function with FFMPEG #6735

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f31c6ba
add RTMP stream function with FFMPEG
q414252595 Sep 24, 2020
f24a5fe
add RTMP stream function with FFMPEG
q414252595 Sep 25, 2020
8437b6c
add RTMP stream function with FFMPEG
q414252595 Sep 25, 2020
9009113
minor fix
q414252595 Sep 25, 2020
0c0befc
minor fix
edwardxliu Sep 28, 2020
3aef7cf
minor fix
edwardxliu Sep 28, 2020
6c8d550
Merge pull request #1 from AlexeyAB/master
edwardxliu Sep 28, 2020
0d0225f
add reference
edwardxliu Sep 28, 2020
f68848c
Merge branch 'master' of https://github.com/edwardxliu/darknet
edwardxliu Sep 28, 2020
da4e826
Merge pull request #2 from AlexeyAB/master
edwardxliu Nov 26, 2020
7703fac
reduce latency of input stream from IP camera
edwardxliu Dec 8, 2020
658621f
Update README.md
edwardxliu Dec 8, 2020
91e3d7d
Update README.md
edwardxliu Dec 8, 2020
3f2d3fa
input rtsp stream in stream.cpp with ffmpeg
edwardxliu Dec 14, 2020
7102c70
Merge branch 'master' of https://github.com/edwardxliu/darknet
edwardxliu Dec 14, 2020
50660eb
update
edwardxliu Dec 14, 2020
816210a
update
edwardxliu Dec 14, 2020
e6020cc
Merge pull request #3 from AlexeyAB/master
edwardxliu Dec 14, 2020
4fddf7c
upate
edwardxliu Dec 15, 2020
e6e6f71
Merge branch 'master' of https://github.com/edwardxliu/darknet
edwardxliu Dec 15, 2020
b141cef
minor fix
edwardxliu Dec 15, 2020
3298cbe
update
edwardxliu Dec 15, 2020
2067e38
update
edwardxliu Dec 15, 2020
25ffaf4
fix set-env issue
edwardxliu Dec 15, 2020
22a44da
fix set-env issue
edwardxliu Dec 15, 2020
b1538bf
fix set-env issue
edwardxliu Dec 15, 2020
3dd41fd
Merge branch 'master' into master
cenit Apr 16, 2021
b64f25a
Merge branch 'master' into master
cenit Aug 26, 2023
b9e920d
Merge branch 'master' into pr/6735
cenit Oct 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
594 changes: 594 additions & 0 deletions .github/workflows/ccpp.yml.bk

Large diffs are not rendered by default.

24 changes: 23 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ OPENMP=0
LIBSO=0
ZED_CAMERA=0
ZED_CAMERA_v2_8=0
STREAM=0
FFMPEG=0

# set GPU=1 and CUDNN=1 to speedup on GPU
# set CUDNN_HALF=1 to further speedup 3 x times (Mixed-precision on Tensor Cores) GPU: Volta, Xavier, Turing, Ampere, Ada and higher
Expand Down Expand Up @@ -94,7 +96,21 @@ NVCC=nvcc
OPTS=-Ofast
LDFLAGS= -lm -pthread
COMMON= -Iinclude/ -I3rdparty/stb/include
CFLAGS=-Wall -Wfatal-errors -Wno-unused-result -Wno-unknown-pragmas -fPIC -rdynamic
CFLAGS=-Wall -Wfatal-errors -Wno-unused-result -Wno-unknown-pragmas -fPIC -fpermissive -rdynamic

ifeq ($(STREAM), 1)
COMMON+= -DSTREAM
CFLAGS+= -DSTREAM
LDFLAGS+= `pkg-config --libs libavformat libavcodec libavutil libswscale 2>/dev/null`
COMMON+= `pkg-config --cflags libavformat libavcodec libavutil libswscale 2>/dev/null`
endif

ifeq ($(FFMPEG), 1)
COMMON+= -DFFMPEG
CFLAGS+= -DFFMPEG
LDFLAGS+= `pkg-config --libs libswresample libswscale libavutil libavcodec libavformat 2>/dev/null`
COMMON+= `pkg-config --cflags libswresample libswscale libavutil libavcodec libavformat 2>/dev/null`
endif

ifeq ($(DEBUG), 1)
#OPTS= -O0 -g
Expand Down Expand Up @@ -172,6 +188,12 @@ ifeq ($(GPU), 1)
LDFLAGS+= -lstdc++
OBJ+=convolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o network_kernels.o avgpool_layer_kernels.o
endif
ifeq ($(STREAM), 1)
OBJ+=stream.o streamer.o
endif
ifeq ($(FFMPEG), 1)
OBJ+=image_ffmpeg.o
endif

OBJS = $(addprefix $(OBJDIR), $(OBJ))
DEPS = $(wildcard src/*.h) Makefile include/darknet.h
Expand Down
25 changes: 22 additions & 3 deletions src/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include <sys/time.h>
#endif

#ifdef FFMPEG
#include "image_ffmpeg.h"
#endif

#ifdef OPENCV

#include "http_stream.h"
Expand Down Expand Up @@ -54,6 +58,9 @@ static const int thread_wait_ms = 1;
static volatile int run_fetch_in_thread = 0;
static volatile int run_detect_in_thread = 0;

#ifdef FFMPEG
static int input_is_stream = 0;
#endif

void *fetch_in_thread(void *ptr)
{
Expand All @@ -65,10 +72,16 @@ void *fetch_in_thread(void *ptr)
this_thread_yield();
}
int dont_close_stream = 0; // set 1 if your IP-camera periodically turns off and turns on video-stream
if (letter_box)
if (letter_box){
in_s = get_image_from_stream_letterbox(cap, net.w, net.h, net.c, &in_img, dont_close_stream);
else
}else{
#ifdef FFMPEG
if (input_is_stream) in_s = get_image_from_ffmpeg_stream_resize(&in_img, net.w, net.h, net.c);
else in_s = get_image_from_stream_resize(cap, net.w, net.h, net.c, &in_img, dont_close_stream);
#else
in_s = get_image_from_stream_resize(cap, net.w, net.h, net.c, &in_img, dont_close_stream);
#endif
}
if (!in_s.data) {
printf("Stream closed.\n");
custom_atomic_store_int(&flag_exit, 1);
Expand Down Expand Up @@ -178,6 +191,10 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int

if(filename){
printf("video file: %s\n", filename);
#ifdef FFMPEG
open_video_stream(filename);
input_is_stream = 1;
#endif
cap = get_capture_video_stream(filename);
demo_skip_frame = is_live_stream(filename);
}else{
Expand Down Expand Up @@ -358,7 +375,6 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
write_frame_cv(output_video_writer, show_img);
printf("\n cvWriteFrame \n");
}

while (custom_atomic_load_int(&run_detect_in_thread)) {
if(avg_fps > 50) this_thread_yield();
else this_thread_sleep_for(thread_wait_ms); // custom_join(detect_thread, 0);
Expand Down Expand Up @@ -406,6 +422,9 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
start_time = get_time_point();
}
}
#ifdef FFMPEG
av_pkt_unref();
#endif
}
printf("input video stream closed. \n");
if (output_video_writer) {
Expand Down
33 changes: 29 additions & 4 deletions src/detector.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
#include "demo.h"
#include "option_list.h"

#ifdef STREAM
#include "stream.h"
#endif

#ifdef FFMPEG
#include "image_ffmpeg.h"
#endif

#ifndef __COMPAR_FN_T
#define __COMPAR_FN_T
typedef int (*__compar_fn_t)(const void*, const void*);
Expand Down Expand Up @@ -158,7 +166,6 @@ void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, i
args.mosaic_bound = net.mosaic_bound;
args.contrastive = net.contrastive;
args.contrastive_jit_flip = net.contrastive_jit_flip;
args.contrastive_color = net.contrastive_color;
if (dont_show && show_imgs) show_imgs = 2;
args.show_imgs = show_imgs;

Expand Down Expand Up @@ -1979,6 +1986,15 @@ void run_detector(int argc, char **argv)
int ext_output = find_arg(argc, argv, "-ext_output");
int save_labels = find_arg(argc, argv, "-save_labels");
char* chart_path = find_char_arg(argc, argv, "-chart", 0);

int stream_bitrate = find_int_arg(argc, argv, "-stream_bitrate", 0);
int stream_frame_width = find_int_arg(argc, argv, "-stream_width", 0);
int stream_frame_height = find_int_arg(argc, argv, "-stream_height", 0);
int stream_gop_size = find_int_arg(argc, argv, "-stream_gop", 0);
int stream_fps = find_int_arg(argc, argv, "-stream_fps", 0);
char *stream_addr = find_char_arg(argc, argv, "-stream_address", 0);
char *stream_profile = find_char_arg(argc, argv, "-stream_profile", "high444");

// While training, decide after how many epochs mAP will be calculated. Default value is 4 which means the mAP will be calculated after each 4 epochs
int mAP_epochs = find_int_arg(argc, argv, "-mAP_epochs", 4);
if (argc < 4) {
Expand Down Expand Up @@ -2028,17 +2044,26 @@ void run_detector(int argc, char **argv)
int it_num = 100;
draw_object(datacfg, cfg, weights, filename, thresh, dont_show, it_num, letter_box, benchmark_layers);
}
else if (0 == strcmp(argv[2], "demo")) {
else if (0 == strcmp(argv[2], "demo") || 0 == strcmp(argv[2], "stream")) {
list *options = read_data_cfg(datacfg);
int classes = option_find_int(options, "classes", 20);
char *name_list = option_find_str(options, "names", "data/names.list");
char **names = get_labels(name_list);
if (filename)
if (strlen(filename) > 0)
if (filename[strlen(filename) - 1] == 0x0d) filename[strlen(filename) - 1] = 0;
demo(cfg, weights, thresh, hier_thresh, cam_index, filename, names, classes, avgframes, frame_skip, prefix, out_filename,
mjpeg_port, dontdraw_bbox, json_port, dont_show, ext_output, letter_box, time_limit_sec, http_post_host, benchmark, benchmark_layers, json_file_output);

if (0 == strcmp(argv[2], "demo")){
demo(cfg, weights, thresh, hier_thresh, cam_index, filename, names, classes, avgframes, frame_skip, prefix, out_filename,
mjpeg_port, dontdraw_bbox, json_port, dont_show, ext_output, letter_box, time_limit_sec, http_post_host, benchmark, benchmark_layers, json_file_output);
}
#ifdef STREAM
if (0 == strcmp(argv[2], "stream")){
stream(cfg, weights, thresh, hier_thresh, cam_index, filename, names, classes, avgframes, frame_skip, prefix, out_filename,
mjpeg_port, dontdraw_bbox, json_port, dont_show, ext_output, letter_box, time_limit_sec, http_post_host, benchmark, benchmark_layers,
stream_bitrate, stream_addr, stream_frame_width, stream_frame_height, stream_profile, stream_gop_size, stream_fps);
}
#endif
free_list_contents_kvp(options);
free_list(options);
}
Expand Down
Loading