[OpenCV4.5.0] 画像をランダムフォレストで学習して判定したい1 ~写真の用意

まず画像の用意から。
缶コーヒーで(1)FIRE微糖、(2)Georgia blend、(3)BLACK無糖を用意する。

続いて、各缶コーヒーの写真を取る。取り敢えず、10枚ずつ位。
(1)FIRE微糖

(2)Georgia blend

(3)BLACK無糖

どの写真がどの缶コーヒーかラベリングをしたいので、画像の名前を変更する。
fireは fire_${i}.jpg、 Georgia blendは georgia_${i}.jpg、BLACK無糖は black_${i}.jpgにする。

centos8
imgフォルダの中にblack, fire, georgiaフォルダを作成し、その中にそれぞれの画像を入れる。

続いて、これらの画像を学習していきます。

[AWS] IAMでユーザのEC2操作権限を制御したい

まずユーザ作成

続いて、set permissionsでcreate policy
sidはユニークであればなんでも良い。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "ec2:Describe*",
      ],
      "Sid": "Stmt1373379895000",
      "Resource": "*"
      "Effect": "Allow"
    },
    {
      "Action": [
        "ec2:StartInstances",
        "ec2:RebootInstances",
        "ec2:StopInstances",
      ],
      "Sid": "Stmt1373378552000",
      "Resource": [
        "arn:aws:ec2:ap-northeast-1:${account_id}:instance/${instance_id}"
      ],
      "Effect": "Allow"
    }
  ]
}

ec2:Describe*は、リソースレベルで管理できない。
よって、リージョンを変えるか、AWSアカウントを変えるしかない。

どういう風に制御しているのか理解できれば、間接的にセキュリティ方針を俯瞰して観れるようになりますね。

[AWS] Route53でSTG用サブドメインの設定をしたい

### 前提
– 既にお名前.comで取得したドメインをRoute53で割り当てている
– ACMで証明書を発行し、HTTP/HTTPSのinbound rule設定済み

### やりたいこと
– STG用に、商用ドメインの前に「stg.${domain}」としてサブドメインを使いたい
eg. http://stg.hoge.com

色々試した結果、上手くいく方法がわかった。

### 手順
1. EIPの作成
EC2 -> Elastic IPs -> Allocate Elastic IP addressで、EIPを作成し、Associate Elastic IP addressで、STGのインスタンスに紐づける。

2. Route53でサブドメイン用のHosted Zoneの作成
Route53 -> Hosted Zone -> Create hosted zone で Domain nameに stg.${domain}で hosted zoneを新規に作成する。
すると、NSレコード4つとSOAが作られる。※AレコードのEIPは後で追加したもの。

3. NSレコード4つをお名前.comの対象のドメインのDNS設定で追加する。ホスト名は「stg」で、タイプは「NS」、Valueは上記で作られた値
※お名前.com側はこれで終わり

4. サブドメインのHosted ZoneにEIPを追加
create recordでrecord nameはnull、ValueにEIP、Record TypeはAを選択して、EIPを設定

5. 上記で作られた4のNSレコードを、商用ドメイン側のHosted zoneにも追加する。

6. ブラウザから stg.${domain} にアクセスして疎通確認

さードキュメント作るぞーーーーーーーーーーーーーーーー

[AWS] bastionサーバからprivate route tableへのSSHログインの方法 [ドハマり]

まず、public route table (public subnet割り当て)と private route table(private subnet割り当て) を作ります。

public route table
– internet gatewayを割り当てています。

private route table
– 後でnat gatewayを割り当て

private subnetに配置しているEC2のセキュリティグループのインバウンドルール
-> sshのルールで、bastion serverの ${private IPv4 ip}/32 を設定する

