You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 22.04下无需Cheese,用命令行/Python脚本定时触发USB相机拍照

实现USB摄像头定时拍照方案

第一步:确认外接摄像头的设备节点

先通过命令定位外接摄像头对应的/dev/videoX设备节点:

v4l2-ctl --list-devices

从输出中找到标注为Generalplus Technology Inc. 808 Camera的设备,记录它对应的设备节点(比如/dev/video2,实际以你的输出为准)。

方案一:Shell脚本 + fswebcam

  1. 安装拍照工具fswebcam:
sudo apt update && sudo apt install -y fswebcam
  1. 创建图片保存目录:
mkdir -p ~/usb_camera_photos
  1. 编写定时拍照脚本(命名为camera_capture.sh):
#!/bin/bash
SAVE_DIR=~/usb_camera_photos
# 替换为你的外接摄像头设备节点
CAMERA_DEVICE=/dev/video2

while true; do
    # 生成带纳秒级时间戳的文件名,避免覆盖
    FILE_NAME="${SAVE_DIR}/photo_$(date +%Y%m%d_%H%M%S_%N).jpg"
    # 拍照,可按需调整分辨率(-r 参数)
    fswebcam -d $CAMERA_DEVICE -r 1920x1080 --no-banner $FILE_NAME
    # 等待1.72秒
    sleep 1.72
done
  1. 赋予脚本执行权限并运行:
chmod +x camera_capture.sh
./camera_capture.sh

方案二:Python脚本 + OpenCV

  1. 安装依赖库:
pip install opencv-python
  1. 编写Python拍照脚本(命名为camera_capture.py):
import cv2
import time
import os

# 图片保存目录
save_dir = os.path.expanduser("~/usb_camera_photos")
os.makedirs(save_dir, exist_ok=True)

# 替换为外接摄像头的设备编号(对应/dev/videoX的X值)
camera_idx = 2

# 初始化摄像头
cap = cv2.VideoCapture(camera_idx)
if not cap.isOpened():
    print("无法打开目标摄像头")
    exit()

try:
    while True:
        # 读取摄像头帧
        ret, frame = cap.read()
        if not ret:
            print("无法获取摄像头画面")
            break
        
        # 生成带微秒级时间戳的文件名
        timestamp = time.strftime("%Y%m%d_%H%M%S_%f")
        file_path = os.path.join(save_dir, f"photo_{timestamp}.jpg")
        
        # 保存图片
        cv2.imwrite(file_path, frame)
        
        # 等待1.72秒
        time.sleep(1.72)
finally:
    # 释放摄像头资源
    cap.release()
  1. 运行脚本:
python3 camera_capture.py

内容的提问来源于stack exchange,提问作者Glauco Costa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 11:01:27