Interface_Recognition/recognition.py

113 lines
2.6 KiB
Python

import cv2
import easyocr
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['simHei']
plt.rcParams['axes.unicode_minus'] = False
reader = easyocr.Reader(['ch_sim'], gpu=False)
image = cv2.imread('file/zj.jpg')
# 所有要提取的坐标区域
id = [
(460, 415, 800, 460),
(460, 610, 800, 655),
(550, 805, 800, 850),
(460, 995, 800, 1040),
(460, 1190, 800, 1235),
(1860, 415, 2200, 460),
(1930, 600, 2200, 665),
(1860, 805, 2200, 850),
(1930, 980, 2200, 1055),
(1860, 1190, 2200, 1235),
]
score = [
(335, 488, 465, 545),
(335, 680, 465, 740),
(335, 875, 465, 930),
(335, 1070, 465, 1125),
(335, 1260, 465, 1320),
(1710, 488, 1845, 545),
(1710, 680, 1845, 740),
(1710, 875, 1845, 930),
(1710, 1070, 1845, 1125),
(1710, 1260, 1845, 1320),
]
# 提取图像区域并转换为RGB
images_rgb_id = []
images_rgb_score = []
results_id = []
results_score = []
for (x1, y1, x2, y2) in id:
cropped = image[y1:y2, x1:x2]
rgb = cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB)
images_rgb_id.append(rgb)
results_id.append(reader.readtext(rgb, detail=0))
for (x1, y1, x2, y2) in score:
cropped = image[y1:y2, x1:x2]
rgb = cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB)
images_rgb_score.append(rgb)
results_score.append(reader.readtext(rgb, detail=0))
# 打印结果
for i, (player_id, player_score) in enumerate(zip(results_id, results_score), 1):
print(f"玩家{i}", player_id, f"的评分为:", player_score)
# 找出最高评分玩家
max_val = 0.0
max_idx = []
min_val = 20.0
min_idx = []
for i, texts in enumerate(results_score):
if not texts:
continue
try:
val = float(texts[0])
except ValueError:
continue
if val > max_val:
max_val = val
max_idx = [i]
elif val == max_val:
max_idx.append(i)
if val < min_val:
min_val = val
min_idx = [i]
elif val == min_val:
min_idx.append(i)
print("全场最高分:", max_val)
print("全场最高分玩家ID:")
for idx in max_idx:
print(results_id[idx])
print("全场最低分:", min_val)
print("全场最低分玩家ID:")
for idx in min_idx:
print(results_id[idx])
# 显示图像
plt.figure(figsize=(8, 6))
plt.title('玩家ID')
plt.axis('off')
for i, img in enumerate(images_rgb_id, 1):
plt.subplot(5, 2, i)
plt.imshow(img)
plt.axis('off')
plt.figure(figsize=(8, 6))
plt.title('玩家评分')
plt.axis('off')
for i, img in enumerate(images_rgb_score, 1):
plt.subplot(5, 2, i)
plt.imshow(img)
plt.axis('off')
plt.tight_layout()
plt.show()