GithubHelp home page GithubHelp logo

gcy / esp32-cam-mjpeg-stream-decoder-and-control-library Goto Github PK

View Code? Open in Web Editor NEW
49.0 5.0 11.0 22.31 MB

The library is MJPEG stream decoder based on libcurl and OpenCV, and written in C/C++.

License: GNU General Public License v3.0

C++ 42.63% C 57.37%
esp32-cam esp32 esp32-arduino esp32-library mjpeg-stream mjpeg-decoder mjpeg-player wxwidgets opencv3 opencv4

esp32-cam-mjpeg-stream-decoder-and-control-library's Introduction

ESP32-CAM-MJPEG-Stream-Decoder-and-Control-Library

ESP32-CAM is an inexpensive MJPEG stream embedded device. The library is MJPEG stream decoder based on libcurl and OpenCV, and written in C/C++.

Firmware

Modify the following code in "CameraWebServer.ino" file, that is MJPEG stream boundary.

#define PART_BOUNDARY "WINBONDBOUDARY"
static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
static const char* _STREAM_PART = "Content-Type: image/jpeg\r\n\r\n";

Easy-to-use

Dependence

  • OpenCV 3 or 4

  • libcurl 7

  • wxWidgets 2.8.12(for wxESP32-CAM example)

Use

Include "ESP32-CAM MJPEG Library" folder into your project.

  • ESP32-CAM Library.h
  • ESP32-CAM Library.cpp
#include "./ESP32-CAM MJPEG Library/ESP32-CAM Library.h"

typedef enum {
    FRAMESIZE_QQVGA,    // 160x120
    FRAMESIZE_QQVGA2,   // 128x160
    FRAMESIZE_QCIF,     // 176x144
    FRAMESIZE_HQVGA,    // 240x176
    FRAMESIZE_QVGA,     // 320x240
    FRAMESIZE_CIF,      // 400x296
    FRAMESIZE_VGA,      // 640x480
    FRAMESIZE_SVGA,     // 800x600
    FRAMESIZE_XGA,      // 1024x768
    FRAMESIZE_SXGA,     // 1280x1024
    FRAMESIZE_UXGA      // 1600x1200
} framesize_t;

ESP32_CAM *esp32_cam = new ESP32_CAM(std::string( "192.168.1.254" ));   //ESP32-CAM local IP address

esp32_cam->SetResolution(FRAMESIZE_SVGA); // Set mjpeg video stream resolution

esp32_cam->StartVideoStream();   // Start mjpeg video stream thread

//cv::Mat frame = esp32_cam->GetFrame(); // Get mjpeg frame
cv::Mat frame; 
esp32_cam >> frame;  // "operator >>" call GetFrame(), like cv::VideoCapture call read() 
if(!frame.empty()){
   cv::imshow("Example",frame);  //Show mjpeg video stream
}

Command POST and ESP32-CAM GET

In cmd_handler, "var" is variable name, "val" is value of variable, curl POST command is:

int ESP32_CAM::SendStream(CURL *curl_object)
{
   long return_code;
   CURLcode response;

   curl_easy_setopt(curl_object,CURLOPT_URL,"192.168.1.254/control?var=framesize&val=7");

   response = curl_easy_perform(curl_object);

   response = curl_easy_getinfo(curl_object, CURLINFO_RESPONSE_CODE, &return_code);

   return (int)return_code;
}

"192.168.1.254/control?var=framesize&val=8", "192.168.1.254" is ESP32-CAM IP address setting from:

// Set your Static IP address
IPAddress local_IP(192, 168, 1, 254);
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);
WiFi.begin(ssid, password);  
WiFi.config(local_IP, gateway, subnet);

In this example, "/control?var=framesize&val=8" POST /control API and set parameter "framesize" value is "7"(FRAMESIZE_SVGA 800x600).

API FlashControl(bool), Flash LED turn on or off "/led?var=flash&val=1", val=1 is on, val=0 is off.

Receive ESP32-CAM Page Content

