mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 07:43:03 +04:00
Merge branch 4.x
This commit is contained in:
@@ -4,10 +4,6 @@
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" >
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="14"
|
||||
android:targetSdkVersion="21" />
|
||||
|
||||
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
|
||||
<uses-feature android:name="android.hardware.camera"/>
|
||||
<uses-feature android:name="android.hardware.camera2" android:required="false"/>
|
||||
|
||||
+43
-26
@@ -1,13 +1,19 @@
|
||||
#!/usr/bin/env python
|
||||
'''
|
||||
This sample using FlowNet v2 model to calculate optical flow.
|
||||
Original paper: https://arxiv.org/abs/1612.01925.
|
||||
Original repo: https://github.com/lmb-freiburg/flownet2.
|
||||
This sample using FlowNet v2 and RAFT model to calculate optical flow.
|
||||
|
||||
FlowNet v2 Original Paper: https://arxiv.org/abs/1612.01925.
|
||||
FlowNet v2 Repo: https://github.com/lmb-freiburg/flownet2.
|
||||
|
||||
Download the converted .caffemodel model from https://drive.google.com/open?id=16qvE9VNmU39NttpZwZs81Ga8VYQJDaWZ
|
||||
and .prototxt from https://drive.google.com/file/d/1RyNIUsan1ZOh2hpYIH36A-jofAvJlT6a/view?usp=sharing.
|
||||
Otherwise download original model from https://lmb.informatik.uni-freiburg.de/resources/binaries/flownet2/flownet2-models.tar.gz,
|
||||
convert .h5 model to .caffemodel and modify original .prototxt using .prototxt from link above.
|
||||
|
||||
RAFT Original Paper: https://arxiv.org/pdf/2003.12039.pdf
|
||||
RAFT Repo: https://github.com/princeton-vl/RAFT
|
||||
|
||||
Download the .onnx model from here https://github.com/opencv/opencv_zoo/raw/281d232cd99cd920853106d853c440edd35eb442/models/optical_flow_estimation_raft/optical_flow_estimation_raft_2023aug.onnx.
|
||||
'''
|
||||
|
||||
import argparse
|
||||
@@ -17,8 +23,11 @@ import cv2 as cv
|
||||
|
||||
|
||||
class OpticalFlow(object):
|
||||
def __init__(self, proto, model, height, width):
|
||||
self.net = cv.dnn.readNetFromCaffe(proto, model)
|
||||
def __init__(self, model, height, width, proto=""):
|
||||
if proto:
|
||||
self.net = cv.dnn.readNetFromCaffe(proto, model)
|
||||
else:
|
||||
self.net = cv.dnn.readNet(model)
|
||||
self.net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
|
||||
self.height = height
|
||||
self.width = width
|
||||
@@ -26,8 +35,10 @@ class OpticalFlow(object):
|
||||
def compute_flow(self, first_img, second_img):
|
||||
inp0 = cv.dnn.blobFromImage(first_img, size=(self.width, self.height))
|
||||
inp1 = cv.dnn.blobFromImage(second_img, size=(self.width, self.height))
|
||||
self.net.setInputsNames(["img0", "img1"])
|
||||
self.net.setInput(inp0, "img0")
|
||||
self.net.setInput(inp1, "img1")
|
||||
|
||||
flow = self.net.forward()
|
||||
output = self.motion_to_color(flow)
|
||||
return output
|
||||
@@ -46,7 +57,7 @@ class OpticalFlow(object):
|
||||
rad = rad[..., np.newaxis] / maxrad
|
||||
a = np.arctan2(-fy / maxrad, -fx / maxrad) / np.pi
|
||||
fk = (a + 1) / 2.0 * (ncols - 1)
|
||||
k0 = fk.astype(np.int)
|
||||
k0 = fk.astype(np.int32)
|
||||
k1 = (k0 + 1) % ncols
|
||||
f = fk[..., np.newaxis] - k0[..., np.newaxis]
|
||||
|
||||
@@ -59,41 +70,47 @@ class OpticalFlow(object):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description='Use this script to calculate optical flow using FlowNetv2',
|
||||
parser = argparse.ArgumentParser(description='Use this script to calculate optical flow',
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||
parser.add_argument('-input', '-i', required=True, help='Path to input video file. Skip this argument to capture frames from a camera.')
|
||||
parser.add_argument('--height', default=320, type=int, help='Input height')
|
||||
parser.add_argument('--width', default=448, type=int, help='Input width')
|
||||
parser.add_argument('--proto', '-p', default='FlowNet2_deploy_anysize.prototxt', help='Path to prototxt.')
|
||||
parser.add_argument('--model', '-m', default='FlowNet2_weights.caffemodel', help='Path to caffemodel.')
|
||||
parser.add_argument('--proto', '-p', default='', help='Path to prototxt.')
|
||||
parser.add_argument('--model', '-m', required=True, help='Path to model.')
|
||||
args, _ = parser.parse_known_args()
|
||||
|
||||
if not os.path.isfile(args.model) or not os.path.isfile(args.proto):
|
||||
raise OSError("Prototxt or caffemodel not exist")
|
||||
if not os.path.isfile(args.model):
|
||||
raise OSError("Model does not exist")
|
||||
if args.proto and not os.path.isfile(args.proto):
|
||||
raise OSError("Prototxt does not exist")
|
||||
|
||||
winName = 'Calculation optical flow in OpenCV'
|
||||
cv.namedWindow(winName, cv.WINDOW_NORMAL)
|
||||
cap = cv.VideoCapture(args.input if args.input else 0)
|
||||
hasFrame, first_frame = cap.read()
|
||||
|
||||
divisor = 64.
|
||||
var = {}
|
||||
var['ADAPTED_WIDTH'] = int(np.ceil(args.width/divisor) * divisor)
|
||||
var['ADAPTED_HEIGHT'] = int(np.ceil(args.height/divisor) * divisor)
|
||||
var['SCALE_WIDTH'] = args.width / float(var['ADAPTED_WIDTH'])
|
||||
var['SCALE_HEIGHT'] = args.height / float(var['ADAPTED_HEIGHT'])
|
||||
if args.proto:
|
||||
divisor = 64.
|
||||
var = {}
|
||||
var['ADAPTED_WIDTH'] = int(np.ceil(args.width/divisor) * divisor)
|
||||
var['ADAPTED_HEIGHT'] = int(np.ceil(args.height/divisor) * divisor)
|
||||
var['SCALE_WIDTH'] = args.width / float(var['ADAPTED_WIDTH'])
|
||||
var['SCALE_HEIGHT'] = args.height / float(var['ADAPTED_HEIGHT'])
|
||||
|
||||
config = ''
|
||||
proto = open(args.proto).readlines()
|
||||
for line in proto:
|
||||
for key, value in var.items():
|
||||
tag = "$%s$" % key
|
||||
line = line.replace(tag, str(value))
|
||||
config += line
|
||||
config = ''
|
||||
proto = open(args.proto).readlines()
|
||||
for line in proto:
|
||||
for key, value in var.items():
|
||||
tag = "$%s$" % key
|
||||
line = line.replace(tag, str(value))
|
||||
config += line
|
||||
|
||||
caffemodel = open(args.model, 'rb').read()
|
||||
caffemodel = open(args.model, 'rb').read()
|
||||
|
||||
opt_flow = OpticalFlow(caffemodel, var['ADAPTED_HEIGHT'], var['ADAPTED_WIDTH'], bytearray(config.encode()))
|
||||
else:
|
||||
opt_flow = OpticalFlow(args.model, 360, 480)
|
||||
|
||||
opt_flow = OpticalFlow(bytearray(config.encode()), caffemodel, var['ADAPTED_HEIGHT'], var['ADAPTED_WIDTH'])
|
||||
while cv.waitKey(1) < 0:
|
||||
hasFrame, second_frame = cap.read()
|
||||
if not hasFrame:
|
||||
|
||||
@@ -0,0 +1,370 @@
|
||||
/**
|
||||
* @file yolo_detector.cpp
|
||||
* @brief Yolo Object Detection Sample
|
||||
* @author OpenCV team
|
||||
*/
|
||||
|
||||
//![includes]
|
||||
#include <opencv2/dnn.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/imgcodecs.hpp>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include "iostream"
|
||||
#include "common.hpp"
|
||||
#include <opencv2/highgui.hpp>
|
||||
//![includes]
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::dnn;
|
||||
|
||||
void getClasses(std::string classesFile);
|
||||
void drawPrediction(int classId, float conf, int left, int top, int right, int bottom, Mat& frame);
|
||||
void yoloPostProcessing(
|
||||
std::vector<Mat>& outs,
|
||||
std::vector<int>& keep_classIds,
|
||||
std::vector<float>& keep_confidences,
|
||||
std::vector<Rect2d>& keep_boxes,
|
||||
float conf_threshold,
|
||||
float iou_threshold,
|
||||
const std::string& test_name
|
||||
);
|
||||
|
||||
std::vector<std::string> classes;
|
||||
|
||||
|
||||
std::string keys =
|
||||
"{ help h | | Print help message. }"
|
||||
"{ device | 0 | camera device number. }"
|
||||
"{ model | onnx/models/yolox_s_inf_decoder.onnx | Default model. }"
|
||||
"{ yolo | yolox | yolo model version. }"
|
||||
"{ input i | | Path to input image or video file. Skip this argument to capture frames from a camera. }"
|
||||
"{ classes | | Optional path to a text file with names of classes to label detected objects. }"
|
||||
"{ thr | .5 | Confidence threshold. }"
|
||||
"{ nms | .4 | Non-maximum suppression threshold. }"
|
||||
"{ mean | 0.0 | Normalization constant. }"
|
||||
"{ scale | 1.0 | Preprocess input image by multiplying on a scale factor. }"
|
||||
"{ width | 640 | Preprocess input image by resizing to a specific width. }"
|
||||
"{ height | 640 | Preprocess input image by resizing to a specific height. }"
|
||||
"{ rgb | 1 | Indicate that model works with RGB input images instead BGR ones. }"
|
||||
"{ padvalue | 114.0 | padding value. }"
|
||||
"{ paddingmode | 2 | Choose one of computation backends: "
|
||||
"0: resize to required input size without extra processing, "
|
||||
"1: Image will be cropped after resize, "
|
||||
"2: Resize image to the desired size while preserving the aspect ratio of original image }"
|
||||
"{ backend | 0 | Choose one of computation backends: "
|
||||
"0: automatically (by default), "
|
||||
"1: Halide language (http://halide-lang.org/), "
|
||||
"2: Intel's Deep Learning Inference Engine (https://software.intel.com/openvino-toolkit), "
|
||||
"3: OpenCV implementation, "
|
||||
"4: VKCOM, "
|
||||
"5: CUDA }"
|
||||
"{ target | 0 | Choose one of target computation devices: "
|
||||
"0: CPU target (by default), "
|
||||
"1: OpenCL, "
|
||||
"2: OpenCL fp16 (half-float precision), "
|
||||
"3: VPU, "
|
||||
"4: Vulkan, "
|
||||
"6: CUDA, "
|
||||
"7: CUDA fp16 (half-float preprocess) }"
|
||||
"{ async | 0 | Number of asynchronous forwards at the same time. "
|
||||
"Choose 0 for synchronous mode }";
|
||||
|
||||
void getClasses(std::string classesFile)
|
||||
{
|
||||
std::ifstream ifs(classesFile.c_str());
|
||||
if (!ifs.is_open())
|
||||
CV_Error(Error::StsError, "File " + classesFile + " not found");
|
||||
std::string line;
|
||||
while (std::getline(ifs, line))
|
||||
classes.push_back(line);
|
||||
}
|
||||
|
||||
void drawPrediction(int classId, float conf, int left, int top, int right, int bottom, Mat& frame)
|
||||
{
|
||||
rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 255, 0));
|
||||
|
||||
std::string label = format("%.2f", conf);
|
||||
if (!classes.empty())
|
||||
{
|
||||
CV_Assert(classId < (int)classes.size());
|
||||
label = classes[classId] + ": " + label;
|
||||
}
|
||||
|
||||
int baseLine;
|
||||
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
|
||||
|
||||
top = max(top, labelSize.height);
|
||||
rectangle(frame, Point(left, top - labelSize.height),
|
||||
Point(left + labelSize.width, top + baseLine), Scalar::all(255), FILLED);
|
||||
putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.5, Scalar());
|
||||
}
|
||||
|
||||
void yoloPostProcessing(
|
||||
std::vector<Mat>& outs,
|
||||
std::vector<int>& keep_classIds,
|
||||
std::vector<float>& keep_confidences,
|
||||
std::vector<Rect2d>& keep_boxes,
|
||||
float conf_threshold,
|
||||
float iou_threshold,
|
||||
const std::string& test_name)
|
||||
{
|
||||
// Retrieve
|
||||
std::vector<int> classIds;
|
||||
std::vector<float> confidences;
|
||||
std::vector<Rect2d> boxes;
|
||||
|
||||
if (test_name == "yolov8")
|
||||
{
|
||||
cv::transposeND(outs[0], {0, 2, 1}, outs[0]);
|
||||
}
|
||||
|
||||
if (test_name == "yolonas")
|
||||
{
|
||||
// outs contains 2 elemets of shape [1, 8400, 80] and [1, 8400, 4]. Concat them to get [1, 8400, 84]
|
||||
Mat concat_out;
|
||||
// squeeze the first dimension
|
||||
outs[0] = outs[0].reshape(1, outs[0].size[1]);
|
||||
outs[1] = outs[1].reshape(1, outs[1].size[1]);
|
||||
cv::hconcat(outs[1], outs[0], concat_out);
|
||||
outs[0] = concat_out;
|
||||
// remove the second element
|
||||
outs.pop_back();
|
||||
// unsqueeze the first dimension
|
||||
outs[0] = outs[0].reshape(0, std::vector<int>{1, 8400, 84});
|
||||
}
|
||||
|
||||
for (auto preds : outs)
|
||||
{
|
||||
preds = preds.reshape(1, preds.size[1]); // [1, 8400, 85] -> [8400, 85]
|
||||
for (int i = 0; i < preds.rows; ++i)
|
||||
{
|
||||
// filter out non object
|
||||
float obj_conf = (test_name == "yolov8" || test_name == "yolonas") ? 1.0f : preds.at<float>(i, 4) ;
|
||||
if (obj_conf < conf_threshold)
|
||||
continue;
|
||||
|
||||
Mat scores = preds.row(i).colRange((test_name == "yolov8" || test_name == "yolonas") ? 4 : 5, preds.cols);
|
||||
double conf;
|
||||
Point maxLoc;
|
||||
minMaxLoc(scores, 0, &conf, 0, &maxLoc);
|
||||
|
||||
conf = (test_name == "yolov8" || test_name == "yolonas") ? conf : conf * obj_conf;
|
||||
if (conf < conf_threshold)
|
||||
continue;
|
||||
|
||||
// get bbox coords
|
||||
float* det = preds.ptr<float>(i);
|
||||
double cx = det[0];
|
||||
double cy = det[1];
|
||||
double w = det[2];
|
||||
double h = det[3];
|
||||
|
||||
// [x1, y1, x2, y2]
|
||||
if (test_name == "yolonas"){
|
||||
boxes.push_back(Rect2d(cx, cy, w, h));
|
||||
} else {
|
||||
boxes.push_back(Rect2d(cx - 0.5 * w, cy - 0.5 * h,
|
||||
cx + 0.5 * w, cy + 0.5 * h));
|
||||
}
|
||||
classIds.push_back(maxLoc.x);
|
||||
confidences.push_back(static_cast<float>(conf));
|
||||
}
|
||||
}
|
||||
|
||||
// NMS
|
||||
std::vector<int> keep_idx;
|
||||
NMSBoxes(boxes, confidences, conf_threshold, iou_threshold, keep_idx);
|
||||
|
||||
for (auto i : keep_idx)
|
||||
{
|
||||
keep_classIds.push_back(classIds[i]);
|
||||
keep_confidences.push_back(confidences[i]);
|
||||
keep_boxes.push_back(boxes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @function main
|
||||
* @brief Main function
|
||||
*/
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
CommandLineParser parser(argc, argv, keys);
|
||||
parser.about("Use this script to run object detection deep learning networks using OpenCV.");
|
||||
if (parser.has("help"))
|
||||
{
|
||||
parser.printMessage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
CV_Assert(parser.has("model"));
|
||||
CV_Assert(parser.has("yolo"));
|
||||
// if model is default, use findFile to get the full path otherwise use the given path
|
||||
std::string weightPath = findFile(parser.get<String>("model"));
|
||||
std::string yolo_model = parser.get<String>("yolo");
|
||||
|
||||
float confThreshold = parser.get<float>("thr");
|
||||
float nmsThreshold = parser.get<float>("nms");
|
||||
//![preprocess_params]
|
||||
float paddingValue = parser.get<float>("padvalue");
|
||||
bool swapRB = parser.get<bool>("rgb");
|
||||
int inpWidth = parser.get<int>("width");
|
||||
int inpHeight = parser.get<int>("height");
|
||||
Scalar scale = parser.get<float>("scale");
|
||||
Scalar mean = parser.get<Scalar>("mean");
|
||||
ImagePaddingMode paddingMode = static_cast<ImagePaddingMode>(parser.get<int>("paddingmode"));
|
||||
//![preprocess_params]
|
||||
|
||||
// check if yolo model is valid
|
||||
if (yolo_model != "yolov5" && yolo_model != "yolov6"
|
||||
&& yolo_model != "yolov7" && yolo_model != "yolov8"
|
||||
&& yolo_model != "yolox" && yolo_model != "yolonas")
|
||||
CV_Error(Error::StsError, "Invalid yolo model: " + yolo_model);
|
||||
|
||||
// get classes
|
||||
if (parser.has("classes"))
|
||||
{
|
||||
getClasses(findFile(parser.get<String>("classes")));
|
||||
}
|
||||
|
||||
// load model
|
||||
//![read_net]
|
||||
Net net = readNet(weightPath);
|
||||
int backend = parser.get<int>("backend");
|
||||
net.setPreferableBackend(backend);
|
||||
net.setPreferableTarget(parser.get<int>("target"));
|
||||
//![read_net]
|
||||
|
||||
VideoCapture cap;
|
||||
Mat img;
|
||||
bool isImage = false;
|
||||
bool isCamera = false;
|
||||
|
||||
// Check if input is given
|
||||
if (parser.has("input"))
|
||||
{
|
||||
String input = parser.get<String>("input");
|
||||
// Check if the input is an image
|
||||
if (input.find(".jpg") != String::npos || input.find(".png") != String::npos)
|
||||
{
|
||||
img = imread(findFile(input));
|
||||
if (img.empty())
|
||||
{
|
||||
CV_Error(Error::StsError, "Cannot read image file: " + input);
|
||||
}
|
||||
isImage = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
cap.open(input);
|
||||
if (!cap.isOpened())
|
||||
{
|
||||
CV_Error(Error::StsError, "Cannot open video " + input);
|
||||
}
|
||||
isCamera = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int cameraIndex = parser.get<int>("device");
|
||||
cap.open(cameraIndex);
|
||||
if (!cap.isOpened())
|
||||
{
|
||||
CV_Error(Error::StsError, cv::format("Cannot open camera #%d", cameraIndex));
|
||||
}
|
||||
isCamera = true;
|
||||
}
|
||||
|
||||
// image pre-processing
|
||||
//![preprocess_call]
|
||||
Size size(inpWidth, inpHeight);
|
||||
Image2BlobParams imgParams(
|
||||
scale,
|
||||
size,
|
||||
mean,
|
||||
swapRB,
|
||||
CV_32F,
|
||||
DNN_LAYOUT_NCHW,
|
||||
paddingMode,
|
||||
paddingValue);
|
||||
|
||||
// rescale boxes back to original image
|
||||
Image2BlobParams paramNet;
|
||||
paramNet.scalefactor = scale;
|
||||
paramNet.size = size;
|
||||
paramNet.mean = mean;
|
||||
paramNet.swapRB = swapRB;
|
||||
paramNet.paddingmode = paddingMode;
|
||||
//![preprocess_call]
|
||||
|
||||
//![forward_buffers]
|
||||
std::vector<Mat> outs;
|
||||
std::vector<int> keep_classIds;
|
||||
std::vector<float> keep_confidences;
|
||||
std::vector<Rect2d> keep_boxes;
|
||||
std::vector<Rect> boxes;
|
||||
//![forward_buffers]
|
||||
|
||||
Mat inp;
|
||||
while (waitKey(1) < 0)
|
||||
{
|
||||
|
||||
if (isCamera)
|
||||
cap >> img;
|
||||
if (img.empty())
|
||||
{
|
||||
std::cout << "Empty frame" << std::endl;
|
||||
waitKey();
|
||||
break;
|
||||
}
|
||||
//![preprocess_call_func]
|
||||
inp = blobFromImageWithParams(img, imgParams);
|
||||
//![preprocess_call_func]
|
||||
|
||||
//![forward]
|
||||
net.setInput(inp);
|
||||
net.forward(outs, net.getUnconnectedOutLayersNames());
|
||||
//![forward]
|
||||
|
||||
//![postprocess]
|
||||
yoloPostProcessing(
|
||||
outs, keep_classIds, keep_confidences, keep_boxes,
|
||||
confThreshold, nmsThreshold,
|
||||
yolo_model);
|
||||
//![postprocess]
|
||||
|
||||
// covert Rect2d to Rect
|
||||
//![draw_boxes]
|
||||
for (auto box : keep_boxes)
|
||||
{
|
||||
boxes.push_back(Rect(cvFloor(box.x), cvFloor(box.y), cvFloor(box.width - box.x), cvFloor(box.height - box.y)));
|
||||
}
|
||||
|
||||
paramNet.blobRectsToImageRects(boxes, boxes, img.size());
|
||||
|
||||
for (size_t idx = 0; idx < boxes.size(); ++idx)
|
||||
{
|
||||
Rect box = boxes[idx];
|
||||
drawPrediction(keep_classIds[idx], keep_confidences[idx], box.x, box.y,
|
||||
box.width + box.x, box.height + box.y, img);
|
||||
}
|
||||
|
||||
const std::string kWinName = "Yolo Object Detector";
|
||||
namedWindow(kWinName, WINDOW_NORMAL);
|
||||
imshow(kWinName, img);
|
||||
//![draw_boxes]
|
||||
|
||||
outs.clear();
|
||||
keep_classIds.clear();
|
||||
keep_confidences.clear();
|
||||
keep_boxes.clear();
|
||||
boxes.clear();
|
||||
|
||||
if (isImage)
|
||||
{
|
||||
waitKey();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user