nareshmlx/opencv-pr huggingface dataset:
**File:** modules/dnn/src/model.cpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -4,7 +4,6 @@
 
 #include "precomp.hpp"
 #include <algorithm>
-#include <iostream>
 #include <utility>
 #include <iterator>
 
@@ -37,9 +36,10 @@ struct Model::Impl
     virtual void setPreferableBackend(Backend backendId) { net.setPreferableBackend(backendId); }
```

Review comment:
I wrote it to keep consistent with the previous `text_deteciton.cpp`, and it gives users a choice to specify the output names. Maybe it is not necessary, and I have removed it.
**File:** samples/dnn/scene_text_detection.cpp
**Change Type:** added
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -0,0 +1,151 @@
+#include <iostream>
+#include <fstream>
+#include <regex>
+
+#include <opencv2/imgproc.hpp>
+#include <opencv2/highgui.hpp>
+#include <opencv2/dnn/dnn.hpp>
+
+using namespace cv;
```

Review comment:
Do I need to add some other information? Patent Name: 一种基于可微分二值化的实时文本检测方法 Publication Number: CN110781967A
**File:** samples/dnn/text_detection.cpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition
**Review Line:** 7

**Code Changes:**
```diff
-
-    More converted onnx text recognition models can be downloaded directly here:
+    Text recognition models can be downloaded directly here:
     Download link: https://drive.google.com/drive/folders/1cTbQ3nuZG-EKWak6emD_s8_hHXWz7lAr?usp=sharing
-    And these models taken from here:https://github.com/clovaai/deep-text-recognition-benchmark
+    and doc/tutorials/dnn/dnn_text_spotting/dnn_text_spotting.markdown
 
+    How to convert from pb to onnx:
+    Using classes from here: https://github.com/meijieru/crnn.pytorch/blob/master/models/crnn.py
     import torch
     from models.crnn import CRNN
```

Review comment:
I have put these information back to it.
**File:** modules/dnn/src/model.cpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -4,7 +4,6 @@
 
 #include "precomp.hpp"
 #include <algorithm>
-#include <iostream>
 #include <utility>
 #include <iterator>
 
@@ -37,9 +36,10 @@ struct Model::Impl
     virtual void setPreferableBackend(Backend backendId) { net.setPreferableBackend(backendId); }
```

Review comment:
> fillPoly It is a bad idea to use "drawing" functions in algorithms implementations.
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -1309,6 +1309,255 @@ CV__DNN_INLINE_NS_BEGIN
                              float confThreshold = 0.5f, float nmsThreshold = 0.0f);
      };
 
+
+/** @brief This class represents high-level API for text recognition networks.
+ *
+ * TextRecognitionModel allows to set params for preprocessing input image.
+ * TextRecognitionModel creates net from file with trained weights and config,
+ * sets preprocessing input, runs forward pass and return recognition result.
```

Review comment:
API looks over-complicated for common class. Not sure that we really need to support custom **contours** as part of such basic API.
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -1309,6 +1309,255 @@ CV__DNN_INLINE_NS_BEGIN
                              float confThreshold = 0.5f, float nmsThreshold = 0.0f);
      };
 
