Closed masato1004 closed 1 year ago
OpenCVインストール手順 ※pythonのバージョンに合わせる https://python-academia.com/raspberrypi-opencv/
libcamera
in python codeshuld be imported libcamera
library in python code...?
Picamera2
#!/usr/bin/python3
import time
import cv2
from picamera2 import MappedArray, Picamera2, Preview
# This version creates a lores YUV stream, extracts the Y channel and runs the face
# detector directly on that. We use the supplied OpenGL accelerated preview window
# and delegate the face box drawing to its callback function, thereby running the
# preview at the full rate with face updates as and when they are ready.
face_detector = cv2.CascadeClassifier("/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml")
def draw_faces(request):
with MappedArray(request, "main") as m:
for f in faces:
(x, y, w, h) = [c * n // d for c, n, d in zip(f, (w0, h0) * 2, (w1, h1) * 2)]
cv2.rectangle(m.array, (x, y), (x + w, y + h), (0, 255, 0, 0))
picam2 = Picamera2()
picam2.start_preview(Preview.QTGL)
config = picam2.create_preview_configuration(main={"size": (640, 480)},
lores={"size": (320, 240), "format": "YUV420"})
picam2.configure(config)
(w0, h0) = picam2.stream_configuration("main")["size"]
(w1, h1) = picam2.stream_configuration("lores")["size"]
s1 = picam2.stream_configuration("lores")["stride"]
faces = []
picam2.post_callback = draw_faces
picam2.start()
start_time = time.monotonic()
# Run for 10 seconds so that we can include this example in the test suite.
while time.monotonic() - start_time < 10:
buffer = picam2.capture_buffer("lores")
grey = buffer[:s1 * h1].reshape((h1, s1))
faces = face_detector.detectMultiScale(grey, 1.1, 3)
一旦旧OSを使用しても動作できるよう再開発
旧OSでAFカメラを使用できる? Arducamの高解像度カメラを使う
import cv2
from datetime import datetime
# /dev/video0を指定
DEV_ID = 0
# パラメータ
WIDTH = 640
HEIGHT = 480
def main():
# /dev/video0を指定
cap = cv2.VideoCapture(DEV_ID)
# 解像度の指定
cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
# キャプチャの実施
ret, frame = cap.read()
if ret:
# ファイル名に日付を指定
date = datetime.now().strftime("%Y%m%d_%H%M%S")
path = "./" + date + ".jpg"
cv2.imwrite(path, frame)
# 後片付け
cap.release()
cv2.destroyAllWindows()
return
if __name__ == "__main__":
main()
旧OSでAFカメラを使用できる? Arducamの高解像度カメラを使う
Buster
とBullseye
の違いは致命的?
旧OS(Buster)でのオートフォーカスカメラの使用は一旦諦め,新OS(Bullseye)でのAccess Point 化の実装を目指す
ひとまずターミナルでもPythonコードでも使用できることは確認できていて,cv2の行列の形と一致させられる.(cv2でimshow
のコードが動いている)
4656 x 3496
pycamera2
公式ドキュメント参照)picam2.create_still_configuration(lores={"size": (320, 240)}, display="lores")
libcamera-hello
を使用する時に比べて,Pythonコードで実行すると画角がめちゃ狭くなる.
オートフォーカス機能付きラズパイカメラ
従来使用していたラズパイカメラと同じ方法では使用できないっぽいので,使えるようになるまでの検討段階をまとめていきます.
使用ラズパイカメ
16MP IMX519
現段階での課題点
cameratest.py
を使用しても,ネットに書いてあるコマンドを使っても撮影できない