春のパン祭り点数集計も仕上げの段階ですが、今回はPCのカメラ画像を取得できるようにしたいと思います。
カメラから画像取得
OpenCVチュートリアルの最初のほうでやっています。
これを試してみます。
https://docs.opencv.org/4.x/dd/d43/tutorial_py_video_display.html
やることは、cv2.VideoCapture()
でVideoCaptureオブジェクトを作り、read()
で画像データを1枚得る、というところです。
import cv2 %matplotlib inline from matplotlib import pyplot as plt cap = cv2.VideoCapture(0) if not cap.isOpened(): print('Cannot open camera') else: print('Camera opened')
Camera opened
ret, frame = cap.read() if not ret: print("Can't receive frame") else: plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) plt.show()
print(frame.shape)
(480, 640, 3)
カメラ画像はあっさり取得できました。 ここでは、今年の春のパン祭りシール台紙を撮影してみました。
解像度はVGAサイズのようです。
思ったより小さい…大丈夫か?
この画像で点数計算をやってみます。
文字のフォントとしては、2021年のものと同じように見えるので、2021年のテンプレートデータを使います。
from harupan_data.harupan import * svm = load_svm('harupan_data/harupan_svm.dat') templates2021 = load_templates('harupan_data/templates2021.json')
import time t0 = time.time() score, result_img = calc_harupan(frame, templates2021, svm) t1 = time.time() print('Score: ', score) print('Elapsed time: ', t1 - t0) plt.figure(figsize=(6.4,4.8), dpi=200) plt.imshow(cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)), plt.xticks([]), plt.yticks([]) plt.show()
Number of candidates: 18 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Score: 19.5 Elapsed time: 5.427248001098633
ちょっとうまくいっていない…
撮影し直してみる。
ret, frame2 = cap.read() if not ret: print("Can't receive frame") else: plt.imshow(cv2.cvtColor(frame2, cv2.COLOR_BGR2RGB)) plt.show()
t0 = time.time() score, result_img = calc_harupan(frame2, templates2021, svm) t1 = time.time() print('Score: ', score) print('Elapsed time: ', t1 - t0) plt.figure(figsize=(6.4,4.8), dpi=200) plt.imshow(cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)), plt.xticks([]), plt.yticks([]) plt.show()
Number of candidates: 33 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 Score: 24.5 Elapsed time: 6.267446279525757
結構アップにしてみましたが、いまいち。
- "2.5"の検出がうまくいっていない
"2"、"."、"5"の間の間隔が小さくて、輪郭検出に失敗していると思われます。 - "0.5"の検出もうまくいっていない
点数文字でない"5"の輪郭データを学習に入れたので、厳しめに見られているのか。 - シール下部の"ヤマザキ"の文字が"1"に認識されてしまっている
一応画像データは保存しておこう。
cv2.imwrite('harupan_220330_1.jpg', frame) cv2.imwrite('harupan_220330_2.jpg', frame2)
True
ここまで
短いですが、今回ここまでにしておきます。