+
+/** @brief This class represents high-level API for text recognition networks.
+ *
+ * TextRecognitionModel allows to set params for preprocessing input image.
+ * TextRecognitionModel creates net from file with trained weights and config,
+ * sets preprocessing input, runs forward pass and return recognition result.
```

Review comment:
> Ptr<Voc> voc; Please keep only one "implementation" pointer (it is already in Model::impl). (perhaps "Bridge" design pattern should be applied here)
**File:** samples/dnn/scene_text_detection.cpp
**Change Type:** added
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -0,0 +1,151 @@
+#include <iostream>
+#include <fstream>
+#include <regex>
+
+#include <opencv2/imgproc.hpp>
+#include <opencv2/highgui.hpp>
+#include <opencv2/dnn/dnn.hpp>
+
+using namespace cv;
```

Review comment:
If there is a reference/link on patented DNN model in this patch then it is still better to provide such type of information near the corresponding link (instead of whole sample).
**File:** modules/dnn/src/model.cpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -4,7 +4,6 @@
 
 #include "precomp.hpp"
 #include <algorithm>
-#include <iostream>
 #include <utility>
 #include <iterator>
 
@@ -37,9 +36,10 @@ struct Model::Impl
     virtual void setPreferableBackend(Backend backendId) { net.setPreferableBackend(backendId); }
```

Review comment:
Is there any better solution? The official post-processing uses this function to do it > https://github.com/MhLiao/DB/blob/f08c44faa1b4790039381203a8079d73a77494b4/structure/representers/seg_detector_representer.py#L203
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -1309,6 +1309,255 @@ CV__DNN_INLINE_NS_BEGIN
                              float confThreshold = 0.5f, float nmsThreshold = 0.0f);
      };
 
+
+/** @brief This class represents high-level API for text recognition networks.
+ *
+ * TextRecognitionModel allows to set params for preprocessing input image.
+ * TextRecognitionModel creates net from file with trained weights and config,
+ * sets preprocessing input, runs forward pass and return recognition result.
```

Review comment:
The idea of adding this optional parameter is proposed by Vadim in the last months. I think it can be another choice for users?
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -1309,6 +1309,255 @@ CV__DNN_INLINE_NS_BEGIN
                              float confThreshold = 0.5f, float nmsThreshold = 0.0f);
      };
 
+
+/** @brief This class represents high-level API for text recognition networks.
+ *
+ * TextRecognitionModel allows to set params for preprocessing input image.
+ * TextRecognitionModel creates net from file with trained weights and config,
+ * sets preprocessing input, runs forward pass and return recognition result.
```

Review comment:
I moved `decodeType` into `Voc`, but I have noticed that you propose to update cv::dnn::Model class in https://github.com/opencv/opencv/pull/18711. Hence, I need to wait for that pr to be merged, and update my codes?
**File:** modules/dnn/src/model.cpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition
**Review Line:** 512

**Code Changes:**
```diff
+
+    inline
+    void setVocabulary(const std::vector<std::string>& inputVoc)
+    {
+        vocabulary = inputVoc;
+    }
+
+    inline
+    void setDecodeType(const std::string& type)
+    {
+        decodeType = type;
```

Review comment:
Just `vocabulary = voc;` should work fine.
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -1309,6 +1309,255 @@ CV__DNN_INLINE_NS_BEGIN
                              float confThreshold = 0.5f, float nmsThreshold = 0.0f);
      };
 
+
+/** @brief This class represents high-level API for text recognition networks.
+ *
+ * TextRecognitionModel allows to set params for preprocessing input image.
+ * TextRecognitionModel creates net from file with trained weights and config,
+ * sets preprocessing input, runs forward pass and return recognition result.
```

Review comment:
Main concern here: - this contours parameter is optional for User, but it is not optional for extending implementation for supporting of other DL models which can't work with contours.
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -1309,6 +1309,255 @@ CV__DNN_INLINE_NS_BEGIN
                              float confThreshold = 0.5f, float nmsThreshold = 0.0f);
      };
 
+
+/** @brief This class represents high-level API for text recognition networks.
+ *
+ * TextRecognitionModel allows to set params for preprocessing input image.
+ * TextRecognitionModel creates net from file with trained weights and config,
+ * sets preprocessing input, runs forward pass and return recognition result.
```

Review comment:
ok, I understant that, and have removed it.
**File:** samples/dnn/scene_text_recognition.cpp
**Change Type:** added
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition
**Review Line:** 55

**Code Changes:**
```diff
+    TextRecognitionModel recognizer(modelPath);
+
+    // Load vocabulary
+    CV_Assert(!vocPath.empty());
+    std::ifstream vocFile;
+    vocFile.open(samples::findFile(vocPath));
+    CV_Assert(vocFile.is_open());
+    String vocLine;
+    std::vector<String> vocabulary;
+    while (std::getline(vocFile, vocLine)) {
+        vocabulary.push_back(vocLine);
```

Review comment:
I wonder whether I need to add `samples::findFile()` whenever I open files? Do I need to change `TextRecognitionModel recognizer(modelPath)` into `TextRecognitionModel recognizer(samples::findFile(modelPath))` ? Does it slow the speed?
**File:** samples/dnn/scene_text_recognition.cpp
**Change Type:** added
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition
**Review Line:** 55

**Code Changes:**
```diff
+    TextRecognitionModel recognizer(modelPath);
+
+    // Load vocabulary
+    CV_Assert(!vocPath.empty());
+    std::ifstream vocFile;
+    vocFile.open(samples::findFile(vocPath));
+    CV_Assert(vocFile.is_open());
+    String vocLine;
+    std::vector<String> vocabulary;
+    while (std::getline(vocFile, vocLine)) {
+        vocabulary.push_back(vocLine);
```

Review comment:
`samples::findFile()` helps to use file by name (instead of full path) from `<opencv>/samples/data` location. Model file is not stored there (and no plans to put it there due its size).
**File:** modules/dnn/src/model.cpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -4,7 +4,6 @@
 
 #include "precomp.hpp"
 #include <algorithm>
-#include <iostream>
 #include <utility>
 #include <iterator>
 
@@ -37,9 +36,10 @@ struct Model::Impl
     virtual void setPreferableBackend(Backend backendId) { net.setPreferableBackend(backendId); }
```

Review comment:
Is there any `confidences` scoring in DB detection algo?
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -1309,6 +1309,255 @@ CV__DNN_INLINE_NS_BEGIN
                              float confThreshold = 0.5f, float nmsThreshold = 0.0f);
      };
 
+
+/** @brief This class represents high-level API for text recognition networks.
+ *
+ * TextRecognitionModel allows to set params for preprocessing input image.
+ * TextRecognitionModel creates net from file with trained weights and config,
+ * sets preprocessing input, runs forward pass and return recognition result.
```

Review comment:
There is an issue with ObjC binding generator with contours handling (`std::vector< std::vector<Point> >` type). I will take a look on this on next week.
**File:** modules/dnn/src/model.cpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -4,7 +4,6 @@
 
 #include "precomp.hpp"
 #include <algorithm>
-#include <iostream>
 #include <utility>
 #include <iterator>
 
@@ -37,9 +36,10 @@ struct Model::Impl
     virtual void setPreferableBackend(Backend backendId) { net.setPreferableBackend(backendId); }
```

Review comment:
https://github.com/HannibalAPE/opencv/blob/f59aa6d4ae76e08f199e56ae94235295e0380076/modules/dnn/src/model.cpp#L972 You can regard the return of `contourScore()` as a kind of `confidence`, but it is only used to filter some bad detection results. It is not the same meaning as those in general object detection algorithms.
**File:** modules/dnn/src/model.cpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -4,7 +4,6 @@
 
 #include "precomp.hpp"
 #include <algorithm>
-#include <iostream>
 #include <utility>
 #include <iterator>
 
@@ -37,9 +36,10 @@ struct Model::Impl
     virtual void setPreferableBackend(Backend backendId) { net.setPreferableBackend(backendId); }
```

Review comment:
I have removed it, and updated the `rotateVec` in `unclip`.
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition
**Review Line:** 1465

**Code Changes:**
```diff
+ *
+ * Configurable parameters:
+ * - (float) confThreshold - used to filter boxes by confidences, default: 0.5f
+ * - (float) nmsThreshold - used in non maximum suppression, default: 0.0f
+ */
+class CV_EXPORTS_W_SIMPLE TextDetectionModel_EAST : public TextDetectionModel
+{
+public:
+    CV_DEPRECATED_EXTERNAL  // avoid using in C++ code, will be moved to "protected" (need to fix bindings first)
+    TextDetectionModel_EAST();
+
```

Review comment:
Why we need to derive implementations for algorithms? Is seems to me that more optimal introduce a parameter which can indicate EAST or DB model.
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition
**Review Line:** 1465

**Code Changes:**
```diff
+ *
+ * Configurable parameters:
+ * - (float) confThreshold - used to filter boxes by confidences, default: 0.5f
+ * - (float) nmsThreshold - used in non maximum suppression, default: 0.0f
+ */
+class CV_EXPORTS_W_SIMPLE TextDetectionModel_EAST : public TextDetectionModel
+{
+public:
+    CV_DEPRECATED_EXTERNAL  // avoid using in C++ code, will be moved to "protected" (need to fix bindings first)
+    TextDetectionModel_EAST();
+
```

Review comment:
There are different returned results (type). Also there is different set of configuration parameters. It is really hard to properly handle with checks both/multiple sets in one instance. We should not create GOD classes.
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -1309,6 +1309,255 @@ CV__DNN_INLINE_NS_BEGIN
                              float confThreshold = 0.5f, float nmsThreshold = 0.0f);
      };
 
+
+/** @brief This class represents high-level API for text recognition networks.
+ *
+ * TextRecognitionModel allows to set params for preprocessing input image.
+ * TextRecognitionModel creates net from file with trained weights and config,
+ * sets preprocessing input, runs forward pass and return recognition result.
```

Review comment:
Actually `RotatedRect` doesn't work well with perspective transformations (like [this one](https://www.pngwing.com/en/free-png-pdprp)). Perhaps we need 4 points in API with strong order (bl, tl, tr, br - according to `targetVertices`) which should be used with `getPerspectiveTransform()` to get more accurate results. I will try to update API on this week.
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -1309,6 +1309,255 @@ CV__DNN_INLINE_NS_BEGIN
                              float confThreshold = 0.5f, float nmsThreshold = 0.0f);
      };
 
