This commit is contained in:
HuangHai
2026-01-12 16:53:43 +08:00
parent 4cef36dc96
commit f263c133b8
7 changed files with 441 additions and 758 deletions

View File

@@ -2,7 +2,8 @@ import logging
import os
import cv2
import numpy as np
import time
import time
import hashlib
from Apps.XinDianTu.Config.Setting import BOTTOM_SAFE_EXCLUDE_RATIO
from Config.Config import TEMP_IMAGE_DIR
@@ -11,24 +12,40 @@ logger = logging.getLogger(__name__)
def read_image(path):
"""读取图片,支持中文路径"""
"""读取图片,支持中文路径及鲁棒性检查"""
try:
return cv2.imdecode(np.fromfile(path, dtype=np.uint8), -1)
if not path or not os.path.exists(path):
return None
if os.path.getsize(path) == 0:
return None
return cv2.imdecode(np.fromfile(path, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
except Exception as e:
logger.info(f"Error reading image {path}: {e}")
logger.error(f"读取图片失败 {path}: {e}")
return None
def save_image(path, img):
"""保存图片,支持中文路径"""
"""保存图片,支持中文路径及鲁棒性检查"""
try:
if img is None:
return False
# 确保目录存在
dir_name = os.path.dirname(path)
if dir_name and not os.path.exists(dir_name):
os.makedirs(dir_name, exist_ok=True)
ext = os.path.splitext(path)[1]
if not ext:
ext = ".jpg"
cv2.imencode(ext, img)[1].tofile(path)
return True
success, encoded_img = cv2.imencode(ext, img)
if success:
encoded_img.tofile(path)
return True
return False
except Exception as e:
logger.error(f"Error saving image {path}: {e}")
logger.error(f"保存图片失败 {path}: {e}")
return False
@@ -672,6 +689,29 @@ def get_row_stats(gray):
return row_means, row_stds, edge_means
def clean_station_name(name):
"""
清理场站名称,移除结尾的省略号及多余空格,方便比对
"""
if not name: return ""
name = name.strip()
# 移除结尾的 . (通常是截断标识)
while name.endswith("."):
name = name[:-1]
return name.strip()
def get_file_md5(path):
"""计算文件的 MD5 值"""
if not os.path.exists(path):
return ""
hash_md5 = hashlib.md5()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def crop_cards_from_image(img_path, output_dir=None, save_debug=True):
"""
从图片中裁剪场站卡片