-
Notifications
You must be signed in to change notification settings - Fork 284
Text Detection model DB #175
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| cmake_minimum_required(VERSION 3.24) | ||
| set(project_name "opencv_zoo_text_detection_db") | ||
|
|
||
| PROJECT (${project_name}) | ||
|
|
||
| find_package(OpenCV ${OPENCV_VERSION} REQUIRED HINTS ${OPENCV_INSTALLATION_PATH}) | ||
| # Find OpenCV, you may need to set OpenCV_DIR variable | ||
| # to the absolute path to the directory containing OpenCVConfig.cmake file | ||
| # via the command line or GUI | ||
|
|
||
| set(OPENCV_VERSION "4.7.0") | ||
| set(OPENCV_INSTALLATION_PATH "" CACHE PATH "Where to look for OpenCV installation") | ||
|
|
||
| file(GLOB SourceFile | ||
| "demo.cpp") | ||
| # If the package has been found, several variables will | ||
| # be set, you can find the full list with descriptions | ||
| # in the OpenCVConfig.cmake file. | ||
| # Print some message showing some of them | ||
| message(STATUS "OpenCV library status:") | ||
| message(STATUS " config: ${OpenCV_DIR}") | ||
| message(STATUS " version: ${OpenCV_VERSION}") | ||
| message(STATUS " libraries: ${OpenCV_LIBS}") | ||
| message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}") | ||
|
|
||
| # Declare the executable target built from your sources | ||
| add_executable(${project_name} ${SourceFile}) | ||
|
|
||
| # Link your application with OpenCV libraries | ||
| target_link_libraries(${project_name} PRIVATE ${OpenCV_LIBS}) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| #include <iostream> | ||
|
|
||
| #include <opencv2/dnn.hpp> | ||
| #include <opencv2/imgproc.hpp> | ||
| #include <opencv2/highgui.hpp> | ||
|
|
||
| using namespace std; | ||
| using namespace cv; | ||
| using namespace dnn; | ||
|
|
||
| vector< pair<cv::dnn::Backend, cv::dnn::Target> > backendTargetPairs = { | ||
| std::make_pair<cv::dnn::Backend, cv::dnn::Target>(dnn::DNN_BACKEND_OPENCV, dnn::DNN_TARGET_CPU), | ||
| std::make_pair<cv::dnn::Backend, cv::dnn::Target>(dnn::DNN_BACKEND_CUDA, dnn::DNN_TARGET_CUDA), | ||
| std::make_pair<cv::dnn::Backend, cv::dnn::Target>(dnn::DNN_BACKEND_CUDA, dnn::DNN_TARGET_CUDA_FP16), | ||
| std::make_pair<cv::dnn::Backend, cv::dnn::Target>(dnn::DNN_BACKEND_TIMVX, dnn::DNN_TARGET_NPU), | ||
| std::make_pair<cv::dnn::Backend, cv::dnn::Target>(dnn::DNN_BACKEND_CANN, dnn::DNN_TARGET_NPU)}; | ||
|
|
||
|
|
||
| std::string keys = | ||
| "{ help h | | Print help message. }" | ||
| "{ model m | text_detection_DB_IC15_resnet18_2021sep.onnx | Usage: Set model type, defaults to text_detection_DB_IC15_resnet18_2021sep.onnx }" | ||
| "{ input i | | Usage: Path to input image or video file. Skip this argument to capture frames from a camera.}" | ||
| "{ width | 736 | Usage: Resize input image to certain width, default = 736. It should be multiple by 32.}" | ||
| "{ height | 736 | Usage: Resize input image to certain height, default = 736. It should be multiple by 32.}" | ||
| "{ binary_threshold | 0.3 | Usage: Threshold of the binary map, default = 0.3.}" | ||
| "{ polygon_threshold | 0.5 | Usage: Threshold of polygons, default = 0.5.}" | ||
| "{ max_candidates | 200 | Usage: Set maximum number of polygon candidates, default = 200.}" | ||
| "{ unclip_ratio | 2.0 | Usage: The unclip ratio of the detected text region, which determines the output size, default = 2.0.}" | ||
| "{ save s | true | Usage: Specify to save file with results (i.e. bounding box, confidence level). Invalid in case of camera input.}" | ||
| "{ viz v | true | Usage: Specify to open a new window to show results. Invalid in case of camera input.}" | ||
| "{ backend bt | 0 | Choose one of computation backends: " | ||
| "0: (default) OpenCV implementation + CPU, " | ||
| "1: CUDA + GPU (CUDA), " | ||
| "2: CUDA + GPU (CUDA FP16), " | ||
| "3: TIM-VX + NPU, " | ||
| "4: CANN + NPU}"; | ||
|
|
||
|
|
||
| class DB { | ||
| public: | ||
|
|
||
| DB(string modPath, Size inSize = Size(736, 736), float binThresh = 0.3, | ||
| float polyThresh = 0.5, int maxCand = 200, double unRatio = 2.0, | ||
| dnn::Backend bId = DNN_BACKEND_DEFAULT, dnn::Target tId = DNN_TARGET_CPU) : modelPath(modPath), inputSize(inSize), binaryThreshold(binThresh), | ||
| polygonThreshold(polyThresh), maxCandidates(maxCand), unclipRatio(unRatio), | ||
| backendId(bId), targetId(tId) | ||
| { | ||
| this->model = TextDetectionModel_DB(readNet(modelPath)); | ||
| this->model.setPreferableBackend(backendId); | ||
| this->model.setPreferableTarget(targetId); | ||
|
|
||
| this->model.setBinaryThreshold(binaryThreshold); | ||
| this->model.setPolygonThreshold(polygonThreshold); | ||
| this->model.setUnclipRatio(unclipRatio); | ||
| this->model.setMaxCandidates(maxCandidates); | ||
|
|
||
| this->model.setInputParams(1.0 / 255.0, inputSize, Scalar(122.67891434, 116.66876762, 104.00698793)); | ||
| } | ||
| pair< vector<vector<Point>>, vector<float> > infer(Mat image) { | ||
| CV_Assert(image.rows == this->inputSize.height && "height of input image != net input size "); | ||
| CV_Assert(image.cols == this->inputSize.width && "width of input image != net input size "); | ||
| vector<vector<Point>> pt; | ||
| vector<float> confidence; | ||
| this->model.detect(image, pt, confidence); | ||
| return make_pair< vector<vector<Point>> &, vector< float > &>(pt, confidence); | ||
| } | ||
|
|
||
| private: | ||
| string modelPath; | ||
| TextDetectionModel_DB model; | ||
| Size inputSize; | ||
| float binaryThreshold; | ||
| float polygonThreshold; | ||
| int maxCandidates; | ||
| double unclipRatio; | ||
| dnn::Backend backendId; | ||
| dnn::Target targetId; | ||
|
|
||
| }; | ||
|
|
||
| Mat visualize(Mat image, pair< vector<vector<Point>>, vector<float> >&results, double fps=-1, Scalar boxColor=Scalar(0, 255, 0), Scalar textColor=Scalar(0, 0, 255), bool isClosed=true, int thickness=2) | ||
| { | ||
| Mat output; | ||
| image.copyTo(output); | ||
| if (fps > 0) | ||
| putText(output, format("FPS: %.2f", fps), Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, textColor); | ||
| polylines(output, results.first, isClosed, boxColor, thickness); | ||
| return output; | ||
| } | ||
|
|
||
| int main(int argc, char** argv) | ||
| { | ||
| CommandLineParser parser(argc, argv, keys); | ||
|
|
||
| parser.about("Use this program to run Real-time Scene Text Detection with Differentiable Binarization in opencv Zoo using OpenCV."); | ||
| if (argc == 1 || parser.has("help")) | ||
|
fengyuentau marked this conversation as resolved.
Outdated
|
||
| { | ||
| parser.printMessage(); | ||
| return 0; | ||
| } | ||
|
|
||
| int backendTargetid = parser.get<int>("backend"); | ||
| String modelName = parser.get<String>("model"); | ||
|
|
||
| if (modelName.empty()) | ||
| { | ||
| CV_Error(Error::StsError, "Model file " + modelName + " not found"); | ||
| } | ||
|
|
||
| Size inpSize(parser.get<int>("width"), parser.get<int>("height")); | ||
| float binThresh = parser.get<float>("binary_threshold"); | ||
| float polyThresh = parser.get<float>("polygon_threshold"); | ||
| int maxCand = parser.get<int>("max_candidates"); | ||
| double unRatio = parser.get<float>("unclip_ratio"); | ||
| bool save = parser.get<bool>("save"); | ||
| bool viz = parser.get<float>("viz"); | ||
|
|
||
| DB model(modelName, inpSize, binThresh, polyThresh, maxCand, unRatio, backendTargetPairs[backendTargetid].first, backendTargetPairs[backendTargetid].second); | ||
|
|
||
| //! [Open a video file or an image file or a camera stream] | ||
| VideoCapture cap; | ||
| if (parser.has("input")) | ||
| cap.open(parser.get<String>("input")); | ||
| else | ||
| cap.open(0); | ||
| if (!cap.isOpened()) | ||
| CV_Error(Error::StsError, "Cannot opend video or file"); | ||
| Mat originalImage; | ||
| static const std::string kWinName = modelName; | ||
| while (waitKey(1) < 0) | ||
| { | ||
| cap >> originalImage; | ||
| if (originalImage.empty()) | ||
| { | ||
| cout << "Frame is empty" << endl; | ||
| waitKey(); | ||
| break; | ||
| } | ||
| int originalW = originalImage.cols; | ||
| int originalH = originalImage.rows; | ||
| double scaleHeight = originalH / double(inpSize.height); | ||
| double scaleWidth = originalW / double(inpSize.width); | ||
| Mat image; | ||
| resize(originalImage, image, inpSize); | ||
|
|
||
| // inference | ||
| TickMeter tm; | ||
| tm.start(); | ||
| pair< vector<vector<Point>>, vector<float> > results = model.infer(image); | ||
| tm.stop(); | ||
| auto x = results.first; | ||
| // Scale the results bounding box | ||
| for (auto &pts : results.first) | ||
| { | ||
| for (int i = 0; i < 4; i++) | ||
| { | ||
| pts[i].x = int(pts[i].x * scaleWidth); | ||
| pts[i].y = int(pts[i].y * scaleHeight); | ||
| } | ||
| } | ||
| originalImage = visualize(originalImage, results, tm.getFPS()); | ||
| tm.reset(); | ||
| if (parser.has("input")) | ||
| { | ||
| if (save) | ||
| { | ||
| cout << "Result image saved to result.jpg\n"; | ||
| imwrite("result.jpg", originalImage); | ||
| } | ||
| if (viz) | ||
| imshow(kWinName, originalImage); | ||
| } | ||
| else | ||
| imshow(kWinName, originalImage); | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.