OpenCV+Python识别机读卡
背景介绍
正常机读卡是通过读卡机读取识别结果的,目前OpenCV已经这么强大了,尝试着用OpenCV+Python来识别机读卡。要识别的机读卡长这样:
我们做以下操作:
1.识别答题卡中每题选中项结果。
不做以下操作:
1.不识别101-106题(这些题实际情况下经常用不到,如果要识别原理也一样)
实现思路
通过分析答题卡特征:
1.答题区域为整张图片最大轮廓,先找出答题区域。
2.答题区域分为6行,每行4组,第6行只有1组,我们暂不处理第6行,只处理前面5行。
3.给定每一行第一个选项中心点坐标,该行其余选项的中心点坐标可以推算出来。
4.通过找到每个选项中心点坐标,再加上选项宽高,就可以在答题区域绘出每个选项的范围。
5.通过计算每个选项范围图像里非0像素点个数:
单选题非0像素点最少的既是答案。
多选题结合阈值判断该选项是否选中。
6.输出完整答案。
实现步骤
1.图像预处理
将图片转灰度图、黑帽运算:移除干扰项、二值化突出轮廓、查找轮廓、找出答题区域。
import cv2
import numpy as np
# 1.读取图片并缩放
orginImg = cv2.imread("1.jpg")
size = ((int)(650*1.8), (int)(930*1.8)) # 尽可能将图片弄大一点,下面好处理
img = cv2.resize(orginImg, size)
#显示图像
def imshow(name,image):
scale_percent = 50 # 缩放比例
width = int(image.shape[1] * scale_percent / 100)
height = int(image.shape[0] * scale_percent / 100)
dim = (width, height)
resized_image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
cv2.imshow(name, resized_image)
imshow("1.orgin", img)
# 2.转灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imshow("2.gray", gray)
# 3.黑帽运算:移除干扰项
cvblackhat = cv2.morphologyEx(
gray, cv2.MORPH_BLACKHAT, cv2.getStructuringElement(cv2.MORPH_RECT, (15, 15)))
imshow("3.black", cvblackhat)
# 4.二值化突出轮廓,自动阈值范围 cv2.THRESH_BINARY|cv2.THRESH_OTSU
thresh = cv2.threshold(
cvblackhat, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
imshow("4.thresh", thresh)
# 5.提取轮廓,并在图上标记轮廓
cnts, hierarchy = cv2.findContours(
thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
mark = img.copy()
cv2.drawContours(mark, cnts, -1, (0, 0, 255), 2)
imshow("5.contours", mark)
# 6.提取我们感兴趣的部分(这里我们只需要答题部分) img[y:y+h, x:x+w]
roi = None
for (i, c) in enumerate(cnts):
(x, y, w, h) = cv2.boundingRect(c)
ar = w/float(h)
if w > 500 and h > 500 and ar > 0.9 and ar < 1.1:
roi = img[y:y+h, x:x+w]
break
imshow("5.roi", roi)
运行结果
2.查找每个选项的中心点坐标
这里每行起始点坐标和每个选项宽高,都是写的固定值(手动量出来的,本来想通过图像提取轮廓来取每个选项中心坐标点的,可是由于机读卡填图不规范,可能会影响轮廓提取,不是很靠谱,暂时没想到更好的办法)
# 7.查找每个选项的中心点坐标
# 思路:
# 通过分析:
# 1.答题区域分为6行,每行4组,第6行只有1组,我们暂不处理第6行,只处理前面5行。
# 2.只要给定每一行第一个选项中心坐标,该行其余选项的中心坐标可以推算出来。
# 3.通过找到每个选项中心点坐标,再加上选项宽高,就可以在答题区域绘出每个选项的范围。
# 4.通过计算每个选项范围图像里非0像素点个数,结合阈值判断该选项是否选中。
# 5.结合题目个数,遍历每个选项,构造出最终答案。
item = [34, 20] # 每个选项宽度(跟图形缩放有关系)
x_step = 44 # x方向行距(两个选项水平方向距离)
y_step = 28 # y方向行距(两个选项垂直方向距离)
blank = 92 # 每组间距(5个一组)水平方向距离
centers = [] # 每个选项的中心点坐标,用来框选选项
# 答题区域有5行多1组,这里只处理前面5行,最后一组暂不处理
startPonits = [(25, 44), (25, 216), (26, 392), (26, 566), (28, 744)]
for (i, p) in enumerate(startPonits):
temp = [] # 暂存该组选项坐标
start = list(p) # 该行起始点坐标
for g in range(0, 4, 1): # 每行有4组
if len(temp) > 0:
startx = temp[len(temp)-1][0] + blank # 最后一个选项的x坐标+每组间距
else:
startx = start[0]
start[0] = startx
for i in range(start[0], start[0]+5*x_step, x_step): # 水平5个选项
for j in range(start[1], start[1]+4*y_step, y_step): # 垂直4个选项
temp.append((i, j))
for (i, c) in enumerate(temp):
centers.append(c)
# 8.将选项绘制到答题区域
show = roi.copy()
for (i, (x, y)) in enumerate(centers):
left = x-(int)(item[0]/2)
top = y-(int)(item[1]/2)
# 绘选项区域矩形
cv2.rectangle(show, (left, top), (left +
item[0], top + item[1]), (0, 0, 255), -1)
# 绘制序号标签
cv2.putText(show, str(i+1), (left, top+10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
imshow("5.show", show)
运行结果
3.遍历每个选项,计算非0像素点个数
# 9.截取每个答题选项,并计算非0像素点个数
map = roi.copy()
map = cv2.cvtColor(map, cv2.COLOR_BGR2GRAY)
map = cv2.threshold(map, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
points = []
for (i, (x, y)) in enumerate(centers):
left = x-(int)(item[0]/2)
top = y-(int)(item[1]/2)
# 截取每个选项小图
item_img = map[top:top+item[1], left:left+item[0]]
count = cv2.countNonZero(item_img)
points.append(count)
cv2.imwrite("item/"+str(i+1)+"-"+str(count)+".jpg", item_img)
这里为了方便观看,将每个选项截取另存为成图像了,以索“引号+非0像素点个数”命名。实际过程中可以不另存为图像。
大概像这样:
4.整理答案
# 10.整理答案
answer = [] # 二维数组:保存每个题ABCD4个选项值
group = [] # 将点分组,每4个1组,对应每题的4个选项
for i in range(0, len(points), 1):
if len(group) > 0 and group.count(points[i]) > 0:
group.append(points[i]+1)
else:
group.append(points[i])
if (i+1) % 4 == 0:
answer.append(group)
group = []
def printItem(i, optoins):
question = {0: "A", 1: "B", 2: "C", 3: "D"}
index = 0
if i < 80:
# 单选题(非0像素点最少的既是答案)
an = min(optoins)
index = optoins.index(an)
print("第"+str(i+1)+"题"+question.get(index))
else:
# 多选题(根据阈值来判断是否选中)
ans = ""
for (j, p) in enumerate(options):
if p < 400:
index = optoins.index(p)
ans += question.get(index)
print("第"+str(i+1)+"题"+ans)
# 打印题目答案
for (i, options) in enumerate(answer):
printItem(i, options)
输出结果:
完整代码
import cv2
import numpy as np
# 1.读取图片并缩放
orginImg = cv2.imread("1.jpg")
size = ((int)(650*1.8), (int)(930*1.8)) # 尽可能将图片弄大一点,下面好处理
img = cv2.resize(orginImg, size)
# 显示图像
def imshow(name, image):
scale_percent = 50 # 缩放比例
width = int(image.shape[1] * scale_percent / 100)
height = int(image.shape[0] * scale_percent / 100)
dim = (width, height)
resized_image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
cv2.imshow(name, resized_image)
imshow("1.orgin", img)
# 2.转灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imshow("2.gray", gray)
# 3.黑帽运算:移除干扰项
cvblackhat = cv2.morphologyEx(
gray, cv2.MORPH_BLACKHAT, cv2.getStructuringElement(cv2.MORPH_RECT, (15, 15)))
imshow("3.black", cvblackhat)
# 4.二值化突出轮廓,自动阈值范围 cv2.THRESH_BINARY|cv2.THRESH_OTSU
thresh = cv2.threshold(
cvblackhat, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
imshow("4.thresh", thresh)
# 5.提取轮廓,并在图上标记轮廓
cnts, hierarchy = cv2.findContours(
thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
mark = img.copy()
cv2.drawContours(mark, cnts, -1, (0, 0, 255), 2)
imshow("5.contours", mark)
# 6.提取我们感兴趣的部分(这里我们只需要答题部分) img[y:y+h, x:x+w]
roi = None
for (i, c) in enumerate(cnts):
(x, y, w, h) = cv2.boundingRect(c)
ar = w/float(h)
if w > 500 and h > 500 and ar > 0.9 and ar < 1.1:
roi = img[y:y+h, x:x+w]
break
imshow("5.roi", roi)
# 7.查找每个选项的中心点坐标
# 思路:
# 通过分析:
# 1.答题区域分为6行,每行4组,第6行只有1组,我们暂不处理第6行,只处理前面5行。
# 2.只要给定每一行第一个选项中心坐标,该行其余选项的中心坐标可以推算出来。
# 3.通过找到每个选项中心点坐标,再加上选项宽高,就可以在答题区域绘出每个选项的范围。
# 4.通过计算每个选项范围图像里非0像素点个数,结合阈值判断该选项是否选中。
# 5.结合题目个数,遍历每个选项,构造出最终答案。
item = [34, 20] # 每个选项宽度(跟图形缩放有关系)
x_step = 44 # x方向行距(两个选项水平方向距离)
y_step = 28 # y方向行距(两个选项垂直方向距离)
blank = 92 # 每组间距(5个一组)水平方向距离
centers = [] # 每个选项的中心点坐标,用来框选选项
# 答题区域有5行多1组,这里只处理前面5行,最后一组暂不处理
startPonits = [(25, 44), (25, 216), (26, 392), (26, 566), (28, 744)]
for (i, p) in enumerate(startPonits):
temp = [] # 暂存该组选项坐标
start = list(p) # 该行起始点坐标
for g in range(0, 4, 1): # 每行有4组
if len(temp) > 0:
startx = temp[len(temp)-1][0] + blank # 最后一个选项的x坐标+每组间距
else:
startx = start[0]
start[0] = startx
for i in range(start[0], start[0]+5*x_step, x_step): # 水平5个选项
for j in range(start[1], start[1]+4*y_step, y_step): # 垂直4个选项
temp.append((i, j))
for (i, c) in enumerate(temp):
centers.append(c)
# 8.将选项绘制到答题区域
show = roi.copy()
for (i, (x, y)) in enumerate(centers):
left = x-(int)(item[0]/2)
top = y-(int)(item[1]/2)
# 绘选项区域矩形
cv2.rectangle(show, (left, top), (left +
item[0], top + item[1]), (0, 0, 255), -1)
# 绘制序号标签
cv2.putText(show, str(i+1), (left, top+10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
cv2.imshow("5.show", show)
# 9.截取每个答题选项,并计算非0像素点个数
map = roi.copy()
map = cv2.cvtColor(map, cv2.COLOR_BGR2GRAY)
map = cv2.threshold(map, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
points = []
for (i, (x, y)) in enumerate(centers):
left = x-(int)(item[0]/2)
top = y-(int)(item[1]/2)
# 截取每个选项小图
item_img = map[top:top+item[1], left:left+item[0]]
count = cv2.countNonZero(item_img)
points.append(count)
cv2.imwrite("item/"+str(i+1)+"-"+str(count)+".jpg", item_img)
# 10.整理答案
answer = [] # 二维数组:保存每个题ABCD4个选项值
group = [] # 将点分组,每4个1组,对应每题的4个选项
for i in range(0, len(points), 1):
if len(group) > 0 and group.count(points[i]) > 0:
group.append(points[i]+1)
else:
group.append(points[i])
if (i+1) % 4 == 0:
answer.append(group)
group = []
def printItem(i, optoins):
question = {0: "A", 1: "B", 2: "C", 3: "D"}
index = 0
if i < 80:
# 单选题(非0像素点最少的既是答案)
an = min(optoins)
index = optoins.index(an)
print("第"+str(i+1)+"题"+question.get(index))
else:
# 多选题(根据阈值来判断是否选中)
ans = ""
for (j, p) in enumerate(options):
if p < 400:
index = optoins.index(p)
ans += question.get(index)
print("第"+str(i+1)+"题"+ans)
# 打印题目答案
for (i, options) in enumerate(answer):
printItem(i, options)
cv2.waitKey(0)
cv2.destroyAllWindows()
2024-8-26 补充:识别准考证区域
在轮廓检测时,将准考证区域也选出来。然后用同样的方法,将准考证号每个选项标记出来,根据非0像素点来识别。
完整代码(包含与标准答案比对出成绩)
import cv2
# 1.读取图片并缩放
orginImg = cv2.imread("111.jpg")
size = ((int)(650*1.8), (int)(930*1.8)) # 尽可能将图片弄大一点,下面好处理
img = cv2.resize(orginImg, size)
# 显示图像
def imshow(name, image):
scale_percent = 50 # 缩放比例
width = int(image.shape[1] * scale_percent / 100)
height = int(image.shape[0] * scale_percent / 100)
dim = (width, height)
resized_image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
cv2.imshow(name, resized_image)
# 图像预处理:灰度化、二值化、提取轮廓、提取答题区域
def prepare(img):
# 1.原图
imshow("1.orgin", img)
# 2.转灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imshow("2.gray", gray)
# 3.黑帽运算:移除干扰项
cvblackhat = cv2.morphologyEx(
gray, cv2.MORPH_BLACKHAT, cv2.getStructuringElement(cv2.MORPH_RECT, (15, 15)))
imshow("3.black", cvblackhat)
# 4.二值化突出轮廓,自动阈值范围 cv2.THRESH_BINARY|cv2.THRESH_OTSU
thresh = cv2.threshold(
cvblackhat, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
imshow("4.thresh", thresh)
# 5.提取轮廓,并在图上标记轮廓
cnts, hierarchy = cv2.findContours(
thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
mark = img.copy()
cv2.drawContours(mark, cnts, -1, (0, 0, 255), 2)
imshow("5.contours", mark)
return cnts
# 提取我们感兴趣的部分(这里我们只需要准考证部分和答题部分) img[y:y+h, x:x+w]
cnts = prepare(img)
answer_area = None # 答题区域
zkzh_area = None # 准考证号区域
for (i, c) in enumerate(cnts):
(x, y, w, h) = cv2.boundingRect(c)
ar = w/float(h)
if w > 500 and h > 500 and ar > 0.9 and ar < 1.1:
if answer_area is None:
answer_area = img[y:y+h, x:x+w]
elif w > 200 and h > 100 and ar > 1.2 and ar < 1.4:
if zkzh_area is None:
zkzh_area = img[y:y+h, x:x+w]
if answer_area is not None and zkzh_area is not None:
break
imshow("5.answer_area", answer_area)
cv2.imshow("6.zkzh_area", zkzh_area)
# 解析准考证号
def get_zkzh(zkzh_area):
zkzh_item = [34, 20] # 每个选项宽度(跟图形缩放有关系)
zkzh_x_step = 45 # x方向行距(两个选项水平方向距离)
zkzh_y_step = 30 # y方向行距(两个选项垂直方向距离)
zkzh_centers = [] # 每个选项的中心点坐标,用来框选选项
zkzh_start_ponits = (28, 112)
for i in range(0, 11, 1): # 11位准考证号
startx = zkzh_start_ponits[0]+i*zkzh_x_step
for j in range(0, 10, 1): # 每位准考证号有10位数
starty = zkzh_start_ponits[1]+j*zkzh_y_step
zkzh_centers.append((startx, starty))
# 将选项绘制到准考证号
zkzh_show = zkzh_area.copy()
for (i, (x, y)) in enumerate(zkzh_centers):
left = x-(int)(zkzh_item[0]/2)
top = y-(int)(zkzh_item[1]/2)
# 绘选项区域矩形
cv2.rectangle(zkzh_show, (left, top), (left +
zkzh_item[0], top + zkzh_item[1]), (0, 0, 255), -1)
# 绘制序号标签
cv2.putText(zkzh_show, str(i+1), (left, top+10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
cv2.imshow("7.zkzh_show", zkzh_show)
# 截取每个准考证号项,并计算非0像素点个数
zkzh_map = zkzh_area.copy()
zkzh_map = cv2.cvtColor(zkzh_map, cv2.COLOR_BGR2GRAY)
zkzh_map = cv2.threshold(
zkzh_map, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
zkzh_points = []
for (i, (x, y)) in enumerate(zkzh_centers):
left = x-(int)(zkzh_item[0]/2)
top = y-(int)(zkzh_item[1]/2)
# 截取每个选项小图
item_img = zkzh_map[top:top+zkzh_item[1], left:left+zkzh_item[0]]
count = cv2.countNonZero(item_img)
zkzh_points.append(count)
# 10.整理准考证号
zkzh_nums = [] # 二维数组:保存每个题ABCD4个选项值
zkzh_group = [] # 将点分组,每4个1组,对应每题的4个选项
for i in range(0, len(zkzh_points), 1):
zkzh_group.append(zkzh_points[i])
if (i+1) % 10 == 0:
zkzh_nums.append(zkzh_group)
zkzh_group = []
zkzh_num = ""
for (i, zkzh_area) in enumerate(zkzh_nums):
an = min(zkzh_area)
index = zkzh_area.index(an)
zkzh_num += str(index)
return zkzh_num
print("准考证号:", get_zkzh(zkzh_area))
# ============================解析答题区域=================================#
# 7.查找每个选项的中心点坐标
# 思路:
# 通过分析:
# 1.答题区域分为6行,每行4组,第6行只有1组,我们暂不处理第6行,只处理前面5行。
# 2.只要给定每一行第一个选项中心坐标,该行其余选项的中心坐标可以推算出来。
# 3.通过找到每个选项中心点坐标,再加上选项宽高,就可以在答题区域绘出每个选项的范围。
# 4.通过计算每个选项范围图像里非0像素点个数,结合阈值判断该选项是否选中。
# 5.结合题目个数,遍历每个选项,构造出最终答案。
answer_item = [34, 20] # 每个选项宽度(跟图形缩放有关系)
answer_x_step = 44 # x方向行距(两个选项水平方向距离)
answer_y_step = 28 # y方向行距(两个选项垂直方向距离)
answer_blank = 92 # 每组间距(5个一组)水平方向距离
answer_centers = [] # 每个选项的中心点坐标,用来框选选项
# 答题区域有5行多1组,这里只处理前面5行,最后一组暂不处理
answer_start_ponits = [(25, 44), (25, 216), (26, 392), (26, 566), (28, 744)]
for (i, p) in enumerate(answer_start_ponits):
temp = [] # 暂存该组选项坐标
start = list(p) # 该行起始点坐标
for g in range(0, 4, 1): # 每行有4组
if len(temp) > 0:
startx = temp[len(temp)-1][0] + answer_blank # 最后一个选项的x坐标+每组间距
else:
startx = start[0]
start[0] = startx
for i in range(start[0], start[0]+5*answer_x_step, answer_x_step): # 水平5个选项
for j in range(start[1], start[1]+4*answer_y_step, answer_y_step): # 垂直4个选项
temp.append((i, j))
for (i, c) in enumerate(temp):
answer_centers.append(c)
# 8.将选项绘制到答题区域
answer_show = answer_area.copy()
for (i, (x, y)) in enumerate(answer_centers):
left = x-(int)(answer_item[0]/2)
top = y-(int)(answer_item[1]/2)
# 绘选项区域矩形
cv2.rectangle(answer_show, (left, top), (left +
answer_item[0], top + answer_item[1]), (0, 0, 255), -1)
# 绘制序号标签
cv2.putText(answer_show, str(i+1), (left, top+10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
cv2.imshow("7.answer_show", answer_show)
# 9.截取每个答题选项,并计算非0像素点个数
answer_map = answer_area.copy()
answer_map = cv2.cvtColor(answer_map, cv2.COLOR_BGR2GRAY)
answer_map = cv2.threshold(
answer_map, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
answer_points = []
for (i, (x, y)) in enumerate(answer_centers):
left = x-(int)(answer_item[0]/2)
top = y-(int)(answer_item[1]/2)
# 截取每个选项小图
item_img = answer_map[top:top+answer_item[1], left:left+answer_item[0]]
count = cv2.countNonZero(item_img)
answer_points.append(count)
# cv2.imwrite("item/"+str(i+1)+"-"+str(count)+".jpg", item_img)
# 9.截取每个答题选项,并计算非0像素点个数
answer_map = answer_area.copy()
answer_map = cv2.cvtColor(answer_map, cv2.COLOR_BGR2GRAY)
answer_map = cv2.threshold(
answer_map, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
answer_points = []
for (i, (x, y)) in enumerate(answer_centers):
left = x-(int)(answer_item[0]/2)
top = y-(int)(answer_item[1]/2)
# 截取每个选项小图
item_img = answer_map[top:top+answer_item[1], left:left+answer_item[0]]
count = cv2.countNonZero(item_img)
answer_points.append(count)
# 10.整理答案
answer = [] # 二维数组:保存每个题ABCD4个选项值
group = [] # 将点分组,每4个1组,对应每题的4个选项
for i in range(0, len(answer_points), 1):
if len(group) > 0 and group.count(answer_points[i]) > 0:
group.append(answer_points[i]+1)
else:
group.append(answer_points[i])
if (i+1) % 4 == 0:
answer.append(group)
group = []
# 最终结果
score_dic = {}
def printItem(i, optoins):
question = {0: "A", 1: "B", 2: "C", 3: "D"}
index = 0
if i < 80:
# 单选题(非0像素点最少的既是答案)
an = min(optoins)
index = optoins.index(an)
score_dic[i+1] = question.get(index)
# print("第"+str(i+1)+"题"+question.get(index))
else:
# 多选题(根据阈值来判断是否选中)
ans = ""
for (j, p) in enumerate(options):
if p < 400:
index = optoins.index(p)
ans += question.get(index)
score_dic[i+1] = ans
# print("第"+str(i+1)+"题"+ans)
# 打印题目答案
for (i, options) in enumerate(answer):
printItem(i, options)
print("填涂结果:")
print(score_dic)
# 与标准答案比较,得到最终得分
score = 0
answers = {1: "B", 2: "B", 3: "A", 4: "B", 5: "B", 6: "A", 7: "B", 8: "B", 9: "A", 10: "B", 11: "B", 12: "A", 13: "A", 14: "B", 15: "B", 16: "A", 17: "A", 18: "B", 19: "B", 20: "A", 21: "C", 22: "C", 23: "B", 24: "D", 25: "C", 26: "D", 27: "A", 28: "D", 29: "C", 30: "B", 31: "C", 32: "D", 33: "B", 34: "A", 35: "D", 36: "C", 37: "B", 38: "B", 39: "D", 40: "D", 41: "B", 42: "C", 43: "B", 44: "D", 45: "B", 46: "A", 47: "D", 48: "D", 49: "C", 50: "D", 51: "B", 52: "D", 53: "A", 54: "A", 55: "D", 56: "D", 57: "C", 58: "D", 59: "B", 60: "D", 61: "A", 62: "B", 63: "D", 64: "B", 65: "C", 66: "D", 67: "B", 68: "C", 69: "D", 70: "A", 71: "ABCD", 72: "ABCD", 73: "AC", 74: "BCD", 75: "BC", 76: "BD", 77: "ABCD", 78: "ABCD", 79: "ACD", 80: "ABD"}
print("标准答案:")
print(answers)
for (i, a) in enumerate(score_dic):
s = 0 # 每题得分
if i > 0 and i < 20:
s = 1.25 # 1-20题每题1.25分
elif i > 20 and i < 70:
s = 1 # 21-70题每题1分
elif i > 70 and i < 80:
s = 2.5 # 71-80题每题2.5分
if answers.get(i+1) == score_dic.get(i+1):
score += s
print("最终成绩:")
print(score)
cv2.waitKey(0)
cv2.destroyAllWindows()
运行效果