我正在尝试使用 Python 3.8 和 OpenCV 构建自己的气泡表 OMR 引擎。尽管进行了几天的调试,但我无法克服在单独裁剪气泡时发生的错误:
Traceback (most recent call last):
File "C:\Users\hsolatges\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\lib\shape_base.py", line 867, in split
len(indices_or_sections)
TypeError: object of type 'int' has no len()
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/autoQuiz/omr.py", line 80, in <module>
anwsers_array = tb.parse_anwsers(anwsers_field, MCQ.get('QUESTIONS'), MCQ.get('CHOICES'))
bubbles = get_bubbles(padded_img, questions, choices)
File "c:\autoQuiz\toolbox.py", line 81, in get_bubbles
rows = np.vsplit(img, questions)
File "<__array_function__ internals>", line 5, in vsplit
File "C:\Users\hsolatges\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\lib\shape_base.py", line 991, in vsplit
return split(ary, indices_or_sections, 0)
File "<__array_function__ internals>", line 5, in split
File "C:\Users\hsolatges\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\lib\shape_base.py", line 872, in split
raise ValueError(
ValueError: array split does not result in an equal division
由于气泡区域的大小是任意的,我尝试对其进行边缘填充,使其宽度和高度都是问题数/选择数 (ABCDE) 的倍数。不幸的是,它不能正常工作。除了tests/omr-1.jpg,其他都失败了。
这里是代码的摘录:
def to_next_multiple(n,b):
return int(ceil(n/b) * b)
def pad_image(img, questions, choices):
w, h = img.shape[:2]
w_final, h_final, = to_next_multiple(w, choices), to_next_multiple(h, questions)
w_padding, h_padding = max(0, w-w_final), max(0, h-h_final)
padded_img = np.pad(img, ((0, h_padding), (0, w_padding)), 'edge')
return padded_img
def get_bubbles(img, questions, choices):
bubbles = []
rows = np.vsplit(img, questions)
for row in rows:
cells = np.hsplit(row, choices)
bubbles.append(cells)
return bubbles
def parse_anwsers(img, questions, choices):
# Otsu's thresholding after Gaussian filtering
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
blurred = cv.GaussianBlur(gray, (5,5), 0)
retValue, thresh = cv.threshold(blurred, 0, 255, cv.THRESH_BINARY+cv.THRESH_OTSU)
padded_img = pad_image(thresh, questions, choices)
w, h = padded_img.shape[:2]
# Debugging
print(f'width: {w}, w_division: {w/choices}')
print(f'height: {h}, h_division: {h/questions}')
bubbles = get_bubbles(padded_img, questions, choices)
answers_array = bubbles
return answers_array
可以在这里找到回购:https ://github.com/hsolatges/autoQuiz
我怎样才能始终如一地准备好成为 np.vsplit/np.hsplit 图像?