update laser cabration
This commit is contained in:
75
vision.py
75
vision.py
@@ -12,6 +12,81 @@ from maix import image
|
||||
import config
|
||||
from logger_manager import logger_manager
|
||||
|
||||
def save_calibration_image(frame, laser_pos, photo_dir=None):
|
||||
"""
|
||||
保存激光校准图像(带标注)
|
||||
在找到的激光点位置绘制圆圈,便于检查算法是否正确
|
||||
|
||||
Args:
|
||||
frame: 原始图像帧
|
||||
laser_pos: 找到的激光点坐标 (x, y)
|
||||
photo_dir: 照片存储目录,如果为None则使用 config.PHOTO_DIR
|
||||
|
||||
Returns:
|
||||
str: 保存的文件路径,如果保存失败则返回 None
|
||||
"""
|
||||
# 检查是否启用图像保存
|
||||
if not config.SAVE_IMAGE_ENABLED:
|
||||
return None
|
||||
|
||||
if photo_dir is None:
|
||||
photo_dir = config.PHOTO_DIR
|
||||
|
||||
try:
|
||||
# 确保照片目录存在
|
||||
try:
|
||||
if photo_dir not in os.listdir("/root"):
|
||||
os.mkdir(photo_dir)
|
||||
except:
|
||||
pass
|
||||
|
||||
# 生成文件名
|
||||
try:
|
||||
all_images = [f for f in os.listdir(photo_dir) if f.endswith(('.bmp', '.jpg', '.jpeg'))]
|
||||
img_count = len(all_images)
|
||||
except:
|
||||
img_count = 0
|
||||
|
||||
x, y = laser_pos
|
||||
filename = f"{photo_dir}/calibration_{int(x)}_{int(y)}_{img_count:04d}.bmp"
|
||||
|
||||
logger = logger_manager.logger
|
||||
if logger:
|
||||
logger.info(f"保存校准图像: {filename}, 激光点: ({x}, {y})")
|
||||
|
||||
# 转换图像为 OpenCV 格式以便绘制
|
||||
img_cv = image.image2cv(frame, False, False)
|
||||
|
||||
# 绘制激光点圆圈(用绿色圆圈标出找到的激光点)
|
||||
cv2.circle(img_cv, (int(x), int(y)), 10, (0, 255, 0), 2) # 外圈:绿色,半径10
|
||||
cv2.circle(img_cv, (int(x), int(y)), 5, (0, 255, 0), 2) # 中圈:绿色,半径5
|
||||
cv2.circle(img_cv, (int(x), int(y)), 2, (0, 255, 0), -1) # 中心点:绿色实心
|
||||
|
||||
# 可选:绘制十字线帮助定位
|
||||
cv2.line(img_cv,
|
||||
(int(x - 20), int(y)),
|
||||
(int(x + 20), int(y)),
|
||||
(0, 255, 0), 1) # 水平线
|
||||
cv2.line(img_cv,
|
||||
(int(x), int(y - 20)),
|
||||
(int(x), int(y + 20)),
|
||||
(0, 255, 0), 1) # 垂直线
|
||||
|
||||
# 转换回 MaixPy 图像格式并保存
|
||||
result_img = image.cv2image(img_cv, False, False)
|
||||
result_img.save(filename)
|
||||
|
||||
if logger:
|
||||
logger.debug(f"校准图像已保存: {filename}")
|
||||
|
||||
return filename
|
||||
except Exception as e:
|
||||
logger = logger_manager.logger
|
||||
if logger:
|
||||
logger.error(f"保存校准图像失败: {e}")
|
||||
import traceback
|
||||
logger.error(traceback.format_exc())
|
||||
return None
|
||||
|
||||
def detect_circle_v3(frame, laser_point=None):
|
||||
"""检测图像中的靶心(优先清晰轮廓,其次黄色区域)- 返回椭圆参数版本
|
||||
|
||||
Reference in New Issue
Block a user