分类 综合 下的文章

下载mac arm版本的jdk,在.zprofile中按如下配置:
export JAVA_HOME=/Users/wlg/Softwares/jdk1.8.0_381.jdk/Contents/Home
export CLASSPATH=.:$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH

1 换源

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

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

uname -a

结果如下:
屏幕截图 2023-08-20 193101.png

如果红圈处显示为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 python3-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

本文首发于我的学习之路,点击访问我的学习之路(liguang.wang)

1 背景

最近使用起了Mac Mini,需要安装Pytorch。网上的教程非常老旧,可读性较差,于是记录下自己的安装过程,分享给大家。

2 详细过程

2.1 安装Miniconda

miniconda比Anaconda更加轻量,使用起来几乎一样,根据网上现有信息,很多都是在推荐安装miniconda,所以我们此处也是安装miniconda。
为了追求速度更快,我们使用清华源。
1、下载miniconda。
本文推荐使用此版本(https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-py39_23.3.1-0-MacOSX-arm64.sh),可以直接点击下载,也可以使用此命令下载:

wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-py39_23.3.1-0-MacOSX-arm64.sh

注意,本文以此推荐版本为准,使用其他版本时本文命令和最终效果可能有出入!

2、安装miniconda。
使用如下命令安装miniconda:

zsh ./Miniconda3-py39_23.3.1-0-MacOSX-arm64.sh

如果没有权限,使用如下命令加权限:

chmod +x  ./Miniconda3-py39_23.3.1-0-MacOSX-arm64.sh

注意:请先进入到下载文件所在的目录再执行本小节。
3、检查安装结果。
首先,关闭终端,然后再重新打开。
如果用户名前出现(base)字样,说明安装成功,请跳过本小节。
如果没有出现,请继续阅读本小节。
使用如下命令:

cd ./miniconda/bin

然后执行以下命令,对miniconda进行初始化:

conda init zsh

然后关闭终端,再重新打开,此时(bash)出现。
如果提示command not found,请继续按下面的命令操作:
在终端输入

vim ~/.zshrc

在里面添加这一句,即将conda所在路径添加到环境变量中

export PATH=/Users/qianying/miniconda3/bin:$PATH

保存后,退出,在终端输入:

source ~/.zshrc

完成后,关闭终端重新打开,输入:

conda init zsh

完成初始化。
注意:以上命令请根据自己的目录情况进行相应调整。本小节仅适用于shell为zsh的情况(如果未做调整,M2电脑默认shell均为zsh),如果默认shell不是zsh,请根据相应shell调整第二条命令!
4、关闭自动激活base。
输入以下命令关闭自动激活base:

conda config --set auto_activate_base false

至此,miniconda安装完成。

2.2 安装Pytorch

1、为保证网速,先修改为清华源。

cd ~
vim ./.condarc

然后输入i,写入如下内容:

channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  deepmodeling: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/

然后按下ESC按键,输入:wq保存。
最后在终端中执行以下命令:

conda clean -i

从而清除缓存。
2、建立一个新的conda环境。
使用以下命令:

conda create -n torch python=3.9

3、使用以下命令安装Pytorch:

conda install pytorch::pytorch torchvision torchaudio -c pytorch

4、使用以下命令激活新创建的conda环境:

conda activate pytorch

5、输入python,然后依次输入下面的语句,如果和图片一致,说明安装成功。
截屏2023-07-21 22.00.04.png

3 修订记录

2023.7.21 完成初稿
2023.11.04 针对command not found进行修改。

1、创建分区
使用fdisk 设备文件名创建分区。这里以/dev/sda为例,输入下面的命令:

fdisk /dev/sda

使用输入m查看命令帮助,结果如下图所示:
屏幕截图 2022-10-12 205212.png
其中,输入p查看当前设备上的分区情况,使用n创建新分区。
2、查看当前设备的分区情况。输入p,得到如图所示的结果:
屏幕截图 2022-10-13 092326.png
3、创建一个主分区。
输入n,然后输入p,表示创建一个主分区。然后需要输入分区编号,这里可以直接回车,表示采用默认编号。接下来输入起始的磁柱号,同样可以直接回车,表示紧接着从最近的磁柱号来划分分区。接下来,输入分区空间大小,此时,可以直接输入分区截止的磁柱号,也可以输入分区的大小。由于输入结束的磁柱号不够直观,这里我们直接输入分区大小,这里以分区大小为1G为例,输入+1G。输入完成后,按下回车,此时分区就已经创建好了。最后,输入w进行保存,完成分区创建。相关结果如图所示:
屏幕截图 2022-10-13 093824.png
4、通知内核分区产生变化。输入下面的命令:

partprobe /dev/sda

通知内核分区发生了变化。
5、创建文件系统。使用下面的命令,格式化新创建的分区:

mkfs -t ext3 -b 1024 /dev/sda4

其中,-t表示文件系统的类型,-b表示块大小,最后加上分区的设备文件。
6、挂载。
将新创建的分区挂载到某个目录下面。这里以test666为例。输入下面的命令,将新创建的分区挂载到test666目录下:

mount /dev/sda4 test666

注意,如果挂载点不在当前目录,需要输入目录的绝对路径。
屏幕截图 2022-10-13 093941.png
7、输入df -h命令,查看挂载结果。
屏幕截图 2022-10-13 094017.png

一、下载anaconda

1、直接通过anaconda官网下载会非常慢,很容易超时失败,这里我们使用清华源来下载anaconda。
2、直接访问链接https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/下载自己想要的版本即可。如果执行了这一步请跳过第3步。如果您不知道选哪个版本,请跳过这一步继续看第3步。
3、如果跳过了上面的第2步,可通过访问此链接https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2021.11-Windows-x86_64.exe下载本文推荐的anaconda3-2021.11版本。

二、安装anaconda

1、直接选好自己想要安装到的目录后,其他保持默认即可,非常不建议选中自动添加环境变量选项(即图片中的第一项),而是建议参考后面步骤手动添加环境变量如果您在安装过程中更改了包括但不限于自动添加环境变量选项,本小节不再适用,请从第三小节看起。
屏幕截图 2022-09-04 182150.png
2、手动设置环境变量。
安装如图所示步骤,将anaconda的Scripts目录添加到Path中。
屏幕截图 2020-12-26 152903-sy.png
先选中Path,然后点编辑。
屏幕截图 2020-12-26 152939-sy.png
点击新建,输入自己anaconda的Scripts目录的绝对路径
屏幕截图 2020-12-26 153019-sy.png

三、切换国内源

采用上海交通大学anaconda源。在C盘用户目录下找到.condarc文件,写入以下内容,保存。

default_channels:
  - https://anaconda.mirrors.sjtug.sjtu.edu.cn/pkgs/r
  - https://anaconda.mirrors.sjtug.sjtu.edu.cn/pkgs/main
custom_channels:
  conda-forge: https://anaconda.mirrors.sjtug.sjtu.edu.cn/cloud/
  pytorch: https://anaconda.mirrors.sjtug.sjtu.edu.cn/cloud/
channels:
  - defaults

四、第一次使用conda activate时报错

按照屏幕上输出的提示信息,找到cmd.exe对应的那个执行即可。

支持作者和疑问解答

本文首发于我的学习之路(https://liguang.wang),欢迎访问!
写文章不易,如果这篇文章有帮到你,希望你能给予一定赞赏。您可以通过当前网站或App的赞赏渠道支持,或者点击此处支持作者
如果对于本文内容有疑问,可以点击此处联系作者