1 Star 2 Fork 0

打补丁的狮子 / guitar_365_practice

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
tool_read_text_on_img.py 6.95 KB
一键复制 编辑 原始数据 按行查看 历史
import sys
import os
import json
from __defines__ import *
from resourcesmanager import *
from functions import Logger
import easyocr
import unicodedata
def is_chinese(char):
if "CJK" in unicodedata.name(char):
return True
else:
return False
def contain_chines(str):
for c in str:
if is_chinese(c):
return True
return False
reader = easyocr.Reader(["ch_sim", "en"], gpu=False)
CD_JSON_FILE_NAME = "cd.json"
files = [t.name for t in PracticeImageTypeEnum]
def read_result(result: list, ptype: PracticeImageTypeEnum) -> tuple:
# result = map(lambda x: x.upper(), result)
res = (-1, -1, "") # (起始时刻,节拍速率,描述)
Logger.debug(f"READED: {ptype.name}, {result}")
proc_result = result
sss = ""
for i in reversed(range(0, len(proc_result))):
proc_result[i] = proc_result[i].replace("二", "=", 99999)
proc_result[i] = proc_result[i].replace(" ", "", 99999)
proc_result[i] = proc_result[i].upper()
if contain_chines(proc_result[i]):
sss = sss + proc_result[i]
del proc_result[i]
start_index = -1
end_index = -1
res = (res[0], res[1], sss)
# contain = False
if (
PracticeImageTypeEnum.每天练习 == ptype
or PracticeImageTypeEnum.周一 == ptype
or PracticeImageTypeEnum.周二 == ptype
or PracticeImageTypeEnum.周三 == ptype
or PracticeImageTypeEnum.周四 == ptype
or PracticeImageTypeEnum.周五 == ptype
or PracticeImageTypeEnum.周六 == ptype
or PracticeImageTypeEnum.周天 == ptype
):
# 时间、频率和简介
for i in range(0, len(proc_result)):
if (
"D0" in proc_result[i]
or "C0" in proc_result[i]
or "CD" in proc_result[i]
) and start_index < 0:
start_index = i + 1
if PracticeImageTypeEnum.每天练习 == ptype:
if start_index > 0:
end_index = start_index + 1
else:
if "CHECK" in proc_result[i] and end_index < 0:
end_index = i - 1
elif PracticeImageTypeEnum.提示 == ptype:
# 读取简介
pass
Logger.debug(f"AFTER CLEAN ---1:", proc_result, start_index, end_index)
Logger.debug(
f"AFTER CLEAN ---2:",
start_index,
"=",
proc_result[start_index] if start_index > 0 else "",
"|",
end_index,
"=",
proc_result[end_index] if end_index > 0 else "",
"|",
sss[0:10] + "......",
)
if start_index >= 0:
try:
seconds = proc_result[start_index]
seconds = seconds.replace("~", "")
seconds = seconds.replace(".", ":")
seconds = seconds.replace(",", ":")
seconds = seconds.replace(";", ":")
seconds = seconds.replace(" ", "", 99999)
seconds = seconds.split(":")
if len(seconds) == 2:
seconds = int(seconds[0]) * 60 + int(seconds[1])
elif len(seconds) == 1:
seconds = int(seconds[0])
res = (seconds, res[1], res[2])
Logger.debug(f"AFTER CLEAN ---4:", proc_result)
except Exception as e:
Logger.warning(f"AFTER CLEAN ---5: EXCEPTION", e)
if end_index >= 0:
try:
rate = proc_result[end_index]
rate = rate.split("=")
if len(rate) == 2:
rate = rate[1]
elif len(rate) == 1:
rate = rate[0]
rate = int(rate)
res = (res[0], rate, res[2])
Logger.debug(f"AFTER CLEAN ---6:", proc_result)
except Exception as e:
Logger.warning(f"AFTER CLEAN ---7: EXCEPTION", e)
Logger.info(
f"AFTER CLEAN ---8: ({res[0], res[1], res[2][0:10]}...)",
)
return res
def read_cd_seconds_and_beatrate(is_elec: bool = True):
# Logger.debug(f"-------: {is_elec}")
cd_infos = {}
try:
with open(
os.path.join(
ELECTRIC_GUITAR_RESOURCES_ABS_DIR
if is_elec
else ACOUSTIC_GUITAR_RESOURCES_ABS_DIR,
CD_JSON_FILE_NAME,
),
"r",
encoding="utf-8",
) as f:
cd_infos = json.loads(f.read())
except Exception as e:
Logger.warning(e)
for i in range(1, 53):
if str(i) in cd_infos:
continue
with open(
os.path.join(
ELECTRIC_GUITAR_RESOURCES_ABS_DIR
if is_elec
else ACOUSTIC_GUITAR_RESOURCES_ABS_DIR,
CD_JSON_FILE_NAME,
),
"w",
encoding="utf-8",
) as f:
f.write(json.dumps(cd_infos, indent=4, ensure_ascii=False))
root_dir = os.path.join(
ELECTRIC_GUITAR_RESOURCES_ABS_DIR
if is_elec
else ACOUSTIC_GUITAR_RESOURCES_ABS_DIR,
RESUORECE_DIR_NAME,
f"第{i}周",
)
cd_infos[str(i)] = []
for f in files:
img_path = os.path.join(root_dir, f + ".jpg")
Logger.debug(f"READ: {img_path}:")
result = reader.readtext(img_path, detail=0)
if PracticeImageTypeEnum.每天练习.name == f:
res = read_result(result=result, ptype=PracticeImageTypeEnum.每天练习)
elif PracticeImageTypeEnum.周一.name == f:
res = read_result(result=result, ptype=PracticeImageTypeEnum.周一)
elif PracticeImageTypeEnum.周二.name == f:
res = read_result(result=result, ptype=PracticeImageTypeEnum.周二)
elif PracticeImageTypeEnum.周三.name == f:
res = read_result(result=result, ptype=PracticeImageTypeEnum.周三)
elif PracticeImageTypeEnum.周四.name == f:
res = read_result(result=result, ptype=PracticeImageTypeEnum.周四)
elif PracticeImageTypeEnum.周五.name == f:
res = read_result(result=result, ptype=PracticeImageTypeEnum.周五)
elif PracticeImageTypeEnum.周六.name == f:
res = read_result(result=result, ptype=PracticeImageTypeEnum.周六)
elif PracticeImageTypeEnum.周天.name == f:
res = read_result(result=result, ptype=PracticeImageTypeEnum.周天)
elif PracticeImageTypeEnum.提示.name == f:
res = read_result(result=result, ptype=PracticeImageTypeEnum.提示)
cd_infos[str(i)].append(res)
with open(
os.path.join(
ELECTRIC_GUITAR_RESOURCES_ABS_DIR
if is_elec
else ACOUSTIC_GUITAR_RESOURCES_ABS_DIR,
CD_JSON_FILE_NAME,
),
"w",
encoding="utf-8",
) as f:
f.write(json.dumps(cd_infos, indent=4, ensure_ascii=False))
return cd_infos
# read_cd_seconds_and_beatrate(True)
# read_cd_seconds_and_beatrate(False)
Python
1
https://gitee.com/PatchLion_admin/guitar_365_practice.git
git@gitee.com:PatchLion_admin/guitar_365_practice.git
PatchLion_admin
guitar_365_practice
guitar_365_practice
dev

搜索帮助