Closed jholar closed 4 years ago
Why did you put [0] behind the cv2.findContours()
? Try to remove that.
Still getting same error after removing [0]
Then try to figure out what shape is returned by the function. Just print the out put or run len(...)
function over it.
""" Crop background and transform perspective from the photo of page """ import numpy as np import cv2
from .helpers import *
def detection(image): """Finding Page."""
image_edges = _edges_detection(image, 200, 250)
# Close gaps between edges (double page clouse => rectangle kernel)
closed_edges = cv2.morphologyEx(image_edges,
cv2.MORPH_CLOSE,
np.ones((5, 11)))
# Countours
page_contour = _find_page_contours(closed_edges, resize(image))
# Recalculate to original scale
page_contour = page_contour.dot(ratio(image))
# Transform prespective
new_image = _persp_transform(image, page_contour)
return new_image
def _edges_detection(img, minVal, maxVal): """Preprocessing (gray, thresh, filter, border) + Canny edge detection.""" img = cv2.cvtColor(resize(img), cv2.COLOR_BGR2GRAY)
img = cv2.bilateralFilter(img, 9, 75, 75)
img = cv2.adaptiveThreshold(img, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 115, 4)
# Median blur replace center pixel by median of pixels under kelner
# => removes thin details
img = cv2.medianBlur(img, 11)
# Add black border - detection of border touching pages
img = cv2.copyMakeBorder(img, 5, 5, 5, 5,
cv2.BORDER_CONSTANT,
value=[0, 0, 0])
return cv2.Canny(img, minVal, maxVal)
def _four_corners_sort(pts): """Sort corners in order: top-left, bot-left, bot-right, top-right.""" diff = np.diff(pts, axis=1) summ = pts.sum(axis=1) return np.array([pts[np.argmin(summ)], pts[np.argmax(diff)], pts[np.argmax(summ)], pts[np.argmin(diff)]])
def _contour_offset(cnt, offset): """Offset contour because of 5px border.""" cnt += offset cnt[cnt < 0] = 0 return cnt
def _find_page_contours(edges, img): """Finding corner points of page contour.""" contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Finding biggest rectangle otherwise return original corners
height = edges.shape[0]
width = edges.shape[1]
MIN_COUNTOUR_AREA = height * width * 0.5
MAX_COUNTOUR_AREA = (width - 10) * (height - 10)
max_area = MIN_COUNTOUR_AREA
page_contour = np.array([[0, 0],
[0, height-5],
[width-5, height-5],
[width-5, 0]])
for cnt in contours:
perimeter = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.03 * perimeter, True)
# Page has 4 corners and it is convex
if (len(approx) == 4 and
cv2.isContourConvex(approx) and
max_area < cv2.contourArea(approx) < MAX_COUNTOUR_AREA):
max_area = cv2.contourArea(approx)
page_contour = approx[:, 0]
# Sort corners and offset them
page_contour = _four_corners_sort(page_contour)
return _contour_offset(page_contour, (-5, -5))
def _persp_transform(img, s_points): """Transform perspective from start points to target points."""
height = max(np.linalg.norm(s_points[0] - s_points[1]),
np.linalg.norm(s_points[2] - s_points[3]))
width = max(np.linalg.norm(s_points[1] - s_points[2]),
np.linalg.norm(s_points[3] - s_points[0]))
# Create target points
t_points = np.array([[0, 0],
[0, height],
[width, height],
[width, 0]], np.float32)
# getPerspectiveTransform() needs float32
if s_points.dtype != np.float32:
s_points = s_points.astype(np.float32)
M = cv2.getPerspectiveTransform(s_points, t_points)
return cv2.warpPerspective(img, M, (int(width), int(height)))
Sir actually i am not getting it this is the code of page.py that i am using please check and suggest
Please check and suggest now
Yes that's the file. But you didn't have to copy it here!
I said you should run the len()
function on the output of cv2.findContours(...)
function not the image. Just wrap the function into the length function and print the output. Or just print the output.
@Breta01 sir, one suggestion, it is not releated for this issue.
can you update readme for mnist dataset with this link https://www.kaggle.com/c/digit-recognizer/data
this one fine to train with the handwritting-ocr.
Thanks sir issue is solved program is running now but giving wrong output. I am passing test4.jpg as input image please help
I have the same error as Jholar
I am pretty sure you guys are using the pretrained model which will never work on your hand writing !!
@jholar @LauraOrozco I had that issue as well and I was able to fix it by changing
im2, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
to :
contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
I think it may be an opencv compatibility issue as per this thread on StackOverflow . - "the function cv2.findContours() has been changed to return only the contours and the hierarchy and not ret"
I'm on opencv-python 4.1.2.30
Yes, I am aware of that. There are some changes in the API from OpenCV 3 to 4. If you want you can send a pull request with the fix and update the python requirements.
Yes that's the file. But you didn't have to copy it here!
I said you should run the
len()
function on the output ofcv2.findContours(...)
function not the image. Just wrap the function into the length function and print the output. Or just print the output.
Sir, what do you mean by that, can you explain little briefly. i am getting the same error
@jholar @LauraOrozco I had that issue as well and I was able to fix it by changing
im2, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
to :
contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
I think it may be an opencv compatibility issue as per this thread on StackOverflow . - "the function cv2.findContours() has been changed to return only the contours and the hierarchy and not ret"
I'm on opencv-python 4.1.2.30
my opencv-python is 4.1.2 . still getting the same issue
Do we have any solution to this?
Yes, I am aware of that. There are some changes in the API from OpenCV 3 to 4. If you want you can send a pull request with the fix and update the python requirements.
I have fixed and send a pull request.
Please help me to solve this error urgently Thanks in advance. Python version = 3.6.8 Open CV = 3.4.5.20