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, result in enumerate(results_id, 1):
    print(f"玩家{i}名称:", result)

for i, result in enumerate(results_score, 1):
    print(f"玩家{i}游戏评分:", result)

# 显示图像
plt.figure(figsize=(8, 6))
for i, img in enumerate(images_rgb_id, 1):
    plt.subplot(5, 2, i)
    plt.imshow(img)
    plt.title('测试')
    plt.axis('off')

plt.figure(figsize=(8, 6))
for i, img in enumerate(images_rgb_score, 1):
    plt.subplot(5, 2, i)
    plt.imshow(img)
    plt.title('测试')
    plt.axis('off')

plt.tight_layout()
plt.show()

# 找出最高评分玩家
max_val = 0.0
max_idx = 0
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

print("最高评分:", max_val)
print("最高评分玩家ID:", results_id[max_idx])