[aws ec2]Apache2を入れるが、Internal Server Error

### apache2 インストール
$ sudo apt update
$ sudo apt install apache2
$ sudo ufw app list
$ sudo ufw allow ‘Apache Full’
$ sudo ufw status
$ sudo systemctl status apache2
$ hostname -I

### mod_wsgi
$ apt-get install apache2-dev
$ pip3 install mod_wsgi

### settings.py

ALLOWED_HOSTS = ['*'] 

$ mod_wsgi-express module-config
LoadModule wsgi_module “/home/ubuntu/.local/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so”
WSGIPythonHome “/usr”

### migrate
$ python3 manage.py makemigrations sales
$ python3 manage.py migrate

### apache設定
sudo vi /etc/apache2/sites-available/000-default.conf

LoadModule wsgi_module "/usr/lib/apache2/modules/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so"
WSGIScriptAlias / /var/www/hanbai/hanbai/wsgi.py
WSGIPythonHome "/usr"
WSGIPythonPath "/var/www/hanbai"

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        # DocumentRoot /var/www/hanbai

        # WSGIScriptAlias / /var/www/hanbai/hanbai/wsgi.py
        # WSGIPythonPath /var/www/hanbai/        
        <Directory /var/www/hanbai/hanbai/>
            <Files wsgi.py>
                Order deny,allow
                AllowOverride None
                require all granted
            </Files>
        </Directory>
</VirtualHost>

$ sudo /etc/init.d/apache2 restart

$ /var/log/apache2/error.log
[client 59.126.236.35:46554] from django.core.wsgi import get_wsgi_application
[Wed Oct 28 23:46:17.546723 2020] [wsgi:error] [pid 10444:tid 139970643724032] [client 59.126.236.35:46554] ModuleNotFoundError: No module named ‘django’

$ pip3 freeze | grep wsgi
mod-wsgi==4.7.1

WSGIPythonPath /var/www/hanbai:/home/ubuntu/.local/lib/python3.8/site-packages

何故だ。。Djangoをインストールした場所が悪かった?
もう一回やるか。。

[aws ec2]ubuntuにsshする際にPermission denied (publickey).

$ ssh -i ~/.ssh/*.pem ec2-user@${public ip} -vvv
ec2-user@${public ip}: Permission denied (publickey).

セキュリティグループのinbound rulesでsshを許可しているのに何故だろうと調べていたところ、sshでログインする際のubuntuのuser nameはec2-userではなく、ubuntuらしい。

[公式]Linux インスタンス用ユーザーガイド

$ ssh ubuntu@${public ip} -i ~/.ssh/*.pem
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-1024-aws x86_64)

なるほど、わかりませんね。

[Django3.0] MySQL8にrootでmigrationしようとしてAccess deniedとなる時

bionicからfocal(Ubuntu20.04)で作り直している手順で、mysql8にmigrationしようとすると、Access denied for user ‘root’@’localhost'”となった。

### focalにMySQL 8.20 インストール
$ sudo apt install -y mysql-server
$ mysqld –version
$ sudo service mysql start
$ sudo mysql_secure_installation
$ sudo mysql -u root -p

### Django
$ pip3 install Django==3.0.4

### library インストール
// 省略

### git clone
$ git clone https://github.com/*/${appName}.git
$ cd ${appName}
$ git branch dev
$ git checkout dev

### init.py追記

import pymysql
pymysql.install_as_MySQLdb()

$ pip3 install PyMySQL

### migration
$ python3 manage.py makemigrations sales

->django.db.utils.OperationalError: (1698, “Access denied for user ‘root’@’localhost'”)

なんですと???
MySQL8はrootユーザで外部からアクセスできないらしい。
という事で、Django用のユーザを作ってあげる必要がある。

$ sudo mysql –u root -p
mysql> SHOW VARIABLES LIKE ‘validate_password%’;
mysql> set global validate_password.length=6;
mysql> set global validate_password.policy=LOW;
mysql> CREATE USER ‘django’@’%’ IDENTIFIED BY ‘hogehoge’;
mysql> GRANT ALL PRIVILEGES ON . TO ‘django’@’%’ WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

settings.pyを修正して再度migration
$ python3 manage.py makemigrations sales
$ python3 manage.py migrate
$ python3 manage.py createsuperuser
$ python3 manage.py runserver 192.168.33.10:8000
動作確認

ヒョーーーーー、とりあえず20.04、centos8を普段使いの環境にしたから一安心^^

[Ubuntu20.04] Focal Fossaを始めよう

awsでもUbuntu20.04がbionicよりも上に来てるので、これからはbionicではなく、focalを使っていきたいと思う

$ mkdir focal
$ cd focal
$ vagrant init ubuntu/focal64

vagrantfile35行目のポートフォワーディングコメントアウト削除

config.vm.network "private_network", ip: "192.168.33.10"

if Vagrant.has_plugin?("vagrant-vbguest")
   config.vbguest.auto_update = false  
end

$ vagrant up
$ vagrant ssh
$ python3 -V
Python 3.8.5

$ sudo apt update
$ sudo apt upgrade
$ sudo apt install python3-pip
$ pip3 -V
$ git version

うーん、なんか良さそう

[Django3.0] tesseractで画像の文字を解析するページを実装したい

1) コーディングする前に、まず画面設計から作ります。
– イメージとしてはポップアップ

