Interface_Recognition/recognition.py

110 lines
2.5 KiB
Python
Raw Normal View History

2025-05-17 12:54:11 +08:00
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),
2025-05-17 16:18:38 +08:00
(1930, 600, 2200, 665),
2025-05-17 12:54:11 +08:00
(1860, 805, 2200, 850),
2025-05-17 16:18:38 +08:00
(1930, 980, 2200, 1055),
2025-05-17 12:54:11 +08:00
(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))
# 打印结果
2025-05-17 16:31:46 +08:00
for i, (player_id, player_score) in enumerate(zip(results_id, results_score), 1):
print(f"玩家{i}", player_id, f"的评分为:", player_score)
2025-05-17 12:54:11 +08:00
2025-05-17 16:31:46 +08:00
# 找出最高评分玩家
max_val = 0.0
max_idx = 0
2025-05-20 17:51:49 +08:00
min_val = 20.0
2025-05-20 16:33:33 +08:00
min_idx = []
2025-05-17 16:31:46 +08:00
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
2025-05-20 16:33:33 +08:00
if val < min_val:
min_val = val
min_idx = [i]
elif val == min_val:
min_idx.append(i)
2025-05-17 16:31:46 +08:00
print("全场最高分:", max_val)
2025-05-20 18:03:11 +08:00
print("全场最高分玩家ID:")
print(results_id[max_idx])
2025-05-20 16:33:33 +08:00
print("全场最低分:", min_val)
print("全场最低分玩家ID:")
for idx in min_idx:
print(results_id[idx])
2025-05-17 12:54:11 +08:00
# 显示图像
2025-05-17 16:18:38 +08:00
plt.figure(figsize=(8, 6))
2025-05-19 19:38:28 +08:00
plt.title('玩家ID')
plt.axis('off')
2025-05-17 12:54:11 +08:00
for i, img in enumerate(images_rgb_id, 1):
plt.subplot(5, 2, i)
plt.imshow(img)
plt.axis('off')
2025-05-17 16:18:38 +08:00
plt.figure(figsize=(8, 6))
2025-05-19 19:38:28 +08:00
plt.title('玩家评分')
plt.axis('off')
2025-05-17 12:54:11 +08:00
for i, img in enumerate(images_rgb_score, 1):
plt.subplot(5, 2, i)
plt.imshow(img)
plt.axis('off')
plt.tight_layout()
2025-05-17 16:31:46 +08:00
plt.show()