如何实现远程相机拍摄图片自动附加至JupyterLab?
实现远程相机每日新图自动导入JupyterLab的方案
完全可以实现,下面是几种实用的方案,你可以根据相机功能和自身环境选择:
1. Python脚本主动拉取新图片
这是最灵活的方式,不管相机是通过FTP/SMB共享存储,还是提供API接口,都能用Python搞定核心逻辑:
- 编写脚本,定期(比如每天固定时间)连接相机的存储源,获取当前图片列表
- 对比JupyterLab工作目录内已有的图片,仅下载新增文件
- 用
schedule或APScheduler模块实现定时执行,也可以直接借助系统的cron(Linux)或任务计划(Windows)来运行脚本
举个简单的FTP拉取示例:
import os import paramiko from datetime import datetime # 相机FTP配置 FTP_HOST = "相机IP地址" FTP_USER = "FTP用户名" FTP_PASS = "FTP密码" REMOTE_IMG_DIR = "/相机存储图片的目录" LOCAL_IMG_DIR = "/Jupyter工作目录/auto_imgs" # 替换为你的Jupyter实际工作路径 # 创建本地存储目录 os.makedirs(LOCAL_IMG_DIR, exist_ok=True) def fetch_new_images(): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(FTP_HOST, username=FTP_USER, password=FTP_PASS) sftp = ssh.open_sftp() remote_files = sftp.listdir(REMOTE_IMG_DIR) # 筛选当天拍摄的图片(假设文件名含日期,例如20240520_1430.jpg) today = datetime.now().strftime("%Y%m%d") target_files = [f for f in remote_files if today in f] for file in target_files: remote_path = f"{REMOTE_IMG_DIR}/{file}" local_path = f"{LOCAL_IMG_DIR}/{file}" if not os.path.exists(local_path): sftp.get(remote_path, local_path) print(f"已下载新图片: {file}") sftp.close() ssh.close() # 设置每天早上9点自动执行拉取 import schedule import time schedule.every().day.at("09:00").do(fetch_new_images) while True: schedule.run_pending() time.sleep(60)
2. 集成JupyterLab定时任务
如果不想在外部单独运行脚本,可以直接在JupyterLab内完成:
- 安装
jupyter-scheduler扩展,它允许你在Jupyter界面内创建定时任务,直接运行拉取图片的脚本 - 或者编写一个.ipynb格式的处理脚本,通过系统定时任务执行
jupyter nbconvert --execute --to notebook your_fetch_script.ipynb,实现自动运行
3. 相机端主动推送图片
如果你的相机支持主动推送功能(比如FTP上传、Webhook),这是最省心的方式:
- 配置相机,将每日拍摄的新图片自动上传到JupyterLab所在服务器的指定目录(即你的Jupyter工作目录)
- 无需编写拉取脚本,图片会直接出现在Jupyter的工作路径中,打开即可用Python处理
注意事项
- 确保相机与Jupyter服务器之间网络连通,且具备足够权限(比如FTP读写权限、目录读写权限)
- 给图片设置唯一命名规则,避免覆盖旧文件
- 在脚本中添加错误处理逻辑,比如网络中断时自动重试,防止任务意外失败
内容的提问来源于stack exchange,提问作者Galactic Eagle
相关产品推荐
相关产品推荐

