I'm working on a line segmentation project from handwritten text image input. This openCV error being produced when I try to run the program. I have no experience working with openCV contours :
Traceback (most recent call last):
File "checkout.py", line 17, in <module>
crop = page.detection(image)
File "/Users/vishakh/mhr/word_segmentation-master-1/page.py", line 20, in detection
page_contour = _find_page_contours(closed_edges, resize(image))
File "/Users/vishakh/mhr/word_segmentation-master-1/page.py", line 67, in _find_page_contours
im2, contours, hierarchy = cv2.findContours(edges,
ValueError: not enough values to unpack (expected 3, got 2)
This is the code in checkout.py
import page
import words
from PIL import Image
import cv2
# User input page image
image = cv2.cvtColor(cv2.imread("test.jpg"), cv2.COLOR_BGR2RGB)
# Crop image and get bounding boxes
crop = page.detection(image)
boxes = words.detection(crop)
lines = words.sort_words(boxes)
# Saving the bounded words from the page image in sorted way
i = 0
for line in lines:
text = crop.copy()
for (x1, y1, x2, y2) in line:
# roi = text[y1:y2, x1:x2]
save = Image.fromarray(text[y1:y2, x1:x2])
# print(i)
save.save("segmented/segment" + str(i) + ".png")
i += 1
This is the detection function in pages.py where the error is being thrown at.
def detection(image):
"""Finding Page."""
# Edge detection
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
This is the _find_page_contours function where the third error is being thrown at:
def _find_page_contours(edges, img):
"""Finding corner points of page contour."""
im2, 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))