Author: elin
-
A very basic camera test code on OpenCV
import numpy as np import cv2 cap = cv2.VideoCapture(0) cap.set(3,640) # set Width cap.set(4,480) # set Height while(True): ret, frame = cap.read() cv2.imshow('frame', frame) k = cv2.waitKey(30) & 0xff if k == 27: # press 'ESC' to quit break cap.release() cv2.destroyAllWindows()
-
C++中对string进行分割,寻找,长度等处理
在写C++的过程中,有的时候必须要对用户的输入进行一些处理,用户所输入的string, 可能包含了数字,单词。然而电脑只认为输入的是一串字符而已。
所以,在实战过程中,处理用户输入是很重要的。
我们既然要对string做处理,当然是要先包含了这个强大的string模块:#include <string>
using std::string; //告诉电脑我要用string
假设用户输入一个命令+空格后+第一个数字+空格+第二个数字,我们需要两个String形式的变量,一个是源输入,一个是处理后的命令string. 之后我们需要几个double或者float变量给用户所输入的数字。这边还多了一个strnum,是为了处理尚未转换成double的只包含数字的字符串用的。如果内存紧张可以直接不用。
string command, str,strnum;
double argument1, argument2, answer;
getline(cin, str);如果要处理源输入str,必须使用getline(), 使用cin在后续就不好操作了。
int index = 0;//整数型index,用来找第一个空格
int index2 = 0;//整数型index2,用来找第二个空格接下来就要用到string库中的find功能了。index是找到的空格的排序数字位置。比如说这句话:“hello world”,空格就在第六个,那么str.find会返回6。如果没有空格,就返回-1.
index = str.find(” “,0);
现在又要提到我们的substr功能,这是一个很好的分割字符串的工具。其实在一个字符串里截取字符方法有很多种,substr就是其中的一种而已。它的用法就是截取从第n个字母到第m种字母的字符。在以下代码中, n就是0,代表第一个字母,m就是我们上面获得的index代表空格在第几位, command = str.substr(0, index);下面其实是一样的操作,指定从第一个空格到字符结尾都是我们要的数字。确定字符所有字母数是用length函数,非常简明的,调用它就会回归字符的长度。再就是说这个strnum可能会有两个数字,所以我们再在这个字符串里找一下有没有空格。这个方法只适用于小数量的数字上。
strnum = str.substr(index + 1, str.length());
index2 = strnum.find(” “, 0);后面的话,我们都需要在strnum这个字符串里处理了,简单来说就是从数字的先后位置来指定数字顺序。空格前为第一个数字,空格后是第二个。这里我用了个套娃,先运行分割,再使用stod把字符转换为数字。stod应该来自“string to double”, 我记得还有stoi:”string to int”,之后还有很多,这些转换的功能,还是挺方便的。
argument1 = stod(strnum.substr(0, index2));
argument2=stod(strnum.substr(index2+1,strnum.length())暂时就写这么多,要是有意思以后再补充。
纯粹瞎玩
-
将pip源更换到国内镜像
用pip管理工具安装库文件时,默认使用国外的源文件,因此在国内的下载速度会比较慢,可能只有50KB/s。幸好,国内的一些顶级科研机构已经给我们准备好了各种镜像,下载速度可达2MB/s。
其中,比较常用的国内镜像包括:
(1)阿里云http://mirrors.aliyun.com/pypi/simple/
(2)豆瓣http://pypi.douban.com/simple/
(3)清华大学https://pypi.tuna.tsinghua.edu.cn/simple/
(4)中国科学技术大学http://pypi.mirrors.ustc.edu.cn/simple/
设置方法:(以清华镜像为例,其它镜像同理)
(1)临时使用:
可以在使用pip的时候,加上参数-i和镜像地址(如
https://pypi.tuna.tsinghua.edu.cn/simple/
例如:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simpleopencv-contrib-python
这样就会从清华镜像安装opencv-contrib-python库。
(2)永久修改,一劳永逸:
windows下,直接在user\xxx目录中创建一个pip目录,如:C:\Users\xx\pip,然后新建文件pip.in
在pip.ini文件中输入以下内容
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
[index]
在linux下,
一般要修改配置文件:~/.config/pip/pip.conf (Linux), (没有就创建一个), 将 index-url改成至tuna,例如
在.config文件夹中创建pip/pip.conf在pip.conf文件中添加清华大学的pypi镜像,要是想用其他的镜像,替换成相应的地址即可。
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple -
Ubuntu换国内的软件源
ubuntu系统现在一般都会自带中国的软件仓库,但是如果你的ubuntu系统再国内下载很慢,考虑以下操作。
首先备份源列表文件sources.list:
sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup
再打开现有sources.list
sudo gedit /etc/apt/sources.list
编辑/etc/apt/sources.list文件, 在文件最前面添加阿里云镜像源:
#阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse还有一些其他的国内源:
#中科大源
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse#163源
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse#清华源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse加完以后刷新列表:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential如果还是速度慢,去应用软件里找:软件和更新,仔细在检查一下这些源有没有被确认了。
-
Easy Opencv deployment on raspberry pi
What we need in this deployment is a raspberry pi, a pi camera, if you have a screen for raspberry pi it will be better. We also need an internet connection for modules!
Set up our raspbian in raspberry pi. It is very easy to write an image in your sd card, by using “raspberry pi imager“. This is much more convenient than downloading the image by yourself and write using other software.
if you don’t have a screen or ethernet connection for our raspberry pi, it is necessary to set up a wifi connection before we start up the system. Go to the boot direction in this raspbian SD card, create a file named as “wpa_supplicant.conf“, copy the code below into this .conf file:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid=”WiFi SSID”
psk=”WiFi PASSWORD”
}Replace Wifi SSID and Wifi password in the code, then you are good to go.
Boot up the raspberry pi and connect the pi camera to it, find the IP address of raspberry pi on your computer. Using ssh to connect Raspberry pi. For the first time startup on pi, the username should be pi, and the password should be raspberry.
SSH on windows has the various options, shell and putty are both great software and easy to setup. On Linux is easier, the command will just be:
ssh 192.168.xx.xx@pi
Now we are ready to move on installing OpenCV! Don’t forget to run these two commands every time!
sudo apt-get update
sudo apt-get upgrade
remove the software we don’t need, but it’s optional:
$ sudo apt-get purge wolfram-engine
$ sudo apt-get purge libreoffice*
$ sudo apt-get clean
$ sudo apt-get autoremoveThen we can setup our camera and expand file system on this command: sudo raspi-config Camera option is in 5Interfacing options, expand file system is in 7Advance options.
We can take a reboot right now by :sudo reboot. After rebooting, we can use the command: df -h to check the system size.
Usually, in the raspbian system, the python2 and python3 is preinstalled. But we need the module pip in our system. Command: wget https://bootstrap.pypa.io/get-pip.py
Then open this py file: sudo python3 get-pip.py
We want to use python3 instead of python2. After this command, pip3 should be a command in the shell.
Finally, it is time to install our OpenCV module! There are two ways to install, we will take an easy way by using pip. We can also use source code to build, but it will take over 2 hours to finish. Using pip will be much faster. Commands are below:
pip install opencv-python
pip install opencv-contrib-python
Haha, if there is no error message show, Opencv is now installed in our raspberry pi! But don’t get excited too soon my brother, let’s using python3 to check again.
Type python3 in console, then type “import cv2“, hit enter. If still no error message, type “cv2.__version__“. When you see your OpenCV version, congratulations! You finally installed your OpenCV successfully!
Follow these steps, we can also install OpenCV on our Linux and Windows computers, they are basically the same.
Now enjoy and have some random fun!
-
Ubuntu下搭建STC单片机开发环境
首先安装sdcc,全称Small Device C Compiler,是一个转为8位MCU设计的C编译器。sdcc如同gcc,可以把C源码编译、链接成单片机的可执行文件。安装方法是:
sudo apt install sdcc
当然,也可以从源码编译安装,具体可以查看sdcc官网。使用如下命令编译和链接:sdcc main.c
sdcc也可以像gcc那样使用-c参数指定只编译、不链接等等。命令执行后,会看到目录下产生了一堆main.*文件,其中有一个main.ihx就是我们要的文件。
在安装sdcc时自动安装了packihx这个工具,能把ihx文件转为hex文件:packihx main.ihx > main.hex
接下来就是下载了,需要zhuang一个模组:stcgal
sudo pip3 install stcgal
如果之前没有安装过pip3的话,需要先安装pip3:sudo apt install python3-pip
可以正式开始烧录了。把USB-TTL插入电脑,应该会出现/dev/ttyUSB0(如果有多个,那么找到对应那个)。然后执行命令:sudo stcgal main.hex
此时会出现“Waiting for MCU, please cycle power:”,然后需要重新插拔一下单片机电源,然后就会开始下载:stcgal可以额外配置很多的参数。比如STC12C2052这款单片机,可以选择使用内部RC震荡器还是外部时钟源。那么在烧录时可以带上这些配置,比如:
sudo stcgal -o clock_source=internal main.hex
具体参数可以查看。另外我发现,stcgal是可以直接下载ihx格式的文件的。
-
Win和linux系统中环境变量
最近在用pyinstaller编译python时,无论怎么召唤pyinstaller,命令行都不识别。结果发现是Python的第三方模组没有加进系统的环境变量。这个问题在windows和ubuntu上都发现了。
windows中修改环境变量超容易,控制面板里修改环境变量就行,linux里也只要修改./bashrc文件,在最后一行加上你的要的路径,像是:export PATH=$PATH:/xxx/xxx ///xxx/xxx (等号两边没空格),退出来后source .bashrc生效然后echo $PATH查看一下以防外一。
但是这个python第三方模组的路径我找了好久,现在终于想到有一个简单方法:在python里import numpy,在输入numpy.__file__就会自动输出Numpy所在的路径了,linux和win通用。当然如果没有装numpy就看看其他有没有可以import的模组,原理是一样的。
在安装ros2时也发现命令行写ros2没反应。同样也是./bashrc改一下。遇到这个问题的时候我一下子就反应过来了。
我以前用的是zsh,据说也有zshrc,但是太烦了,zsh感觉没啥用,删了。
-
这是一个Elin的网站!
Elin的网站现在已经上线了!我是管理员Elin!
在这个网站,你可以随心所欲的讨论:包括但不限于机器人,人工智能,机器视觉,3D的打印等等有趣的东西,
当然,更加欢迎交流有趣的项目和新的想法!