2) 続いて、Webpack&SASS環境でドラッグ&ドロップのFrontEndを書いていく
– 画像ファイルの制御は file.type.match(‘image.*’)とする。
– 例えばjpegなら、file typeは’image/jpeg’となる。

var imageType = 'image.*';

				if(! file.type.match(imageType)){
					alert('画像を選択してください');
					$('#input_file').val('');
					$('#drop_area').css('border', '1px dashed #aaa');
					return;
				}


良い感じ。これをDjangoに実装する

3) Django
### template
– aタグのリンクをwindow.openで設定する

<a href="javascript:window.open('/read', null, 'top=0,left=0,width=500px,height=650');">画像から商品名を読み取る</a>

– radioのvalueはlangの値を入れます。

<div class="row col-md-12 mt-10">
	  			<div class="form-check col-md-2">
				  <input class="form-check-input" type="radio" name="lang" value="jpn" id="lang" checked>
				  <label class="form-check-label" for="lang">日本語</label>
				</div>
				<div class="form-check col-md-2">
				  <input class="form-check-input" type="radio" name="lang" value="eng" id="lang">
				  <label class="form-check-label" for="lang">英語</label>
				</div>
				<div class="form-check col-md-2">
				  <input class="form-check-input" type="radio" name="lang" value="chi_sim" id="lang">
				  <label class="form-check-label" for="lang">簡体字中国語</label>
				</div>
				<div class="form-check col-md-2">
				  <input class="form-check-input" type="radio" name="lang" value="chi_tra" id="lang">
				  <label class="form-check-label" for="lang">繁体字中国語</label>
				</div>
				<div class="form-check col-md-2">
				  <input class="form-check-input" type="radio" name="lang" value="spa" id="lang">
				  <label class="form-check-label" for="lang">スペイン語</label>
				</div>
			</div>

views.py
– Postされた画像は request.FILES[‘*’]で受け取る。
– 条件分岐で、request.FILES[‘read_img’]とすると、MultiValueDictKeyErrorになるので、request.FILES.get(‘read_img’, False)とする。

from PIL import Image
import sys
import pyocr
def img_read(request):
	if(request.method == 'POST' and request.FILES.get('read_img', False)):
		tools = pyocr.get_available_tools()
		img = request.FILES['read_img']
		langs = request.POST['lang']
		img = Image.open(img)
		txt = tools[0].image_to_string(img,lang=langs,builder=pyocr.builders.TextBuilder(tesseract_layout=6))
		params = {
			'txt': txt,
		}
		return render(request, 'sales/img_read.html', params)
	else:
		return render(request, 'sales/img_read.html')

出来たーーーーーーーーーーーーーー^^
きゃっ♩ きゃっ🎵 きゃっ🎶
早速git pushしよー

[tesseract4.1.1] Ubuntuにtesseract(Eng、日本語、簡体字中国語、繁体字中国語、スペイン語)を入れよう

1. PPA(パーソナル・パッケージ・アーカイブ)を追加
$ sudo add-apt-repository ppa:alex-p/tesseract-ocr
$ sudo apt-get update

2. Tesseractインストール
$ sudo apt install tesseract-ocr
$ sudo apt install libtesseract-dev

$ tesseract -v
tesseract 4.1.1-rc2-25-g9707
$ tesseract –list-langs
List of available languages (2):
eng
osd

3. 日本語、簡体字中国語、繁体字中国語、スペイン語をインストール
$ sudo apt install tesseract-ocr-jpn tesseract-ocr-chi-sim tesseract-ocr-chi-tra tesseract-ocr-spa
$ tesseract –list-langs
List of available languages (6):
chi_sim
chi_tra
eng
jpn
osd
spa

4. pyocr install
$ pip install pyocr

5. 中国語の画像を読み取る

$ tesseract chinabank.jpeg output -l chi_sim
中国平安
PINGAN

保险’ 银行. 投资

6. pythonで書く

from PIL import Image
import sys
import pyocr

tools = pyocr.get_available_tools()
langs = "chi_sim"

img = Image.open('images.jpeg')
txt = tools[0].image_to_string(
		img,
		lang=langs,
		builder=pyocr.builders.TextBuilder(tesseract_layout=6)
	)
print(txt)

$ python app.py
中国平安
PINGAN
保险; 银行.投资

さて、これをDjangoで実装したい。
思ったより考えるの難しいな。

[RaspberryPi 4] cronでカメラモジュールの画像をS3にアップロード