Set curl_easy_setopt parameter CURLOPT_WRITEFUNCTION and CURLOPT_WRITEDATA for writing received data.

std::string ESP32_CAM::GetRSSI()
{
   std::string buffer;

   curl_easy_setopt(curl_command,CURLOPT_WRITEFUNCTION,WriteCallback);
   curl_easy_setopt(curl_command,CURLOPT_WRITEDATA,&buffer); 

   if(SendStream(curl_command,std::string("/RSSI"))){
   }  

   return buffer;
}

alt text

alt text

MJPEG Stream Format

In this case MJPEG stream boundary is fixed length, for split into a MJPEG stream of JPEG binary(byte), each frame JPEG binary with cv::imdecode decode to RGB format.

const char VIDEO_STREAM_INTERLEAVE[] = "--WINBONDBOUDARY\r\nContent-Type: image/jpeg\r\n\r\n";

const char JPEG_SOI_MARKER_FIRST_BYTE = 0xFF;
const char JPEG_SOI_MARKER_SECOND_BYTE = 0xD8;
Content-type: multipart/x-mixed-replace; boundary=WINBONDBOUDARY

--WINBONDBOUDARY\r\n
Content-Type: image/jpeg\r\n\r\n
JPEG Binary(0xFF,0xD8 ... 0xFF,0xD9)
--WINBONDBOUDARY\r\n 
Content-Type: image/jpeg\r\n\r\n 
JPEG Binary(0xFF,0xD8 ... 0xFF,0xD9) 
...
...
...
--WINBONDBOUDARY\r\n 
Content-Type: image/jpeg\r\n\r\n 
JPEG Binary(0xFF,0xD8 ... 0xFF,0xD9) 

Examples

wxESP32-CAM

Mac OS X or Linux

  • "cd /examples/MAC/wxESP32-CAM/"
  • make

alt text

Windows 10

Build x64 version wxWidgets and libcurl

  • 『x64 Native Tools Command Prompt for VS 2017/2019』, open the program as an administrator.
  • Build x64 libcurl 7.67.0
    • cd "./libcurl-7.67.0/winbuild"
    • run "buildconf.bat"
    • nmake /f Makefile.vc mode=static VC=15 MACHINE=x64 DEBUG=no
    • static link libcurl_a.lib, Ws2_32.lib, Wldap32.lib, winmm.lib, Crypt32.lib, Normaliz.lib
  • Build x64 wxWidgets 3.1.2
    • cd "build/msw"
    • nmake /f makefile.vc /ls RUNTIME_LIBS=static SHARED=0 COMPILER_VERSION=141 TARGET_CPU=X64 BUILD=release
    • nmake /f makefile.vc /ls RUNTIME_LIBS=static SHARED=1 COMPILER_VERSION=141 TARGET_CPU=X64 BUILD=release
  • Build wxESP32-CAM
    • Open *.sln project
    • Select "Release x64"
    • Rebuild

alt text

DNN File

This example is wxWidgets GUI for the library demo, and integrate DNN Computer Vision use cases(YOLO V3, OpenPose).

ESP32-CAM Opencv Example

OpenCV highgui MJPEG stream player.

Mac OS X or Linux

  • cd /examples/MAC/ESP32-CAM Opencv Example/
  • make

alt text

Windows 10

  • Open *.sln project
  • Select "Release x64"
  • Rebuild

alt text

Reference

Pinout Diagram

The following image shows the pinout diagram for the ESP32-CAM AI-Thinker. alt text

Schematic Diagram

The following figure shows the schematic diagram for the ESP32-CAM.

alt text

Licensing

Copyright (C) 2019 TonyGUO https://github.com/GCY.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

esp32-cam-mjpeg-stream-decoder-and-control-library's People

Contributors

gcy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

esp32-cam-mjpeg-stream-decoder-and-control-library's Issues

Compiling in Linux, or using ROS

Remember to define _MAC_ when compiling under Linux. It is in the example makefile for Linux, but I didn't see it, and spent a lot of time trying to get the library to work when compiled br catkin_make.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.