【2023新教程】树莓派定时自动拍照并上传腾讯云对象存储COS

1 换源

仅适用于Release date: May 3rd 2023、Debian version: 11 (bullseye)这个树莓派OS版本,其他版本不保证有效。

首先使用如下命令,查看自己树莓派的架构。

复制代码
uname -a

结果如下:

如果红圈处显示为aarch64,使用命令sudo nano /etc/apt/sources.list,注释掉里面的所有内容,加入以下内容:

复制代码
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free

# deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free

deb https://security.debian.org/debian-security bullseye-security main contrib non-free
# deb-src https://security.debian.org/debian-security bullseye-security main contrib non-free

然后保存。

如果红圈处显示armv7l,则使用命令sudo nano /etc/apt/sources.list,注释掉里面的所有内容,加入以下内容:

复制代码
deb https://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ bullseye main non-free contrib rpi
# deb-src https://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ bullseye main non-free contrib rpi

# deb [arch=arm64] https://mirrors.tuna.tsinghua.edu.cn/raspbian/multiarch/ bullseye main

注意两者不可同时选择。

做完上述步骤后,使用命令sudo nano /etc/apt/sources.list.d/raspi.list ,注释掉里面的所有内容,输入以下内容:

复制代码
deb https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ bullseye main

然后保存。

执行sudo apt-get update.

2 安装OpenCV

使用命令sudo apt-get install python-opencv -y安装OpenCV。

3 编写代码

在/home/pi下创建project文件夹,在该文件夹中再创建文件夹中创建img文件夹、code.py文件。

在code.py文件中写入以下内容:

复制代码
# -*- coding=utf-8
import time
import datetime  #日期时间
import os  #文件操作
import cv2  #opencv-python
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带Appid。Bucket 由 BucketName-Appid 组成
secret_id = '你的ID'     # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
secret_key = '你的key'   # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
region = 'ap-guangzhou'      # 替换为用户的 region,已创建桶归属的 region 可以在控制台查,https://console.cloud.tencent.com/cos5/bucket
                           # COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224
token = None               # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048
scheme = 'https'           # 指定使用 http/https 协议来访问 COS,默认为 https,可不填

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)

camera = cv2.VideoCapture(0)  #一个摄像头,后期可扩展多个摄像头
 
def delete_imgs():
    delete_url = "/home/pi/project/img"
    delete_list = os.listdir(delete_url)
    #print(delete_list)
    for i in range(len(delete_list)):
        os.remove(delete_url+'/'+delete_list[i])
    logging.info("delete all imgs success!")

if camera.isOpened():
    logging.info("Start Picture!")
    while True:
        # 设置分辨率
        camera.set(3, 1920)  #width
        camera.set(4, 1080)  #height
        ret, img = camera.read()
        date = datetime.datetime.now().strftime("%Y-%m-%d")
        get_photo_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
        #print(get_photo_time)
        file_path = "/home/pi/regular-photos-of-raspberry-pie/img/" + str(get_photo_time) + ".jpg"
        cv2.imwrite(file_path,img)  #保存到树莓派本地
        upload_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
        #print(upload_time)
        response = client.upload_file(
            Bucket='raspberry1-1257204660',
            LocalFilePath=file_path,
            Key="getRasberryImgs/"+ date + "/" + str(upload_time)+ ".jpg",
            PartSize=1,
            MAXThread=10,
            EnableMD5=False
        )
        os.remove(file_path)  #删除树莓派本地已保存文件//有时候因为进程先后,有些图片文件来不及删除

        #判断img文件中有没有照片,删除掉
        nums = os.listdir('/home/pi/regular-photos-of-raspberry-pie/img')#删除来不及删除的照片
        if nums:
            delete_imgs()
        # camera.release()
        time.sleep(3600)

执行上述代码,即可完成每小时拍一张照并上传到腾讯云对象存储COS中。

参考资料

https://blog.csdn.net/weixin_46709801/article/details/128045344

相关推荐
@insist12312 小时前
信息安全工程师-云计算安全核心知识框架
安全·云计算·软考·信息安全工程师·软件水平考试
ZStack开发者社区17 小时前
全球化2.0 | ZStack亮相印尼云计算与数据中心大会 以新一代云底座助力数字印尼建设
服务器·云计算·gpu算力
SAP上海工博云署17 小时前
汽配出海业务扩张难题拆解:SAP Business One 适配跨境制造管理
大数据·人工智能·云计算·制造·信息与通信·零售
XINVRY-FPGA19 小时前
XC7Z020-2CLG484I Xilinx Zynq-7000 SoC FPGA
嵌入式硬件·fpga开发·云计算·硬件工程·fpga
智慧医养结合软件开源19 小时前
数智协同,赋能康养服务高效升级
大数据·人工智能·云计算·生活
林林奇遇记20 小时前
阿里云虚拟主机wordpress无法安装插件
阿里云·云计算
少年攻城狮20 小时前
阿里云系列---【申请域名并绑定到主机ip】
linux·服务器·tcp/ip·阿里云·云计算
酷道20 小时前
获取Docker阿里云专属镜像加速地址
阿里云·docker·容器·云计算
互联网老欣20 小时前
2026 最新|OpenClaw(Clawdbot)阿里云轻量服务器一键部署保姆级教程(避坑 + 性能调优)
服务器·阿里云·云计算
Harvy_没救了20 小时前
【云计算】OpenStack 核心组件知识总结(一)
云计算·openstack