ropensci / opencv

R bindings for OpenCV
https://docs.ropensci.org/opencv
Other
133 stars 27 forks source link

Include text detection example #17

Open JanMarvin opened 4 years ago

JanMarvin commented 4 years ago

Similar to the face detection I created R functions for text_detection, mostly copying around this sample code.

In a fun coding project I use it to obtain positions of text in images. These positions I can use to crop the larger image with magick to then use tesseracts ocr() on smaller pieces of the original image. (For more preprocessing a magick2opencv() and vice versa function would be nice to have!) [Edit:] A conversion function was added in https://github.com/JanMarvin/opencv/tree/conversion

jeroen commented 4 years ago

From the CI, it looks like some of these API's are quite recent and need to be included conditionally to make sure the package can still be built against older versions of libopencv...

JanMarvin commented 4 years ago

I could have a look if my limited c++ knowledge allows me to fix this. I wasn't really sure if this was something you wanted in opencv after all. Just let me know what you're thinking the next steps should be and I'll see what I can do. After all closing and ignoring the pr is always an option 😉

jeroen commented 4 years ago

You would have to do a little bit of research or trial-and-error on when the given headers or API calls were introduced and wrap them in macros like so:

https://github.com/ropensci/opencv/blob/976d4dad0e01c0a3ace5b65e2ea7b40bc2a1dd31/src/face.cpp#L63-L65

JanMarvin commented 4 years ago

Fixed the build. Requires OpenCV 3.4.3 or newer and a build with DNN enabled

jeroen commented 4 years ago

But you're not setting HAVE_OPENCV_DNN anywhere now? So it is disabled for all builds?

JanMarvin commented 4 years ago

It's defined in opencv.hpp.

Note: Since the c++ part is mostly based on a sample file of a BSD licensed library required for linking with your package anyways, I did not add the BSD header to the package LICENSE.md.

jeroen commented 4 years ago

Oh sorry I didn't know HAVE_OPENCV_DNN was defined by the library! So if the library defines that macro, do you still need to check for the version?

JanMarvin commented 4 years ago

Yeah, didn't realize it at first too, but you do not include it in r-backports on windows

JanMarvin commented 4 years ago

So if the library defines that macro, do you still need to check for the version?

Yes, the version check is still required. OpenCV 3.4.1 introduced Have_OPENCV_DNN, but cv::dnn::readNet() was introduced in 3.4.2. Possibly that version is sufficient, but iirc the sample code was included in 3.4.3 and since I didn't want to build 3.4.2 … :smile:

jeroen commented 4 years ago

OK. I tried to run the example but I can't find the frozen_east_text_detection.pb file? Where does this come from?

JanMarvin commented 4 years ago

It's a tensorflow model provided via this dropbox link

https://github.com/opencv/opencv_extra/blob/1c2150dd4f7475709bd4a0747331fef3c2492d6f/testdata/dnn/download_models.py#L302