fst commit

This commit is contained in:
jario-jin
2023-06-16 10:42:02 +08:00
parent 5793dd0af6
commit 0136d3c2da
122 changed files with 25173 additions and 25 deletions
+172
View File
@@ -0,0 +1,172 @@
#ifndef __SV_ALGORITHM__
#define __SV_ALGORITHM__
#include "sv_video_base.h"
#include <opencv2/opencv.hpp>
#include <opencv2/aruco.hpp>
#include <opencv2/tracking.hpp>
#include <string>
#include <chrono>
namespace yaed {
class EllipseDetector;
}
namespace sv {
// union JsonValue;
// class JsonAllocator;
class CameraAlgorithm
{
public:
CameraAlgorithm();
~CameraAlgorithm();
void loadCameraParams(std::string yaml_fn_);
void loadAlgorithmParams(std::string json_fn_);
cv::Mat camera_matrix;
cv::Mat distortion;
int image_width;
int image_height;
double fov_x;
double fov_y;
std::string alg_params_fn;
protected:
// JsonValue* _value;
// JsonAllocator* _allocator;
std::chrono::system_clock::time_point _t0;
};
class ArucoDetector : public CameraAlgorithm
{
public:
ArucoDetector();
void detect(cv::Mat img_, TargetsInFrame& tgts_);
private:
void _load();
bool _params_loaded;
cv::Ptr<cv::aruco::DetectorParameters> _detector_params;
cv::Ptr<cv::aruco::Dictionary> _dictionary;
int _dictionary_id;
std::vector<int> _ids_need;
std::vector<double> _lengths_need;
};
class EllipseDetector : public CameraAlgorithm
{
public:
EllipseDetector();
~EllipseDetector();
void detectAllInDirectory(std::string input_img_dir_, std::string output_json_dir_);
void detect(cv::Mat img_, TargetsInFrame& tgts_);
protected:
void _load();
bool _params_loaded;
yaed::EllipseDetector* _ed;
float _max_center_distance_ratio;
double _radius_in_meter;
};
class LandingMarkerDetectorBase : public EllipseDetector
{
public:
LandingMarkerDetectorBase();
~LandingMarkerDetectorBase();
void detect(cv::Mat img_, TargetsInFrame& tgts_);
bool isParamsLoaded();
int getMaxCandidates();
std::vector<std::string> getLabelsNeed();
protected:
virtual bool setupImpl();
virtual void roiCNN(std::vector<cv::Mat>& input_rois_, std::vector<int>& output_labels_);
void _loadLabels();
int _max_candidates;
std::vector<std::string> _labels_need;
};
class SingleObjectTrackerBase : public CameraAlgorithm
{
public:
SingleObjectTrackerBase();
~SingleObjectTrackerBase();
void warmUp();
void init(cv::Mat img_, const cv::Rect& bounding_box_);
void track(cv::Mat img_, TargetsInFrame& tgts_);
bool isParamsLoaded();
std::string getAlgorithm();
int getBackend();
int getTarget();
protected:
virtual bool setupImpl();
virtual void initImpl(cv::Mat img_, const cv::Rect& bounding_box_);
virtual bool trackImpl(cv::Mat img_, cv::Rect& output_bbox_);
void _load();
bool _params_loaded;
std::string _algorithm;
int _backend;
int _target;
};
class CommonObjectDetectorBase : public CameraAlgorithm
{
public:
CommonObjectDetectorBase();
~CommonObjectDetectorBase();
void warmUp();
void detect(cv::Mat img_, TargetsInFrame& tgts_, Box* roi_=nullptr, int img_w_=0, int img_h_=0);
bool isParamsLoaded();
std::string getDataset();
std::vector<std::string> getClassNames();
std::vector<double> getClassWs();
std::vector<double> getClassHs();
int getInputH();
void setInputH(int h_);
int getInputW();
void setInputW(int w_);
int getClassNum();
int getOutputSize();
double getThrsNms();
double getThrsConf();
int useWidthOrHeight();
bool withSegmentation();
protected:
virtual bool setupImpl();
virtual void detectImpl(
cv::Mat img_,
std::vector<float>& boxes_x_,
std::vector<float>& boxes_y_,
std::vector<float>& boxes_w_,
std::vector<float>& boxes_h_,
std::vector<int>& boxes_label_,
std::vector<float>& boxes_score_,
std::vector<cv::Mat>& boxes_seg_
);
void _load();
bool _params_loaded;
std::string _dataset;
std::vector<std::string> _class_names;
std::vector<double> _class_ws;
std::vector<double> _class_hs;
int _input_h;
int _input_w;
int _n_classes;
int _output_size;
double _thrs_nms;
double _thrs_conf;
int _use_width_or_height;
bool _with_segmentation;
};
}
#endif
+39
View File
@@ -0,0 +1,39 @@
#ifndef __SV_COMMON_DET__
#define __SV_COMMON_DET__
#include "sv_core.h"
#include <opencv2/opencv.hpp>
#include <opencv2/aruco.hpp>
#include <opencv2/tracking.hpp>
#include <string>
#include <chrono>
namespace sv {
class CommonObjectDetectorCUDAImpl;
class CommonObjectDetector : public CommonObjectDetectorBase
{
public:
CommonObjectDetector();
~CommonObjectDetector();
protected:
bool setupImpl();
void detectImpl(
cv::Mat img_,
std::vector<float>& boxes_x_,
std::vector<float>& boxes_y_,
std::vector<float>& boxes_w_,
std::vector<float>& boxes_h_,
std::vector<int>& boxes_label_,
std::vector<float>& boxes_score_,
std::vector<cv::Mat>& boxes_seg_
);
CommonObjectDetectorCUDAImpl* _cuda_impl;
};
}
#endif
+8
View File
@@ -0,0 +1,8 @@
#ifndef __SV_CORE__
#define __SV_CORE__
#include "sv_video_base.h"
#include "sv_gimbal.h"
#include "sv_algorithm_base.h"
#endif
+162
View File
@@ -0,0 +1,162 @@
/*
* @Description:
* @Author: jario-jin @amov
* @Date: 2023-04-12 09:12:52
* @LastEditors: L LC @amov
* @LastEditTime: 2023-04-18 11:49:27
* @FilePath: /spirecv-gimbal-sdk/include/sv_gimbal.h
*/
#ifndef __SV_GIMBAL__
#define __SV_GIMBAL__
#include <string>
namespace sv
{
typedef void (*PStateInvoke)(double &frame_ang_r, double &frame_ang_p, double &frame_ang_y,
double &imu_ang_r, double &imu_ang_p, double &imu_ang_y,
double &fov_x, double &fov_y);
enum class GimbalType
{
G1,
Q10f
};
enum class GimbalLink
{
SERIAL,
ETHERNET_TCP,
ETHERNET_UDP
};
enum class GimablSerialByteSize
{
FIVE_BYTES = 5,
SIX_BYTES = 6,
SEVEN_BYTES = 7,
EIGHT_BYTES = 8,
};
enum class GimablSerialParity
{
PARITY_NONE = 0,
PARITY_ODD = 1,
PARITY_EVEN = 2,
PARITY_MARK = 3,
PARITY_SPACE = 4,
};
enum class GimablSerialStopBits
{
STOPBITS_ONE = 1,
STOPBITS_TWO = 2,
STOPBITS_ONE_POINT_FIVE = 3,
};
enum class GimablSerialFlowControl
{
FLOWCONTROL_NONE = 0,
FLOWCONTROL_SOFTWARE = 1,
FLOWCONTROL_HARDWARE = 2,
};
static inline void emptyCallback(double &frameAngleRoll, double &frameAnglePitch, double &frameAngleYaw,
double &imuAngleRoll, double &imuAnglePitch, double &imuAngleYaw,
double &fovX, double &fovY)
{
}
//! A gimbal control and state reading class.
/*!
A common gimbal control class for vary type of gimbals.
e.g. AMOV G1
*/
class Gimbal
{
private:
// Device pointers
void *dev;
void *IO;
// Generic serial interface parameters list & default parameters
std::string m_serial_port = "/dev/ttyUSB0";
int m_serial_baud_rate = 115200;
int m_serial_byte_size = (int)GimablSerialByteSize::EIGHT_BYTES;
int m_serial_parity = (int)GimablSerialParity::PARITY_NONE;
int m_serial_stopbits = (int)GimablSerialStopBits::STOPBITS_ONE;
int m_serial_flowcontrol = (int)GimablSerialFlowControl::FLOWCONTROL_NONE;
int m_serial_timeout = 500;
// Ethernet interface parameters list & default parameters
std::string m_net_ip = "192.168.2.64";
int m_net_port = 9090;
GimbalType m_gimbal_type;
GimbalLink m_gimbal_link;
public:
//! Constructor
/*!
\param serial_port: string like '/dev/ttyUSB0' in linux sys.
\param baud_rate: serial baud rate, e.g. 115200
*/
Gimbal(GimbalType gtype = GimbalType::G1, GimbalLink ltype = GimbalLink::SERIAL)
{
m_gimbal_type = gtype;
m_gimbal_link = ltype;
}
~Gimbal();
// set Generic serial interface parameters
void setSerialPort(const std::string &port);
void setSerialPort(const int baud_rate);
void setSerialPort(GimablSerialByteSize byte_size, GimablSerialParity parity,
GimablSerialStopBits stop_bits, GimablSerialFlowControl flowcontrol,
int time_out = 500);
// set Ethernet interface parameters
void setNetIp(const std::string &ip);
void setNetPort(const int &port);
// Create a device instance
void setStateCallback(PStateInvoke callback);
bool open(PStateInvoke callback = emptyCallback);
// Funtions
bool setHome();
bool setZoom(double x);
bool setAutoZoom(int state);
bool setAutoFocus(int state);
bool takePhoto();
bool takeVideo(int state);
int getVideoState();
//! Set gimbal angles
/*!
\param roll: eular roll angle (-60, 60) degree
\param pitch: eular pitch angle (-135, 135) degree
\param yaw: eular yaw angle (-150, 150) degree
\param roll_rate: roll angle rate, degree/s
\param pitch_rate: pitch angle rate, degree/s
\param yaw_rate: yaw angle rate, degree/s
*/
void setAngleEuler(
double roll,
double pitch,
double yaw,
double roll_rate = 0,
double pitch_rate = 0,
double yaw_rate = 0);
//! Set gimbal angle rates
/*!
\param roll_rate: roll angle rate, degree/s
\param pitch_rate: pitch angle rate, degree/s
\param yaw_rate: yaw angle rate, degree/s
*/
void setAngleRateEuler(
double roll_rate,
double pitch_rate,
double yaw_rate);
};
}
#endif
+33
View File
@@ -0,0 +1,33 @@
#ifndef __SV_LANDING_DET__
#define __SV_LANDING_DET__
#include "sv_core.h"
#include <opencv2/opencv.hpp>
#include <opencv2/aruco.hpp>
#include <opencv2/tracking.hpp>
#include <string>
#include <chrono>
namespace sv {
class LandingMarkerDetectorCUDAImpl;
class LandingMarkerDetector : public LandingMarkerDetectorBase
{
public:
LandingMarkerDetector();
~LandingMarkerDetector();
protected:
bool setupImpl();
void roiCNN(
std::vector<cv::Mat>& input_rois_,
std::vector<int>& output_labels_
);
LandingMarkerDetectorCUDAImpl* _cuda_impl;
};
}
#endif
+32
View File
@@ -0,0 +1,32 @@
#ifndef __SV_TRACKING__
#define __SV_TRACKING__
#include "sv_core.h"
#include <opencv2/opencv.hpp>
#include <opencv2/aruco.hpp>
#include <opencv2/tracking.hpp>
#include <string>
#include <chrono>
namespace sv {
class SingleObjectTrackerOCV470Impl;
class SingleObjectTracker : public SingleObjectTrackerBase
{
public:
SingleObjectTracker();
~SingleObjectTracker();
protected:
bool setupImpl();
void initImpl(cv::Mat img_, const cv::Rect& bounding_box_);
bool trackImpl(cv::Mat img_, cv::Rect& output_bbox_);
SingleObjectTrackerOCV470Impl* _ocv470_impl;
};
}
#endif
+399
View File
@@ -0,0 +1,399 @@
#ifndef __SV_VIDEOIO__
#define __SV_VIDEOIO__
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <queue>
#include <stack>
#include <thread>
#include <arpa/inet.h>
#include <netinet/in.h> // for sockaddr_in
#define SV_RAD2DEG 57.2957795
// #define X86_PLATFORM
// #define JETSON_PLATFORM
namespace sv {
//! The rectangle bounding-box of an object.
class Box
{
public:
Box();
int x1;
int y1;
int x2;
int y2;
//! Set the parameters of the bounding-box by XYXY-format.
/*!
\param x1_: The x-axis pixel coordinates of the top-left point.
\param y1_: The y-axis pixel coordinates of the top-left point.
\param x2_: The x-axis pixel coordinates of the bottom-right point.
\param y2_: The y-axis pixel coordinates of the bottom-right point.
*/
void setXYXY(int x1_, int y1_, int x2_, int y2_);
//! Set the parameters of the bounding-box by XYWH-format.
/*!
\param x1_: The x-axis pixel coordinates of the top-left point.
\param y1_: The y-axis pixel coordinates of the top-left point.
\param w_: The width of the bounding rectangle.
\param h_: The height of the bounding rectangle.
*/
void setXYWH(int x_, int y_, int w_, int h_);
};
//! Description class for a single target detection result.
/*!
Support multiple description methods,
such as bounding box, segmentation, ellipse, three-dimensional position, etc.
*/
class Target
{
public:
Target();
//! X coordinate of object center point, [0, 1], (Required)
double cx;
//! Y coordinate of object center point, [0, 1], (Required)
double cy;
//! Object-width / image-width, (0, 1]
double w;
//! Object-height / image-heigth, (0, 1]
double h;
//! Objectness, Confidence, (0, 1]
double score;
//! Category of target.
std::string category;
//! Category ID of target.
int category_id;
//! The same target in different frames shares a unique ID.
int tracked_id;
//! X coordinate of object position in Camera-Frame (unit: meter).
double px;
//! Y coordinate of object position in Camera-Frame (unit: meter).
double py;
//! Z coordinate of object position in Camera-Frame (unit: meter).
double pz;
//! Line of sight (LOS) angle on X-axis (unit: degree).
double los_ax;
//! Line of sight (LOS) angle on Y-axis (unit: degree).
double los_ay;
//! The angle of the target in the image coordinate system, (unit: degree) [-180, 180].
double yaw_a;
//! Whether the height&width of the target can be obtained.
bool has_hw;
//! Whether the category of the target can be obtained.
bool has_category;
//! Whether the tracking-ID of the target can be obtained.
bool has_tid;
//! Whether the 3D-position of the target can be obtained.
bool has_position;
//! Whether the LOS-angle of the target can be obtained.
bool has_los;
//! Whether the segmentation of the target can be obtained.
bool has_seg;
//! Whether the bounding-box of the target can be obtained.
bool has_box;
//! Whether the ellipse-parameters of the target can be obtained.
bool has_ell;
//! Whether the aruco-parameters of the target can be obtained.
bool has_aruco;
//! Whether the direction of the target can be obtained.
bool has_yaw;
void setCategory(std::string cate_, int cate_id_);
void setLOS(double cx_, double cy_, cv::Mat camera_matrix_, int img_w_, int img_h_);
void setTrackID(int id_);
void setPosition(double x_, double y_, double z_);
void setBox(int x1_, int y1_, int x2_, int y2_, int img_w_, int img_h_);
void setAruco(int id_, std::vector<cv::Point2f> corners_, cv::Vec3d rvecs_, cv::Vec3d tvecs_, int img_w_, int img_h_, cv::Mat camera_matrix_);
void setEllipse(double xc_, double yc_, double a_, double b_, double rad_, double score_, int img_w_, int img_h_, cv::Mat camera_matrix_, double radius_in_meter_);
void setYaw(double vec_x_, double vec_y);
void setMask(cv::Mat mask_);
cv::Mat getMask();
bool getBox(Box& b);
bool getAruco(int& id, std::vector<cv::Point2f> &corners);
bool getEllipse(double& xc_, double& yc_, double& a_, double& b_, double& rad_);
std::string getJsonStr();
private:
//! segmentation [[x1,y1, x2,y2, x3,y3,...],...]
/*!
SEG variables: (_s_) segmentation, segmentation_size_h, segmentation_size_w, segmentation_counts, area
*/
std::vector<std::vector<double> > _s_segmentation;
int _s_segmentation_size_h;
int _s_segmentation_size_w;
std::string _s_segmentation_counts;
cv::Mat _mask;
double _s_area;
//! bounding box [x, y, w, h]
/*!
BOX variables: (_b_) box
*/
Box _b_box; // x,y,w,h
//! ellipse x-axis center
/*!
ELL variables: (_e_) xc, yc, a, b, rad
*/
double _e_xc;
double _e_yc;
double _e_a;
double _e_b;
double _e_rad;
//! Aruco Marker ID
/*!
ARUCO variables: (_a_) id, corners, rvecs, tvecs
*/
int _a_id;
std::vector<cv::Point2f> _a_corners;
cv::Vec3d _a_rvecs;
cv::Vec3d _a_tvecs;
};
enum class MissionType {NONE, COMMON_DET, TRACKING, ARUCO_DET, LANDMARK_DET, ELLIPSE_DET};
//! This class describes all objects in a single frame image.
/*!
1. Contains multiple Target instances.
2. Describes the ID of the current frame, image width and height, current field of view, etc.
3. Describes the processed image sub-regions and supports local region detection.
*/
class TargetsInFrame
{
public:
TargetsInFrame(int frame_id_);
//! Frame number.
int frame_id;
//! Frame/image height.
int height;
//! Frame/image width.
int width;
//! Detection frame per second (FPS).
double fps;
//! The x-axis field of view (FOV) of the current camera.
double fov_x;
//! The y-axis field of view (FOV) of the current camera.
double fov_y;
//! 吊舱俯仰角
double pod_patch;
//! 吊舱滚转角
double pod_roll;
//! 吊舱航向角,东向为0,东北天为正,范围[-180,180]
double pod_yaw;
//! 当前经度
double longitude;
//! 当前纬度
double latitude;
//! 当前飞行高度
double altitude;
//! 飞行速度,x轴,东北天坐标系
double uav_vx;
//! 飞行速度,y轴,东北天坐标系
double uav_vy;
//! 飞行速度,z轴,东北天坐标系
double uav_vz;
//! 当前光照强度,Lux
double illumination;
//! Whether the detection FPS can be obtained.
bool has_fps;
//! Whether the FOV can be obtained.
bool has_fov;
//! Whether the processed image sub-region can be obtained.
bool has_roi;
bool has_pod_info;
bool has_uav_pos;
bool has_uav_vel;
bool has_ill;
MissionType type;
//! The processed image sub-region, if size>0, it means no full image detection.
std::vector<Box> rois;
//! Detected Target Instances.
std::vector<Target> targets;
std::string date_captured;
void setTimeNow();
void setFPS(double fps_);
void setFOV(double fov_x_, double fov_y_);
void setSize(int width_, int height_);
std::string getJsonStr();
};
class UDPServer {
public:
UDPServer(std::string dest_ip="127.0.0.1", int port=20166);
~UDPServer();
void send(const TargetsInFrame& tgts_);
private:
struct sockaddr_in _servaddr;
int _sockfd;
};
class VideoWriterBase {
public:
VideoWriterBase();
~VideoWriterBase();
void setup(std::string file_path, cv::Size size, double fps=25.0, bool with_targets=false);
void write(cv::Mat image, TargetsInFrame tgts=TargetsInFrame(0));
void release();
cv::Size getSize();
double getFps();
std::string getFilePath();
bool isRunning();
protected:
virtual bool setupImpl(std::string file_name_);
virtual bool isOpenedImpl();
virtual void writeImpl(cv::Mat img_);
virtual void releaseImpl();
void _init();
void _run();
bool _is_running;
cv::Size _image_size;
double _fps;
bool _with_targets;
int _fid;
int _fcnt;
std::thread _tt;
// cv::VideoWriter _writer;
std::ofstream _targets_ofs;
std::string _file_path;
std::queue<cv::Mat> _image_to_write;
std::queue<TargetsInFrame> _tgts_to_write;
};
class VideoStreamerBase {
public:
VideoStreamerBase();
~VideoStreamerBase();
void setup(cv::Size size, int port=8554, int bitrate=2, std::string url="/live"); // 2M
void stream(cv::Mat image);
void release();
cv::Size getSize();
int getPort();
std::string getUrl();
int getBitrate();
bool isRunning();
protected:
virtual bool setupImpl();
virtual bool isOpenedImpl();
virtual void writeImpl(cv::Mat image);
virtual void releaseImpl();
void _run();
bool _is_running;
cv::Size _stream_size;
int _port;
std::string _url;
int _bitrate;
std::thread _tt;
std::stack<cv::Mat> _image_to_stream;
};
enum class CameraType {NONE, WEBCAM, G1, Q10};
class CameraBase {
public:
CameraBase(CameraType type=CameraType::NONE, int id=0);
~CameraBase();
void open(CameraType type=CameraType::WEBCAM, int id=0);
bool read(cv::Mat& image);
void release();
int getW();
int getH();
int getFps();
std::string getIp();
int getPort();
double getBrightness();
double getContrast();
double getSaturation();
double getHue();
double getExposure();
bool isRunning();
void setWH(int width, int height);
void setFps(int fps);
void setIp(std::string ip);
void setPort(int port);
void setBrightness(double brightness);
void setContrast(double contrast);
void setSaturation(double saturation);
void setHue(double hue);
void setExposure(double exposure);
protected:
virtual void openImpl();
void _run();
bool _is_running;
bool _is_updated;
std::thread _tt;
cv::VideoCapture _cap;
cv::Mat _frame;
CameraType _type;
int _camera_id;
int _width;
int _height;
int _fps;
std::string _ip;
int _port;
double _brightness;
double _contrast;
double _saturation;
double _hue;
double _exposure;
};
void drawTargetsInFrame(
cv::Mat& img_,
const TargetsInFrame& tgts_,
bool with_all=true,
bool with_category=false,
bool with_tid=false,
bool with_seg=false,
bool with_box=false,
bool with_ell=false,
bool with_aruco=false,
bool with_yaw=false
);
std::string get_home();
bool is_file_exist(std::string& fn);
void list_dir(std::string dir, std::vector<std::string>& files, std::string suffixs="", bool r=false);
}
#endif
+27
View File
@@ -0,0 +1,27 @@
#ifndef __SV_VIDEO_INPUT__
#define __SV_VIDEO_INPUT__
#include "sv_core.h"
#include <opencv2/opencv.hpp>
#include <opencv2/aruco.hpp>
#include <opencv2/tracking.hpp>
#include <string>
#include <chrono>
namespace sv {
class Camera : public CameraBase
{
public:
Camera();
~Camera();
protected:
void openImpl();
};
}
#endif
+53
View File
@@ -0,0 +1,53 @@
#ifndef __SV_VIDEO_OUTPUT__
#define __SV_VIDEO_OUTPUT__
#include "sv_core.h"
#include <opencv2/opencv.hpp>
#include <opencv2/aruco.hpp>
#include <opencv2/tracking.hpp>
#include <string>
#include <chrono>
class BsVideoSaver;
class BsPushStreamer;
namespace sv {
class VideoWriterGstreamerImpl;
class VideoStreamerGstreamerImpl;
class VideoWriter : public VideoWriterBase
{
public:
VideoWriter();
~VideoWriter();
protected:
bool setupImpl(std::string file_name_);
bool isOpenedImpl();
void writeImpl(cv::Mat img_);
void releaseImpl();
VideoWriterGstreamerImpl* _gstreamer_impl;
BsVideoSaver* _ffmpeg_impl;
};
class VideoStreamer : public VideoStreamerBase
{
public:
VideoStreamer();
~VideoStreamer();
protected:
bool setupImpl();
bool isOpenedImpl();
void writeImpl(cv::Mat img_);
void releaseImpl();
VideoStreamerGstreamerImpl* _gstreamer_impl;
BsPushStreamer* _ffmpeg_impl;
};
}
#endif
+12
View File
@@ -0,0 +1,12 @@
#ifndef __SV__WORLD__
#define __SV__WORLD__
#include "sv_core.h"
#include "sv_common_det.h"
#include "sv_landing_det.h"
#include "sv_tracking.h"
#include "sv_video_input.h"
#include "sv_video_output.h"
#endif