+
+/** @brief This class represents high-level API for text recognition networks.
+ *
+ * TextRecognitionModel allows to set params for preprocessing input image.
+ * TextRecognitionModel creates net from file with trained weights and config,
+ * sets preprocessing input, runs forward pass and return recognition result.
```

Review comment:
I can not open the above link. Can you share it with google drive?
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -1309,6 +1309,255 @@ CV__DNN_INLINE_NS_BEGIN
                              float confThreshold = 0.5f, float nmsThreshold = 0.0f);
      };
 
+
+/** @brief This class represents high-level API for text recognition networks.
+ *
+ * TextRecognitionModel allows to set params for preprocessing input image.
+ * TextRecognitionModel creates net from file with trained weights and config,
+ * sets preprocessing input, runs forward pass and return recognition result.
```

Review comment:
It is similar to side boxes of cube from [here](https://en.wikipedia.org/wiki/Perspective_(graphical)#/media/File:Perspective1.jpg).
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -1309,6 +1309,255 @@ CV__DNN_INLINE_NS_BEGIN
                              float confThreshold = 0.5f, float nmsThreshold = 0.0f);
      };
 
+
+/** @brief This class represents high-level API for text recognition networks.
+ *
+ * TextRecognitionModel allows to set params for preprocessing input image.
+ * TextRecognitionModel creates net from file with trained weights and config,
+ * sets preprocessing input, runs forward pass and return recognition result.
```

Review comment:
However, DL models may not work with perspective transformations (EAST output doesn't know anything about that) and they just detect rotated text. any thoughts?
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition

**Code Changes:**
```diff
@@ -1309,6 +1309,255 @@ CV__DNN_INLINE_NS_BEGIN
                              float confThreshold = 0.5f, float nmsThreshold = 0.0f);
      };
 
+
+/** @brief This class represents high-level API for text recognition networks.
+ *
+ * TextRecognitionModel allows to set params for preprocessing input image.
+ * TextRecognitionModel creates net from file with trained weights and config,
+ * sets preprocessing input, runs forward pass and return recognition result.
```

Review comment:
It depends on different DL models. Some methods can output irregular quadrilaterals. From my side, the perspective transformation is a temporary replacement, or just a choice for the four-point outputs. (rotated boxes and irregular quadrilaterals) There is a popular and fast text recognition algorithm `ASTER` which adopts `Thin Plate Spline Transformation` in its rectification network. (like [this](https://github.com/ayumiymk/aster.pytorch/blob/master/overview.png)). I am not sure whether the TPS transformation is implemented in OpenCV, maybe not? You can refer to [this](https://github.com/ayumiymk/aster.pytorch/blob/be670046c775b54de79766208f0c59321ae1eccf/lib/models/tps_spatial_transformer.py#L98). By the way, ASTER is also an algorithm from our lab, and we are glad to contribute it to OpenCV. However, there are some things to do before. 1) support TPS transformation 2) update LSTM in modules/dnn/src/onnx/onnx_importer.cpp we need to set [these parameters](https://github.com/opencv/opencv/blob/ef0eed8d3c63b64bacc755dc4ffe5358a2cf30a7/modules/dnn/src/onnx/onnx_importer.cpp#L904) non-zero, but it is not supported now. 3) support [GRU layer](https://pytorch.org/docs/stable/generated/torch.nn.GRU.html?highlight=gru#torch.nn.GRU) 4) ... I suppose to work on it after this PR.
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition
**Review Line:** 1408

**Code Changes:**
```diff
+     *
+     * Each result is quadrangle's 4 points in this order:
+     * - bottom-left
+     * - top-left
+     * - top-right
+     * - bottom-right
+     *
+     * Use cv::getPerspectiveTransform function to retrive image region without perspective transformations.
+     *
+     * @note If DL model doesn't support that kind of output then result may be derived from detectTextRectangles() output.
+     *
```

Review comment:
Added strong requirements for results to avoid "points reordering" in sample code.
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition
**Review Line:** 1421

**Code Changes:**
```diff
+     * @param[out] confidences array with detection confidences
+     */
+    CV_WRAP
+    void detect(
+            InputArray frame,
+            CV_OUT std::vector< std::vector<Point> >& detections,
+            CV_OUT std::vector<float>& confidences
+    ) const;
+
+    /** @overload */
+    CV_WRAP
```

Review comment:
Any thoughts about `Point` vs `Point2f`?
**File:** doc/tutorials/dnn/dnn_text_spotting/dnn_text_spotting.markdown
**Change Type:** added
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition
**Review Line:** 291

**Code Changes:**
```diff
+
+#### Test with an image
+Examples:
+```bash
+example_dnn_scene_text_recognition -mp=path/to/crnn_cs.onnx -i=path/to/an/image -rgb=1 -vp=/path/to/alphabet_94.txt
+example_dnn_scene_text_detection -mp=path/to/DB_TD500_resnet50.onnx -i=path/to/an/image -ih=736 -iw=736
+example_dnn_scene_text_spotting -dmp=path/to/DB_IC15_resnet50.onnx -rmp=path/to/crnn_cs.onnx -i=path/to/an/image -iw=1280 -ih=736 -rgb=1 -vp=/path/to/alphabet_94.txt
+example_dnn_text_detection -dmp=path/to/EAST.pb -rmp=path/to/crnn_cs.onnx -i=path/to/an/image -rgb=1 -vp=path/to/alphabet_94.txt
+```
+
+#### Test on public datasets
```

Review comment:
Actually, DB_TD500_resnet50.onnx and DB_IC15_resnet50.onnx are prepared for different datasets (i.e. TD500 and IC15) respectively, which aims to perform better performance on each benchmark. The recommended settings do not cover the above case (use DB_TD500_resnet50.onnx on IC15 images). If it is needed, I can train a new model with different datasets together. What is your opinion?
**File:** modules/dnn/include/opencv2/dnn/dnn.hpp
**Change Type:** modified
**Context:** PR #17570: [GSoC] High Level API and Samples for Scene Text Detection and Recognition
**Review Line:** 1421

**Code Changes:**
```diff
+     * @param[out] confidences array with detection confidences
+     */
+    CV_WRAP
+    void detect(
+            InputArray frame,
+            CV_OUT std::vector< std::vector<Point> >& detections,
+            CV_OUT std::vector<float>& confidences
+    ) const;
+
+    /** @overload */
+    CV_WRAP
```

Review comment:
I think `Point` is okay.