I have found the following code on this website:
import os
import os.path
import cv2
import glob
import imutils
CAPTCHA_IMAGE_FOLDER = \"generated_captcha_im
This is doing the wrong thing:
contours = contours[0] if imutils.is_cv2() else contours[1]
imutils.is_cv2()
is returning False
even though it should return True
. If you don't mind to remove this dependency, change to:
contours = contours[0]
I found out the reason. Probably, the tutorial you are following was published before OpenCV 4 was released. OpenCV 3 changed cv2.findContours(...) to return image, contours, hierarchy
, while OpenCV 2's cv2.findContours(...) and OpenCV 4's cv2.findContours(...) return contours, hierarchy
. Therefore, before OpenCV 4, it was correct to say that if you use OpenCV 2 it should be contours[0]
else contours[1]
. If you still want to have this "compatibility", you can change to:
contours = contours[1] if imutils.is_cv3() else contours[0]
In OpenCV4, cv2.findContours has only 2 return values. Contours being the FIRST value
contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
Note that I added underscore to let go of the other return value of hierarchy
【OpenCV 3 changed cv2.findContours(...) to return image, contours, hierarchy】 This content is very helpful for me. I adda new variable to the front and I fix all the error..
I wrote the same code in following way:
_, contours, hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
and my code worked. I think previously it was returning 2 variables now we have to unpack into three variables. If this doesn't work try the following:
_, contours, _ = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
this should work.
For more information, you can visit the OpenCV documentation page: https://docs.opencv.org/3.1.0/d4/d73/tutorial_py_contours_begin.html
I hope this will help you.
(x, y, w, h) = cv2.boundingRect(contour.astype(np.int))
This is because of opencv-python version 4.0.0. If you want to fix this without changing your code then downgrade opencv-python to version 3.4.9.31
Uninstall opencv-python
pip uninstall opencv-python
Install opencv-python==3.4.9.31
pip install opencv-python==3.4.9.31
If facing issue with function 'pointSetBoundingRect', you need to install 'opencv-python-headless'
pip install opencv-python-headless==3.4.9.31