### ローカルからbastionにssh
$ ssh ec2-user@${basionserverのpublic ip} -i ~/.ssh/*.pem

そして、bastionサーバにログインし、/.sshに 秘密鍵(*.pem)を配置する

### bastionからprivate subnetのec2にログイン
$ ssh -i .ssh/*.pem ec2-user@${bastionserverのprivate ip}

すごいハマった。3時間ぐらい。
ローカルからはpublic ipでsshログインするけど、bationからはprivate IPv4 ipでログインする
これ違い理解してないと、なんでログインできないか永遠にわからなかったわ

[OpenCV4.5.0] 画像の差分を抽出

2つ画像を用意します。
※プロントで作業しています。ワイヤレスマウスUBS

import cv2
import numpy as np

img_last = cv2.imread(“a.jpg”)
img_last = cv2.cvtColor(img_last, cv2.COLOR_BGR2GRAY) # 白黒化
img_last = cv2.GaussianBlur(img_last, (9, 9), 0) # ぼかし
img_last = cv2.threshold(img_last, 100, 255, cv2.THRESH_BINARY)[1] # 画像の二値化

img = cv2.imread(“b.jpg”)
img_current = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_current = cv2.GaussianBlur(img_current, (9, 9), 0)
img_current = cv2.threshold(img_current, 100, 255, cv2.THRESH_BINARY)[1]

img_diff = cv2.absdiff(img_last, img_current)
cnts = cv2.findContours(img_diff, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]

for pt in cnts:
x, y, w, h = cv2.boundingRect(pt)
if w < 30: continue cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.imwrite("result.png", img) [/python]

OK, Let’s Gooooooooooooooooooooo

[OpenCV4.5.0]NumPyを使って赤色のみ抽出

import cv2
import numpy as np

img = cv2.imread("rose.jpg")

img[:, :, 0] = 0
img[:, :, 1] = 0

cv2.imwrite("result.png", img)

before

after

### HSVで赤っぽい部分の輪郭を表示

import cv2
import numpy as np

img = cv2.imread("rose.jpg")

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV_FULL)
h = hsv[:, :, 0]
s = hsv[:, :, 1]
v = hsv[:, :, 2]

img2 = np.zeros(h.shape, dtype=np.uint8)
img2[(h > 200)  & (s > 100)] = 255

cv2.imwrite("result.png", img2)

ほう

OpenCV+vagrantでmac内蔵カメラが効かない?

ブラウザ上でカメラを起動するのであれば、httpsサーバに変更してChromeでカメラを許可にすれば良いんだが… openCVでカメラ画像の読み込もうとしてエラーになった。。。

import cv2
import numpy as np

cap = cv2.VideoCapture(-1)
while True:

	_, frame = cap.read()
	frame = cv2.resize(frame, (500, 300))

	cv2.imshow('OpenCV web camera', frame)

	k = cv2.waitKey(1)
	if k == 27 or k == 13: break

cap.release()
cv2.destroyAllWindows()

$ python3 app.py
[ WARN:0] global /root/opencv_build/opencv/modules/videoio/src/cap_v4l.cpp (880) open VIDEOIO(V4L2): can’t find camera device
Traceback (most recent call last):
File “app.py”, line 8, in
frame = cv2.resize(frame, (500, 300))
cv2.error: OpenCV(4.5.0-dev) /root/opencv_build/opencv/modules/imgproc/src/resize.cpp:4051: error: (-215:Assertion failed) !ssize.empty() in function ‘resize’

No /dev/video in WSL (1 or 2).が原因っぽいが。。
Videoは今すぐは使わないが、、、
とりあえず動画ファイルの読み込みVideoCapture(‘*.mp4’)で再度やってみよう

cap = cv2.VideoCapture("fuji.mp4")
print(type(cap))
print(cap.isOpened())

$ python3 app.py

False

isOpened()でfalseなので、読み込めてない。

build infoを見る
Video I/O:
DC1394: NO
FFMPEG: NO
avcodec: NO
avformat: NO
avutil: NO
swscale: NO
avresample: NO
GStreamer: NO
v4l/v4l2: YES (linux/videodev2.h)

Video I/Oに問題があるみたい。
なるほど、buildか。。

[OpenCV4.5.0] RPAっぽい葉書から郵便番号の抽出2

元画像

scikit-learnの Handwritten Digits Data SetのSVMの学習済データから予想する。

import cv2
import matplotlib.pyplot as plt

def detect_zipno(fname):
	img = cv2.imread(fname)
	h, w = img.shape[:2]
	img = img[0:h//6, w//3:]

	gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
	gray = cv2.GaussianBlur(gray, (1, 1), 0)
	im2 = cv2.threshold(gray, 140, 255, cv2.THRESH_BINARY_INV)[1]

	cnts = cv2.findContours(im2, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]

	result = []
	for pt in cnts:
		x, y, w, h = cv2.boundingRect(pt)
		if not(50 < w < 70): continue
		result.append([x, y, w, h])
	result = sorted(result, key=lambda x: x[0])

	result2 = []
	lastx = -100
	for x, y, w, h in result:
		if(x - lastx) < 10: continue
		result2.append([x, y, w, h])
		lastx = x
	for x, y, w, h in result2:
		cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 3)
	return result2, img

if __name__ == '__main__':

	cnts, img = detect_zipno("postcard.png")

	cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
	cv2.imwrite("result.png", img)

$ python3 predict_zip.py
[9]
[2]
[5]
[4]
[3]
[4]
[8]

駄目だ、2と4しか合ってない
ただ、やり方の流れはわかった。
データセットの量を多くして、二次元配列を8x8ピクセルではなく、もう少し細かくしたら、結果が変わりそうだ。

[OpenCV4.5.0] RPAっぽい葉書から郵便番号の抽出

まず、葉書の郵便番号部分を抽出する。
元画像

import cv2
import matplotlib.pyplot as plt

def detect_zipno(fname):
	img = cv2.imread(fname)
	h, w = img.shape[:2]
	img = img[0:h//2, w//3:]

	gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
	gray = cv2.GaussianBlur(gray, (3, 3), 0)
	im2 = cv2.threshold(gray, 140, 255, cv2.THRESH_BINARY_INV)[1]

	cnts = cv2.findContours(im2, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]

	result = []
	for pt in cnts:
		x, y, w, h = cv2.boundingRect(pt)
		if not(50 < w < 70): continue
		result.append([x, y, w, h])
	result = sorted(result, key=lambda x: x[0])

	result2 = []
	lastx = -100
	for x, y, w, h in result:
		if(x - lastx) < 10: continue
		result2.append([x, y, w, h])
		lastx = x
	for x, y, w, h in result2:
		cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 3)
	return result2, img

if __name__ == '__main__':

	cnts, img = detect_zipno("postcard.png")

	cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
	cv2.imwrite("result.png", img)

$ python3 app.py

なんでやねん。なんで"2"と"4"が抽出されない。。。
前処理を少し変える。

	img = img[0:h//5, w//3:] # h//3 -> h//5

4が抽出されない。
なぜだ?? ぼかしの周囲のサイズを変えてみる。

	gray = cv2.GaussianBlur(gray, (1, 1), 0)

おおおおおおおおおおおおおおおおお
AI開発で、パラメータを調整するって、こういうこと????
うん、ちょっと興奮しました。
デバッグとはなんか感覚が違いますね。

あれ、というかこれ、記入する領域がわかってたらtesseractでOCRすりゃいいんだから、RPAできんじゃん。。
選挙システムとか注文書とか。。

[OpenCV4.5.0] 輪郭抽出

まず花の写真を用意します。 コスモスかな。

二値化

import cv2
import matplotlib.pyplot as plt

img = cv2.imread("flower.jpg")
img = cv2.resize(img, (300, 169))

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # gray化
gray = cv2.GaussianBlur(gray, (7, 7), 0) # Gaussianぼかし
im2 = cv2.threshold(gray, 140, 240, cv2.THRESH_BINARY_INV)[1] # threshold閾値処理 グレースケール画像を2値画像化

cv2.imwrite("threshold.png", im2)

GaussianBlurは細かな箇所を抽出しないようにぼかす。
findContoursで輪郭を抽出する。

import cv2
import matplotlib.pyplot as plt

img = cv2.imread("flower.jpg")
img = cv2.resize(img, (300, 169))

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # gray化
gray = cv2.GaussianBlur(gray, (7, 7), 0) # Gaussianぼかし
im2 = cv2.threshold(gray, 140, 240, cv2.THRESH_BINARY_INV)[1] # threshold閾値処理 グレースケール画像を2値画像化

# cv2.imwrite("threshold.png", im2)
cnts = cv2.findContours(im2, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]

for pt in cnts:
	x, y, w, h = cv2.boundingRect(pt)

	# if w < 30 or w > 200: continue
	print(x,y,w,h)
	cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)

cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite("find.png", img)

$ python3 app.py
245 91 2 1
182 76 20 18
0 0 300 169

花びらを輪郭にしたかったんですが、雄しべ雌しべが輪郭になってしまいました。
画像を変えてみましょう。

こちらは上手くいきました。
入力値のスクリーニングが鍵やな。