OpenCV TypeError: contour is not a numpy array, nor scalar

I am trying to use OpenCV to extract tags from Nike images. This is the tutorial code taken from:

http://opencv-code.com/tutorials/ocr-ing-nikes-new-rsvp-program/

I changed a few lines of code a little, and there is no error in this part (I’m not sure if it works because I couldn’t start it completely).

When I run the command 'python a.py'. This error is displayed: -

Traceback (most recent call last): File "a.py", line 42, in <module> otcnt = [c for c in cnt if cv2.contourArea(c) < 100] TypeError: contour is not a numpy array, neither a scalar 

a.py: -

 #!/usr/bin/env python import numpy as np import cv2 import cv2.cv as cv def do_ocr(img0): pass if __name__ == "__main__": img0 = cv2.imread('nike-1.jpg') if img0 == None: import sys sys.exit() do_ocr(img0) img1 = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY) img2 = cv2.adaptiveThreshold(img1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 45, 0) size = np.size(img2) skel = np.zeros(img2.shape,np.uint8) ret,img2 = cv2.threshold(img2,127,255,0) element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3)) done = False while( not done): eroded = cv2.erode(img2,element) temp = cv2.dilate(eroded,element) temp = cv2.subtract(img2,temp) skel = cv2.bitwise_or(skel,temp) img2 = eroded.copy() zeros = size - cv2.countNonZero(img2) if zeros==size: done = True img3 = img2 img4 = cv2.copyMakeBorder(img3, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=0) cv2.floodFill(img4, None, (0,0), 255) img5 = cv2.erode(255-img4, np.ones((3,3), np.uint8), iterations=2) cnt = cv2.findContours(img5, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0] cnt = [c for c in cnt if cv2.contourArea(c) > 5000] mask = np.zeros(img0.shape[:2], np.uint8) cv2.drawContours(mask, cnt, -1, 255, -1) dst = img2 & mask cnt = cv2.findContours(dst.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) otcnt = [c for c in cnt if cv2.contourArea(c) < 100] cv2.drawContours(dst, otcnt, -1, 0, -1) api = tesseract.TessBaseAPI() api.Init(".", "eng", tesseract.OEM_DEFAULT) api.SetVariable("tessedit_char_whitelist", "#ABCDEFGHIJKLMNOPQRSTUVWXYZ") api.SetPageSegMode(tesseract.PSM_SINGLE_LINE) image = cv.CreateImageHeader(dst.shape[:2], cv.IPL_DEPTH_8U, 1) cv.SetData(image, dst.tostring(), dst.dtype.itemsize * dst.shape[1]) tesseract.SetCvImage(image, api) print api.GetUTF8Text().string() 

I am very new to programming in Python (and Python syntax) and I should still do this in Python. I will be very grateful if you can publish the full correct version or finger point, with which line of code you should replace any of them.

thanks

ps My first question on stackoverflow is so apologizing if you don't follow any agreement.

+4
source share
1 answer

This runtime error is caused when overriding cnt on line 42 as

 cnt = cv2.findContours(dst.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 

which you set is a tuple of two return values cv2.findContours . Look at your previous function call on line 37 as a guide. All you have to do is change line 42 to

 cnt = cv2.findContours(dst.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0] 

Your next problem is that you did not import tesseract .

+4
source

Source: https://habr.com/ru/post/1491247/


All Articles