0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ラズパイZero2Wで監視カメラ

Posted at

ラズパイZero2Wで監視カメラ

ラズベリーパイZero2Wはラズベリーパイシリーズでも小型で消費電力が比較的小さいがCPU性能は良い。
これを使ってなるべくプログラムを書かずにデータを共有するためにsambaを使ってデータフォルダを共有しました。

下準備

1.ラズパイZero2Wはメモリの搭載量が少ないなので節約のためにWindowsシステムなしで起動します。
2.さらにメモリを補強するためにスワップファイルの大きさを100Mがら2048Mに変更します。
3.カメラの制御用にpython3-opencvをaptでインストールしておきます。
4.sambaをaptでインストールします。

OpenCVでデータを記録するプログラム

このプログラムは一定時間で適当なファイル名を付けてカメラ画像を記録します。

vdf.py
#!/usr/bin/python3

import cv2
import time as tm

cdev = 0
cap = cv2.VideoCapture(cdev)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
sut = 0
fmax = 120
ic = 0
if cap.isOpened():
    try:
        while True:
            ret,rimg = cap.read()
            if ret:
                # ut = int(tm.time())
                ut = int(tm.time()) // 20
                if sut==0:
                    sut = ut
                if sut < ut:
                    ic += 1
                    ic %= fmax
                    fname = f"data/img{ic:04}.jpg"
                    cv2.imwrite(fname,rimg)
                    print(fname)
                sut = ut
            tm.sleep(1)
    except KeyboardInterrupt:
        print("Ctrl+C によりプログラムが中断されました。")

else:
    print("Cam Open Error!!")

fmax = 120
で120個のファイルを作るっことを定義しており
ut = int(tm.time()) // 20
の20が20秒間隔でファイルをセーブします。
この部分を調整して、プログラムを起動するとファイルが指定個数分だけできて後は上書きしていきます。

sambaの設定

sambaの設定は
[homes]
comment = Home Directories
browseable = no

browseable = yes
に変更します。

samba-passwd -a user
ただしuserはラズパイでPythonプログラム実行する人のアカウントID
でパスワードを設定します。
以上でリードオンリーでファイルを読めます。

カメラの話

このシステムでは、カメラは市販のUSBカメラを使っています。
カメラによっては、メモリー不足で制御できないものがありますので要注意!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?