라즈베리파이 Camera 사용
- 카메라 연결 : 케이블의 파랑 부분이 LAN포트 쪽을 향해 연결하면 됨
- 카메라 인터페이스 활성화
- sudo raspi-config –> 5 Interfaceing Options —> P1 Camera —> Enable
- picamera
- picamera 패키지 설치
- sudo apt-get install python-picamera
- picamera 지원 파일 포맷
- 영상 전용 : jpeg, png, gif, bmp, yuv, rgb, rgba, bgr, bgra
- 동영상 : h264, mjpeg, yuv, rgb, rgba, bgr, bgra
- h264가 압축률이 가장 좋다
- picamera 매뉴얼 : https://picamera.readthedocs.org
- picamera 패키지 설치
- 카메라 설정 확인 : vcgencmd get_camera
- supported=1이 나오면 인터페이스 활성화 성공, 0이면 실패
- detected = 1이 나오면 카메라 찾음, 0이면 못찾음
- 카메라 테스트
- raspistill -o test.jpg –> 사진 1장 찍어 test.jpg로 저장
- raspivid -t 10000 –> 비디오를 10초간 찍어서 HDMI 모니터에 출력
- 모니터 연결이 없으면 출력 안됨
- 사진 찍기 : take_picture.py
123456789101112131415161718import picamera, timeCamera = picamera.PiCamera()Camera.resolution = (1920, 1080)#Camera.rotation = 180#Camera.hflip = True#Camera.start_preview()print('Camera Start')try:while True:input();str = time.ctime() + '.jpg'Camera.capture(str)print(str + ' file created')except KeyboardInterrupt:print('Camera Stop')#Camera.stop_preview() - 동영상 찍기 : cam_record.py
1234567891011121314151617import picamera, timeCamera = picamera.PiCamera()Camera.framerate = 30Camera.resolution = (1920, 1080)#Camera.rotation = 180#Camera.hflip = TrueCamera.start_recording('movie.h264', format='h264')print('Camera Recording Start')try:while True:print('frame number : %d' % Camera.frame.index)time.sleep(1)except KeyboardInterrupt:print('Camera Recording Stop')Camera.stop_recording() - 버튼 이용하여 사진 찍기(인터럽트 사용) : btn_take_picture.py
1234567891011121314151617181920212223242526272829303132import picamera, timeimport RPi.GPIO as gpioSW = 16Camera = picamera.PiCamera()Camera.resolution = (1920, 1080)#Camera.rotation = 180#Camera.hflip = True#Camera.start_preview()gpio.setmode(gpio.BCM)gpio.setup(SW, gpio.IN)def push_callback(channel):fname = time.ctime() + '.jpg'fname = fname.replace(' ', '_')print(fname + ' file created')Camera.capture(fname)gpio.add_event_detect(SW, gpio.FALLING, callback=push_callback)print('Camera Start')try:while True:time.sleep(10)except KeyboardInterrupt:print('Camera Stop')#Camera.stop_preview() - 버튼 이용하여 동영상 찍기(인터럽트 사용) : btn_cam_record.py
1234567891011121314151617181920212223242526272829303132333435363738394041import picamera, timeimport RPi.GPIO as gpioSW = 16Camera = picamera.PiCamera()Camera.framerate = 30Camera.resolution = (1920, 1080)#Camera.rotation = 180#Camera.hflip = Truegpio.setmode(gpio.BCM)gpio.setup(SW, gpio.IN)ing = 1fname = '1'def push_callback(channel):global ing, fnameing = (ing + 1) % 2if ing == 0:fname = time.ctime() + '.h264'fname = fname.replace(' ', '_')print('Camera Recording Start')Camera.start_recording(fname, format='h264')else:print(fname + ' file created')print('Camera Recording Stop\n\n')Camera.stop_recording()gpio.add_event_detect(SW, gpio.FALLING, callback=push_callback)try:while True:time.sleep(1)except KeyboardInterrupt:if ing == 0:Camera.stop_recording()