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

http jpg streaming #161

Merged
merged 11 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

:heavy_check_mark: ***NEW FEATURE*** [`cv::imshow` supports Linux framebuffer and Windows](#cvimshow-supports-linux-framebuffer-and-windows)

:heavy_check_mark: ***NEW FEATURE*** [`cv::VideoWriter` supports jpg streaming over http](#cvvideowriter-supports-jpg-streaming-over-http)

|opencv 4.10.0 package size|The official opencv|opencv-mobile|
|:-:|:-:|:-:|
|source zip|95.2 MB|8.25 MB|
Expand Down Expand Up @@ -610,6 +612,35 @@ int main()
</td></tr>
</table>

## `cv::VideoWriter` supports jpg streaming over http

In Linux, `cv::VideoWriter` could be used for streaming images as jpg over http, while it is noop on other platforms. Initialize a `cv::VideoWriter` instance, `open` the writer with name `httpjpg` and a port number, then it will setup a simple http server. You can open the streaming url with a web browser, and image will be shown in the browser as soon as it is sent to the writer. The image size and content can be dynamic, which is useful for streaming frames from a live camera.

```cpp
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

int main()
{
cv::VideoCapture cap;
cap.open(0);

cv::VideoWriter http;
http.open("httpjpg", 7766);

// open streaming url http://<server ip>:7766 in web browser

cv::Mat bgr;
while (1)
{
cap >> bgr;
http << bgr;
}

return 0;
}
```

# opencv modules included

|module|comment|
Expand Down
4 changes: 3 additions & 1 deletion highgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(highgui_srcs
${CMAKE_CURRENT_LIST_DIR}/src/highgui.cpp
${CMAKE_CURRENT_LIST_DIR}/src/kanna_rotate.cpp
${CMAKE_CURRENT_LIST_DIR}/src/videocapture.cpp
${CMAKE_CURRENT_LIST_DIR}/src/videowriter.cpp

# dnn
${CMAKE_CURRENT_LIST_DIR}/src/nms.cpp
Expand All @@ -18,7 +19,8 @@ set(highgui_srcs
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
list(APPEND highgui_srcs
${CMAKE_CURRENT_LIST_DIR}/src/capture_v4l2.cpp
${CMAKE_CURRENT_LIST_DIR}/src/display_fb.cpp)
${CMAKE_CURRENT_LIST_DIR}/src/display_fb.cpp
${CMAKE_CURRENT_LIST_DIR}/src/writer_http.cpp)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
Expand Down
22 changes: 22 additions & 0 deletions highgui/include/opencv2/highgui/highgui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,28 @@ class CV_EXPORTS_W VideoCapture
VideoCaptureImpl* const d;
};

class VideoWriterImpl;
class CV_EXPORTS_W VideoWriter
{
public:
VideoWriter();

~VideoWriter();

bool open(const String& name, int port);

bool isOpened() const;

void release();

VideoWriter& operator<<(const Mat& bgr_image);

void write(const Mat& image);

protected:
VideoWriterImpl* const d;
};

} // namespace cv

#endif // OPENCV_HIGHGUI_HPP
2 changes: 1 addition & 1 deletion highgui/src/highgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#if CV_WITH_RK
#include "jpeg_encoder_rk_mpp.h"
#endif
#if defined __linux__
#if defined __linux__ && !__ANDROID__
#include "display_fb.h"
#endif

Expand Down
144 changes: 144 additions & 0 deletions highgui/src/videowriter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
//
// Copyright (C) 2024 nihui
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>

#include <stdio.h>
#include <string.h>

#if defined __linux__ && !__ANDROID__
#include "writer_http.h"
#endif

namespace cv {

class VideoWriterImpl
{
public:
VideoWriterImpl();

public:
bool is_opened;
int width;
int height;
float fps;

#if defined __linux__ && !__ANDROID__
writer_http wt_http;
#endif
};

VideoWriterImpl::VideoWriterImpl()
{
is_opened = false;
width = 0;
height = 0;
fps = 0;
}

VideoWriter::VideoWriter() : d(new VideoWriterImpl)
{
}

VideoWriter::~VideoWriter()
{
release();

delete d;
}

bool VideoWriter::open(const String& name, int port)
{
if (d->is_opened)
{
release();
}

if (name == "httpjpg")
{
#if defined __linux__ && !__ANDROID__
if (writer_http::supported())
{
int ret = d->wt_http.open(port);
if (ret == 0)
{
d->is_opened = true;
}
}
else
#endif
{
}
}

return d->is_opened;
}

bool VideoWriter::isOpened() const
{
return d->is_opened;
}

void VideoWriter::release()
{
if (!d->is_opened)
return;

#if defined __linux__ && !__ANDROID__
if (writer_http::supported())
{
d->wt_http.close();
}
else
#endif
{
}

d->is_opened = false;
d->width = 0;
d->height = 0;
d->fps = 0;
}

VideoWriter& VideoWriter::operator<<(const Mat& image)
{
write(image);

return *this;
}

void VideoWriter::write(const Mat& image)
{
if (!d->is_opened)
return;

#if defined __linux__ && !__ANDROID__
if (writer_http::supported())
{
// encode jpg
std::vector<uchar> buf;
cv::imencode(".JPG", image, buf);

d->wt_http.write_jpgbuf((const unsigned char*)buf.data(), buf.size());
}
else
#endif
{
}
}

} // namespace cv
Loading