feat: Enhance documentation and clean up code structure
- Added comprehensive documentation for the unified screenshot and score recording process in the new markdown file. - Improved existing markdown files by adding spacing for better readability. - Cleaned up code by removing unnecessary blank lines across various scripts, enhancing overall code clarity. - Updated configuration settings to reflect recent changes and ensure consistency across the project. - Enhanced logging mechanisms to support better debugging and monitoring.
This commit is contained in:
1 parent
e68f1dd3de
commit
f92933641f
61 files changed
+103695
-1485
No files matched your search
+168
-16
@@ -1085,14 +1085,25 @@ class underlying_operations:
|
||||
|
||||
img_np = np.array(img1)
|
||||
|
||||
# 用于收集置信度信息
|
||||
ocr_confidence_info = None
|
||||
|
||||
if getattr(self, "ocr_type", None) == "easyocr":
|
||||
result = self.ocr.readtext(img_np)
|
||||
text = self.extract_ocr_content(result)
|
||||
extract_result = self.extract_ocr_content(result, return_confidence=True)
|
||||
if isinstance(extract_result, tuple):
|
||||
text, ocr_confidence_info = extract_result
|
||||
else:
|
||||
text = extract_result
|
||||
else:
|
||||
result = self.ocr.ocr(img_np)
|
||||
if result == [None]:
|
||||
return ""
|
||||
text = self.extract_ocr_content(result)
|
||||
extract_result = self.extract_ocr_content(result, return_confidence=True)
|
||||
if isinstance(extract_result, tuple):
|
||||
text, ocr_confidence_info = extract_result
|
||||
else:
|
||||
text = extract_result
|
||||
|
||||
# Initialize variables to avoid unbound variable issues
|
||||
img1 = img # img1 is used later, initialize from img
|
||||
@@ -1114,14 +1125,22 @@ class underlying_operations:
|
||||
|
||||
if getattr(self, "ocr_type", None) == "easyocr":
|
||||
result2 = self.ocr.readtext(np.array(img2))
|
||||
text2 = self.extract_ocr_content(result2)
|
||||
extract_result2 = self.extract_ocr_content(result2, return_confidence=True)
|
||||
if isinstance(extract_result2, tuple):
|
||||
text2, ocr_confidence_info = extract_result2
|
||||
else:
|
||||
text2 = extract_result2
|
||||
if text2:
|
||||
text = text2
|
||||
result = result2
|
||||
else:
|
||||
result2 = self.ocr.ocr(np.array(img2))
|
||||
if result2 != [None]:
|
||||
text2 = self.extract_ocr_content(result2)
|
||||
extract_result2 = self.extract_ocr_content(result2, return_confidence=True)
|
||||
if isinstance(extract_result2, tuple):
|
||||
text2, ocr_confidence_info = extract_result2
|
||||
else:
|
||||
text2 = extract_result2
|
||||
if text2:
|
||||
text = text2
|
||||
result = result2
|
||||
@@ -1152,20 +1171,41 @@ class underlying_operations:
|
||||
img_for_attempt, operation_type="ocr"
|
||||
)
|
||||
|
||||
# 保存OCR结果后的调试图像(带OCR文本结果)
|
||||
# 保存OCR结果后的调试图像(带OCR文本结果和置信度)
|
||||
ocr_result_preview = (text or text2 or "").strip()[:50] if (text or text2) else ""
|
||||
extra_info_dict = {
|
||||
"mode": change_color,
|
||||
"text_len": len(text or text2 or ""),
|
||||
"text_preview": ocr_result_preview,
|
||||
"used_img2": bool(img2 is not None and text2 and text2.strip())
|
||||
}
|
||||
# 添加置信度信息
|
||||
if ocr_confidence_info:
|
||||
extra_info_dict["avg_confidence"] = round(ocr_confidence_info.get("avg_confidence", 0.0), 4)
|
||||
extra_info_dict["min_confidence"] = round(ocr_confidence_info.get("min_confidence", 0.0), 4)
|
||||
extra_info_dict["max_confidence"] = round(ocr_confidence_info.get("max_confidence", 0.0), 4)
|
||||
extra_info_dict["confidence_items"] = ocr_confidence_info.get("item_count", 0)
|
||||
|
||||
debug_path = douyin_screenshot_manager.save_debug_image(
|
||||
img_for_attempt,
|
||||
operation_type="ocr",
|
||||
picname=picname,
|
||||
step="after_ocr",
|
||||
extra_info={
|
||||
"mode": change_color,
|
||||
"text_len": len(text or text2 or ""),
|
||||
"text_preview": ocr_result_preview,
|
||||
"used_img2": bool(img2 is not None and text2 and text2.strip())
|
||||
}
|
||||
extra_info=extra_info_dict
|
||||
)
|
||||
|
||||
# 记录OCR置信度到日志
|
||||
if ocr_confidence_info and ocr_confidence_info.get("item_count", 0) > 0:
|
||||
try:
|
||||
logging.info(
|
||||
f"[OCR置信度] {picname}: "
|
||||
f"平均={ocr_confidence_info.get('avg_confidence', 0.0):.4f}, "
|
||||
f"最小={ocr_confidence_info.get('min_confidence', 0.0):.4f}, "
|
||||
f"最大={ocr_confidence_info.get('max_confidence', 0.0):.4f}, "
|
||||
f"项目数={ocr_confidence_info.get('item_count', 0)}"
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
if debug_path:
|
||||
try:
|
||||
logging.info(f"[调试图像] OCR结果保存: {debug_path}, 文本: {ocr_result_preview}")
|
||||
@@ -1219,16 +1259,41 @@ class underlying_operations:
|
||||
|
||||
return text
|
||||
|
||||
def extract_ocr_content(self, content=[]):
|
||||
"""对OCR识别到的内容进行取值和拼接,变成完整的一段内容"""
|
||||
def extract_ocr_content(self, content=[], return_confidence=False):
|
||||
"""对OCR识别到的内容进行取值和拼接,变成完整的一段内容
|
||||
|
||||
参数:
|
||||
content: OCR识别结果
|
||||
return_confidence: 是否返回置信度信息(默认False,保持向后兼容)
|
||||
|
||||
返回:
|
||||
如果return_confidence=False: 返回文本字符串
|
||||
如果return_confidence=True: 返回(text, confidence_info)元组,其中confidence_info是置信度信息字典
|
||||
"""
|
||||
ocr_result = content
|
||||
|
||||
# 用于收集置信度信息
|
||||
confidence_scores = []
|
||||
confidence_info = {
|
||||
"avg_confidence": 0.0,
|
||||
"min_confidence": 1.0,
|
||||
"max_confidence": 0.0,
|
||||
"item_count": 0
|
||||
}
|
||||
|
||||
def _extract_text_from_item(item):
|
||||
def _extract_text_and_confidence_from_item(item):
|
||||
"""提取文本和置信度"""
|
||||
text = ""
|
||||
confidence = None
|
||||
|
||||
# 兼容EasyOCR格式:[(bbox, text, confidence), ...] -> (bbox, text, confidence)
|
||||
if isinstance(item, (list, tuple)) and len(item) >= 2:
|
||||
# EasyOCR格式:item = (bbox, text, confidence),text在索引1
|
||||
# EasyOCR格式:item = (bbox, text, confidence),text在索引1,confidence在索引2
|
||||
if len(item) >= 3:
|
||||
confidence = item[2]
|
||||
if isinstance(item[1], str):
|
||||
return item[1]
|
||||
text = item[1]
|
||||
return text, confidence
|
||||
|
||||
# 兼容旧格式:[box, (text, score)]
|
||||
second = item[1]
|
||||
@@ -1301,6 +1366,56 @@ class underlying_operations:
|
||||
return None
|
||||
return None
|
||||
|
||||
def _extract_text_from_item(item):
|
||||
"""提取文本(原始逻辑,保持向后兼容)"""
|
||||
# 兼容EasyOCR格式:[(bbox, text, confidence), ...] -> (bbox, text, confidence)
|
||||
if isinstance(item, (list, tuple)) and len(item) >= 2:
|
||||
# EasyOCR格式:item = (bbox, text, confidence),text在索引1
|
||||
if isinstance(item[1], str):
|
||||
return item[1]
|
||||
|
||||
# 兼容旧格式:[box, (text, score)]
|
||||
second = item[1]
|
||||
if isinstance(second, (list, tuple)) and len(second) >= 1:
|
||||
if isinstance(second[0], str):
|
||||
return second[0]
|
||||
if isinstance(second, dict):
|
||||
for key in ("text", "rec_text", "transcription"):
|
||||
val = second.get(key)
|
||||
if isinstance(val, str) and val:
|
||||
return val
|
||||
if isinstance(second, str):
|
||||
return second
|
||||
|
||||
# 兼容新格式:dict / 对象
|
||||
if isinstance(item, dict):
|
||||
# 常见:rec_texts 为字符串列表
|
||||
for list_key in ("rec_texts", "texts", "text_list"):
|
||||
vals = item.get(list_key)
|
||||
if isinstance(vals, list) and vals:
|
||||
vals = [v for v in vals if isinstance(v, str) and v]
|
||||
if vals:
|
||||
return "".join(vals)
|
||||
for key in ("text", "rec_text", "transcription"):
|
||||
val = item.get(key)
|
||||
if isinstance(val, str) and val:
|
||||
return val
|
||||
|
||||
for attr in ("text", "rec_text", "transcription"):
|
||||
val = getattr(item, attr, None)
|
||||
if isinstance(val, str) and val:
|
||||
return val
|
||||
|
||||
# 对象属性里也可能是列表
|
||||
for list_attr in ("rec_texts", "texts", "text_list"):
|
||||
vals = getattr(item, list_attr, None)
|
||||
if isinstance(vals, list) and vals:
|
||||
vals = [v for v in vals if isinstance(v, str) and v]
|
||||
if vals:
|
||||
return "".join(vals)
|
||||
|
||||
return ""
|
||||
|
||||
extracted_items = []
|
||||
if isinstance(items, list):
|
||||
for idx, item in enumerate(items):
|
||||
@@ -1317,6 +1432,43 @@ class underlying_operations:
|
||||
|
||||
# 按从上到下、从左到右排序;解决同一行多个框的乱序拼接(例如倒计时两段数字)
|
||||
extracted_items.sort(key=lambda t: (t[0], t[1]))
|
||||
|
||||
# 如果不需要返回置信度,直接返回文本(保持向后兼容)
|
||||
if not return_confidence:
|
||||
return "".join([t[2] for t in extracted_items])
|
||||
|
||||
# 如果需要返回置信度,提取置信度信息
|
||||
if return_confidence:
|
||||
for item in items:
|
||||
confidence = None
|
||||
# EasyOCR格式:[(bbox, text, confidence), ...]
|
||||
if isinstance(item, (list, tuple)) and len(item) >= 3:
|
||||
confidence = item[2]
|
||||
# PaddleOCR格式:[box, (text, score)]
|
||||
elif isinstance(item, (list, tuple)) and len(item) >= 2:
|
||||
second = item[1]
|
||||
if isinstance(second, (list, tuple)) and len(second) >= 2:
|
||||
confidence = second[1]
|
||||
elif isinstance(second, dict):
|
||||
confidence = second.get("confidence") or second.get("score")
|
||||
|
||||
if confidence is not None:
|
||||
try:
|
||||
conf_float = float(confidence)
|
||||
confidence_scores.append(conf_float)
|
||||
confidence_info["min_confidence"] = min(confidence_info["min_confidence"], conf_float)
|
||||
confidence_info["max_confidence"] = max(confidence_info["max_confidence"], conf_float)
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
if confidence_scores:
|
||||
confidence_info["avg_confidence"] = sum(confidence_scores) / len(confidence_scores)
|
||||
confidence_info["item_count"] = len(confidence_scores)
|
||||
else:
|
||||
confidence_info["min_confidence"] = 0.0
|
||||
|
||||
return "".join([t[2] for t in extracted_items]), confidence_info
|
||||
|
||||
return "".join([t[2] for t in extracted_items])
|
||||
|
||||
def delay(self, seconds: float = 1):
|
||||
|
||||
Reference in new issue
Block a user