随着现代图像处理技术和人工智能的迅猛发展,越来越多的研究者尝试将计算机视觉(CV)应用于教育领域,希望通过自动化阅卷减轻教师负担,提高教学质量。
传统的手动阅卷方式不仅繁琐且效率低下,还存在进度难以控制以及漏改、错登分数等问题。
相比之下,现代的“机器阅卷”则更为便捷高效。只需使用相机(如手机)拍摄试卷,即可快速获取成绩,并能将数据导入Excel表格方便管理。
接下来,我们将从实现角度来介绍一种简单的答题卡识别系统的运作原理。该系统主要分为以下几个步骤:
首先,我们需要导入必要的工具包,并进行一系列预处理工作:
python
import numpy as np
import argparse
import imutils
import cv2
然后,设置参数并加载图像:
```python
ap = argparse.ArgumentParser()
ap.addargument("-i", "--image", default="test01.png")
args = vars(ap.parse_args())
ANSWERKEY = {0: 1, 1: 4, 2: 0, 3: 3, 4: 1}
接着,定义一些辅助函数,例如确定四个坐标点的顺序:
python
def orderpoints(pts):
rect = np.zeros((4, 2), dtype="float32")
s = pts.sum(axis=1)
rect[0] = pts[np.argmin(s)]
rect[2] = pts[np.argmax(s)]
diff = np.diff(pts, axis=1)
rect[1] = pts[np.argmin(diff)]
rect[3] = pts[np.argmax(diff)]
return rect
之后,定义四点变换函数,将图像进行透视变换:
python
def fourpointtransform(image, pts):
rect = orderpoints(pts)
(tl, tr, br, bl) = rect
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))
dst = np.array([[0, 0], [maxWidth - 1, 0], [maxWidth - 1, maxHeight - 1], [0, maxHeight - 1]], dtype="float32")
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
return warped
然后,对图像进行边缘检测、轮廓提取和透视变换等处理,最后进行答题卡的识别和评分。
python
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLORBGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 75, 200)
cnts = cv2.findContours(edged.copy(), cv2.RETREXTERNAL, cv2.CHAINAPPROX_SIMPLE)[1]
docCnt = None if len(cnts) > 0: cnts = sorted(cnts, key=cv2.contourArea, reverse=True) for c in cnts: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.02 * peri, True) if len(approx) == 4: docCnt = approx break
warped = fourpointtransform(gray, docCnt.reshape(4, 2)) thresh = cv2.threshold(warped, 0, 255, cv2.THRESHBINARYINV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETREXTERNAL, cv2.CHAINAPPROX_SIMPLE)[1] questionCnts = []
for c in cnts: (x, y, w, h) = cv2.boundingRect(c) ar = w / float(h) if w >= 20 and h >= 20 and ar >= 0.9: questionCnts.append(c)
total = 0 correct = 0
for (q, i) in enumerate(np.arange(0, len(questionCnts), 5)): cnts = contours[i:i+5] bubbled = None for (j, c) in enumerate(cnts): mask = np.zeros(thresh.shape, dtype="uint8") cv2.drawContours(mask, [c], -1, 255, -1) mask = cv2.bitwiseand(thresh, thresh, mask=mask) total = cv2.countNonZero(mask) if bubbled is None or total > bubbled[0]: bubbled = (total, j) color = (0, 0, 255) k = ANSWERKEY[q] if k == bubbled[1]: color = (0, 255, 0) correct += 1 cv2.drawContours(warped, [cnts[k]], -1, color, 3)
score = (correct / 5.0) * 100 print("[INFO] score: {:.2f}%".format(score)) cv2.putText(warped, "{:.2f}%".format(score), (10, 30), cv2.FONTHERSHEYSIMPLEX, 0.9, (0, 0, 255), 2) cv2.imshow("Input", image) cv2.imshow("Output", warped) cv2.waitKey(0) ``` 通过这种方式,我们可以有效地实现自动化阅卷,显著提升工作效率。