提交 0e55beae authored 作者: 陈泽健's avatar 陈泽健

补充对于rtsp流抓取一帧保存为图片,然后再进行对应的亮度判断。

上级 20f6a93d
......@@ -81,7 +81,6 @@ def compare_brightness(light_down_path, light_on_path, threshold=50):
logging.error(f"对比亮度时发生错误: {e}", exc_info=True)
return False
# if __name__ == '__main__':
# logging.info("开始对比亮度")
#
......@@ -104,3 +103,105 @@ def compare_brightness(light_down_path, light_on_path, threshold=50):
# logging.info("灯光已成功打开")
# else:
# logging.error("灯光未成功打开")
import cv2
import numpy as np
def compare_images_feature_matching(image1_path, image2_path):
"""
使用特征匹配对比两张图片的相似度。
"""
try:
image1 = cv2.imread(image1_path, cv2.IMREAD_GRAYSCALE)
image2 = cv2.imread(image2_path, cv2.IMREAD_GRAYSCALE)
# 初始化ORB检测器
orb = cv2.ORB_create()
# 查找关键点和描述符
kp1, des1 = orb.detectAndCompute(image1, None)
kp2, des2 = orb.detectAndCompute(image2, None)
# BFMatcher对象
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# 匹配描述符
matches = bf.match(des1, des2)
# 按距离排序
matches = sorted(matches, key=lambda x: x.distance)
# 计算匹配点的数量
num_matches = len(matches)
logging.info(f"匹配点数量: {num_matches}")
# 判断匹配点数量是否在可接受范围内
return num_matches > 10 # 可以根据需要调整阈值
except Exception as e:
logging.error(f"对比图片时发生错误: {e}", exc_info=True)
return False
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logging.info("开始对比图片")
image1_path = r'D:\GithubData\自动化\ubains-module-test\预定系统\Base\暗图.jpg'
image2_path = r'D:\GithubData\自动化\ubains-module-test\预定系统\Base\亮图.jpg'
if not os.path.exists(image1_path):
logging.error(f"图片 {image1_path} 不存在")
exit(1)
if not os.path.exists(image2_path):
logging.error(f"图片 {image2_path} 不存在")
exit(1)
# 对比两张截图的相似度
if compare_images_feature_matching(image1_path, image2_path):
logging.info("图片相同")
else:
logging.error("图片不同")
def capture_frame_from_rtsp(rtsp_url, output_path):
"""
从RTSP流中捕获一帧并保存为图像文件。
"""
try:
# 打开RTSP流
cap = cv2.VideoCapture(rtsp_url)
if not cap.isOpened():
logging.error("无法打开RTSP流")
return False
# 读取一帧
ret, frame = cap.read()
if not ret:
logging.error("无法从RTSP流中读取帧")
cap.release()
return False
# 保存帧为图像文件
cv2.imwrite(output_path, frame)
logging.info(f"帧已保存到 {output_path}")
# 释放资源
cap.release()
return True
except Exception as e:
logging.error(f"捕获帧时发生错误: {e}", exc_info=True)
return False
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logging.info("开始捕获RTSP流中的帧")
rtsp_url = "rtsp://admin:huawei@123@192.168.4.15/LiveMedia/ch1/Media2" # 替换为你的RTSP流地址
output_path = "captured_frame.jpg" # 保存帧的路径
if capture_frame_from_rtsp(rtsp_url, output_path):
logging.info("帧捕获成功")
else:
logging.error("帧捕获失败")
\ No newline at end of file
......@@ -143,4 +143,6 @@
29. 2024-12-27
- 补充展厅中控屏的灯光控制以及窗帘控制。
30. 2024-12-30
- 封装亮度判断的compare_brightness函数处理,后续通过亮度判断灯光是否正确打开。
\ No newline at end of file
- 封装亮度判断的compare_brightness函数处理,后续通过亮度判断灯光是否正确打开。
31. 2024-12-31
- 补充对于rtsp流抓取一帧保存为图片,然后再进行对应的亮度判断。
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论