1장. V2 카메라 연결 및 설치하기
카메라 연결 방법
<https://projects.raspberrypi.org/en/projects/getting-started-with-picamera/2>
카메라 작동 실행
libcamera-hello -t 0
바로 실행 사진찍기..
libcamera-jpeg -o joye.jpg
파이어 베이스 관리자
pip3 install firebase_admin
pip3 install uuid
폴더생성
mkdir img
카메라 V2 PDF 설명
<https://datasheets.raspberrypi.com/camera/picamera2-manual.pdf>
sudo apt install -y python3-libcamera python3-kms++
sudo apt install -y python3-prctl libatlas-base-dev ffmpeg python3-pip
sudo apt install -y python3-pyqt5 python3-opengl # only if you want GUI features
pip3 install numpy --upgrade
pip3 install PiCamera2
카메라 설정은 더이상 필요없다.
<https://www.raspberrypi.com/news/bullseye-camera-system/>
<https://kdjun97.github.io/iot/raspberry-pi-install/#google_vignette>
pip3 install 실행 안됨
sudo rm /usr/lib/python3.11/EXTERNALLY-MANAGED
파이어 베이스 비공개 키 이름 변경
serviceAccountKey.json
2장. 사진찍기 코드
import time
from picamera2 import Picamera2
picam2 = Picamera2()
preview_config = picam2.create_preview_configuration()
capture_config = picam2.create_still_configuration()
picam2.configure(preview_config)
picam2.start()
time.sleep(2)
picam2.switch_mode_and_capture_file(capture_config, "/home/내디렉토리명/img/test.jpg")
3장. 사진 찍어 파이어 베이스 스토리지로 전송하기 코드
import time
from picamera2 import Picamera2
import datetime
import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage
from uuid import uuid4
PROJECT_ID = "파이어베이스 스토리지 URL 프로젝트명"
cred = credentials.Certificate("./serviceAccountKey.json")
default_app = firebase_admin.initialize_app(cred, {
'storageBucket': f"{PROJECT_ID}.appspot.com"
})
bucket = storage.bucket()
def fileUpload(file):
blob = bucket.blob('images/'+file)
new_token = uuid4()
metadata = {"firebaseStorageDownloadTokens": new_token}
blob.metadata = metadata
blob.upload_from_filename(filename='./img/'+file, content_type='image/jpeg')
print(blob.public_url)
def execute_camera():
subtitle = "Ras"
suffix = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + '.jpg'
filename = "_".join([subtitle, suffix])
picam2 = Picamera2()
preview_config = picam2.create_preview_configuration()
capture_config = picam2.create_still_configuration()
picam2.configure(preview_config)
picam2.start()
time.sleep(2)
picam2.switch_mode_and_capture_file(capture_config, '/home/내디렉토리명/img/' + filename)
fileUpload(filename)
execute_camera()
4장. 인체 감지 센서로 감지 시 사진 찍어 전송하기 코드
import time
from picamera2 import Picamera2
from time import sleep
import datetime
import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage
from uuid import uuid4
import sys, os
import requests
import RPi.GPIO as GPIO
PROJECT_ID = "파이어베이스 스토리지 URL 프로젝트명"
cred = credentials.Certificate("./serviceAccountKey.json")
default_app = firebase_admin.initialize_app(cred, {
'storageBucket': f"{PROJECT_ID}.appspot.com"
})
bucket = storage.bucket()
picam2 = Picamera2()
preview_config = picam2.create_preview_configuration()
capture_config = picam2.create_still_configuration()
picam2.configure(preview_config)
def fileUpload(file):
blob = bucket.blob('images/'+file)
new_token = uuid4()
metadata = {"firebaseStorageDownloadTokens": new_token}
blob.metadata = metadata
blob.upload_from_filename(filename='./img/'+file, content_type='image/jpeg')
print(blob.public_url)
def execute_camera():
subtitle = "Ras"
suffix = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + '.jpg'
filename = "_".join([subtitle, suffix])
picam2.start()
time.sleep(2)
picam2.switch_mode_and_capture_file(capture_config, '/home/내디렉토리명/img/' + filename)
fileUpload(filename)
pin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.IN)
time.sleep(2)
print('Ready')
chk = 0
try:
while True:
if(GPIO.input(pin)==1):
print('motion')
chk += 1
if chk >= 2:
execute_camera()
chk = 0
else:
print('noting')
time.sleep(1)
except KeyboardInterrupt:
pass
print('Exit with ^C. Goodbye!')
GPIO.cleanup()
exit()