1. shellでカメラモジュールを実行する。
app.sh

#!/bin/bash
raspistill -w 800 -h 600 -o /home/pi/image.jpg

$ chmod +x app.sh
// テスト
$ sudo sh ./app.sh

2. raspi4のcronを有効にする
$ sudo vim /etc/rsyslog.conf
-> コメントアウト

cron.*                          /var/log/cron.log

$ sudo /etc/init.d/rsyslog restart
$ sudo /etc/init.d/cron status

3. カメラモジュールを起動するシェルを実行するcronを書く
テストの為、1分おきの実行にする。
$ crontab -e

*/1 * * * * sudo sh /home/pi/app.sh

$ /etc/init.d/cron restart
$ sudo journalctl -f -u cron
// 写真がとれてることを確認

4. raspi4にboto3をインストール
$ python3 -V
$ pip3 -V
$ pip3 install boto3

5. S3にアップロードするpythonスクリプトを書く
// 省略

6. S3にアップロードするcronを追加してテスト
$ which python3

*/1 * * * *  /usr/bin/python3 /home/pi/upload.py

$ /etc/init.d/cron restart
$ sudo journalctl -f -u cron

7. S3のバケットでraspi4で撮った写真がアップロードされているか確認

OK。数日かかると思ってたけど意外と早くできた。
ドキュメント修正して、次はDjangoにtesseract4->cv2の実装やな

ubuntu bionicでpython3のcronを設定する

まず実行したいpyファイルを用意します。
/home/vagrant/local/cron/app.py

$ crontab -l
no crontab for vagrant
$ crontab -e

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]: 2

$ which python
/usr/bin/python

$ crontab -e

*/5 * * * * /usr/bin/python /home/vagrant/local/cron/app.py

// cronリスタート
$ /etc/init.d/cron restart
// 実行ログの確認
$ sudo journalctl -f -u cron

実行テストは、5分おき(*/5 * * * *)ではなく、1分おきの方が良いですね。
続いて、raspberry pi4でcronを設定します。

[Python 3.8.0] S3に画像をアップロード・ダウンロードする

### S3へupload
1.まず送信したい画像を用意します。SVMで使用したgeorgia.jpgを使いましょう。

2.AWSコンソールにログインし、S3からbucketを作成します。
取り敢えず、hanbai-raspi としておきます。

画像の送受信のみに利用する為、Block all public accessにします。

3.AmazonS3FullAccessのユーザを作成

4.ubuntu側でAWS SDKのboto3をインストール
$ pip -V
pip 20.1.1 from /home/vagrant/.local/lib/python3.8/site-packages/pip (python 3.8)
$ pip install boto3

import boto3

accesskey = "****"
secretkey = "****"
region = "ap-northeast-1"

s3 = boto3.client('s3', aws_access_key_id=accesskey, aws_secret_access_key= secretkey, region_name=region)

filename = "georgia.jpg"
bucket_name = "hanbai-raspi"

s3.upload_file(filename,bucket_name,filename)
print("upload {0}".format(filename))

$ python app.py
upload georgia.jpg

はい、アップロードできてます。

続いて、downloadもやっちゃいましょう。

### download

import boto3

accesskey = "****"
secretkey = "****"
region = "ap-northeast-1"

s3 = boto3.resource('s3', aws_access_key_id=accesskey, aws_secret_access_key= secretkey, region_name=region)

bucket = s3.Bucket("hanbai-raspi")
filename="georgia.jpg"
path="img/georgia.jpg"


bucket.download_file(filename,path)
print("upload {0}".format(filename))

アップロードとダウンロードだと書き方が若干異なるので注意が必要ですね。
さあ、続いて、ubuntuとラズパイ4でcronの実行の仕方。

[RaspberryPi 4] カメラモジュールの起動手順

RaspberryPi 3 model B+ を持っていましたが、カメラモジュールを差し込んだ途端、電源はつくがHDMIでモニターにシグナルが送れなくなってしまったので、RaspberryPi 4を買い直すことにした。

1) 秋葉原の秋月電子通商で「RaspberryPi 4」と「ラズパイ4用の電源アダプター」を購入
2) ラズパイnoobsからsdカード(32GB)にdownload
https://www.raspberrypi.org/downloads/noobs/
3) sdカードをRaspberryPi 4に差し込んで、Rasbianをインストール
4) 「設定」->「Raspberry Piの設定」->「インターフェイス」->「カメラ」->「有効」
5) コマンドライン
$ vcgencmd get_camera
supported=1 detected=1

$ sudo raspistill -o image.jpg
/home/pi/image.jpg

来た!
RaspberryPi3のNo Signalの原因が不明で、もしからたモニターに原因があるのでは?と思って、実はモニター(6000円位)も買い直してしまったorz… モニターは必要なかった。。。。
何れにしても、カメラモジュールはクリア!
さー次はcron x python作るぞー