Skip to content

Instantly share code, notes, and snippets.

@ysuito
Created March 20, 2024 12:44
Show Gist options
  • Save ysuito/034a45a5e2ceb64e27033f163fa67d46 to your computer and use it in GitHub Desktop.
Save ysuito/034a45a5e2ceb64e27033f163fa67d46 to your computer and use it in GitHub Desktop.
撮影高速化試行
import cv2
import os
import time
import RPi.GPIO as GPIO
import sys
from picamera2 import Picamera2
import libcamera
import numpy as np
timelog = time.time()
# raw_width = 640 #2304
# raw_height = 480 #1296
rec_width = 320
rec_height = 240
time_log = []
time_log2 = []
time_log3 = []
frame_list = []
meta_data_list = []
# exposure_time = 5000 # 1000-100000 defo:5000
exposure_time = 1000 # 物理シャッターとの干渉を徹底的に避ける
analogue_gain = 16 # 1.0-20.0 defo:2.0
buffers = 1
queue_control = False
# Bolex
pin_shutter = 25 # shutter timing picup
# GPIO設定
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_shutter, GPIO.IN, pull_up_down = GPIO.PUD_UP)
number_max_frame = 32 #連続撮影可能な最大フレーム数 とりあえず16FPS x 60sec = 960フレーム
record_fps = 16 #MP4へ変換する際のFPS設定値
share_folder_path = os.path.expanduser("~/share/")
device_name = "bolex"
codec = cv2.VideoWriter_fourcc(*'avc1')
camera = Picamera2()
# 撮影モードに応じてpicameraのconfigを設定する
def set_camera_mode():
global rec_width, rec_height, raw_width, raw_height
camera.stop()
config = camera.create_still_configuration(
main = {
"format" : "BGR888",
"size" : (rec_width, rec_height)
},
# raw = {
# "format" : "SBGGR10_CSI2P",
# "size" : (raw_width, raw_height)
# },
buffer_count = buffers,
queue = queue_control,
controls = {
"ExposureTime" : exposure_time,
"AnalogueGain" : analogue_gain,
# "FrameDurationLimits" : (100, 100000)
"FrameDurationLimits" : (10000, 10000) # 10ms固定にして可変要素を排除する
},
# transform = libcamera.Transform(hflip=1, vflip=1) # 切り分けのため、処理をスキップする
)
camera.configure(config)
camera.start()
# timerを受けて画像を取得する関数
def shutter(text):
global time_log, time_log2, time_log3, frame_list, meta_data_list
if len(frame_list) < number_max_frame:
time_log.append(time.time())
request = camera.capture_request()
time_log2.append(time.time())
frame_list.append(request.make_image("main"))
meta_data_list.append(request.get_metadata())
request.release()
time_log3.append(time.time())
else:
movie_save()
# ムービー撮影後、画像を連結してムービーファイルを保存する。
def movie_save():
global recording_completed, frame_list, meta_data_list
recording_completed = False
print("save movie")
movie_file_path = share_folder_path + device_name + ".mp4"
video = cv2.VideoWriter(movie_file_path, codec, record_fps, (rec_width, rec_height))
if not video.isOpened():
print("video writer can't be opened")
sys.exit()
for i in range(len(frame_list)):
frame = np.array(frame_list[i])
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
video.write(frame)
print("movie rec finished")
video.release()
print_log()
def print_log():
global time_log, time_log2, frame_list, meta_data_list
print(" frame interval / camera interval // capture request / extract data (msec)" )
for i in range(len(time_log)-1):
t1 = (time_log[i + 1] - time_log[i]) * 1000
t2 = (meta_data_list[i + 1]["SensorTimestamp"] - meta_data_list[i]["SensorTimestamp"]) / 1000000
t3 = (time_log2[i] - time_log[i]) * 1000
t4 = (time_log3[i] - time_log2[i]) * 1000
print(f'{t1:16.3f}' + ' / ' + f'{t2:15.3f}' + ' // ' + f'{t3:15.3f}' + ' / ' + f'{t4:12.3f}')
time_log = []
time_log2 = []
frame_list = []
meta_data_list = []
# メイン
if __name__ == "__main__":
set_camera_mode()
# シャッター動作検出時のコールバック関数
GPIO.add_event_detect(pin_shutter, GPIO.RISING, callback = shutter, bouncetime = 1)
while(True):
time.sleep(10)
@ysuito
Copy link
Author

ysuito commented Mar 20, 2024

割り込み無しの検証用コード。

import cv2
import os
import time
import RPi.GPIO as GPIO
import sys
from picamera2 import Picamera2
import libcamera
import numpy as np

timelog = time.time()

# raw_width           = 640           #2304
# raw_height          = 480           #1296
rec_width           = 320
rec_height          = 240
time_log            = []
time_log2           = []
time_log3           = []
frame_list          = []
meta_data_list      = []
# exposure_time       = 5000              # 1000-100000  defo:5000
exposure_time       = 1000 # \u7269\u7406\u30b7\u30e3\u30c3\u30bf\u30fc\u3068\u306e\u5e72\u6e09\u3092\u5fb9\u5e95\u7684\u306b\u907f\u3051\u308b
analogue_gain       = 16	            # 1.0-20.0    defo:2.0

buffers             = 1
queue_control       = False

# Bolex
pin_shutter         = 25    # shutter timing picup 

# GPIO\u8a2d\u5b9a
#GPIO.setmode(GPIO.BCM)
#GPIO.setup(pin_shutter,     GPIO.IN, pull_up_down = GPIO.PUD_UP)

number_max_frame    = 32                 #\u9023\u7d9a\u64ae\u5f71\u53ef\u80fd\u306a\u6700\u5927\u30d5\u30ec\u30fc\u30e0\u6570\u3000\u3068\u308a\u3042\u3048\u305a16FPS x 60sec = 960\u30d5\u30ec\u30fc\u30e0
record_fps          = 16                #MP4\u3078\u5909\u63db\u3059\u308b\u969b\u306eFPS\u8a2d\u5b9a\u5024
#share_folder_path   = os.path.expanduser("~/share/")
device_name         = "bolex"
codec               = cv2.VideoWriter_fourcc(*'avc1')
camera              = Picamera2()

# \u64ae\u5f71\u30e2\u30fc\u30c9\u306b\u5fdc\u3058\u3066picamera\u306econfig\u3092\u8a2d\u5b9a\u3059\u308b
def set_camera_mode():
    global rec_width, rec_height, raw_width, raw_height
    camera.stop()

    config  = camera.create_still_configuration(
        main    = {
            "format"    : "BGR888", 
            "size"      : (rec_width, rec_height)
        }, 
#         raw     = {
#             "format"    : "SBGGR10_CSI2P",
#             "size"      : (raw_width, raw_height)
#         },
        buffer_count    = buffers,
        queue           = queue_control,
        controls        = {
            "ExposureTime"          : exposure_time, 
            "AnalogueGain"          : analogue_gain,
#             "FrameDurationLimits"   : (100, 100000)
            "FrameDurationLimits"   : (10000, 10000) # 10ms\u56fa\u5b9a\u306b\u3057\u3066\u53ef\u5909\u8981\u7d20\u3092\u6392\u9664\u3059\u308b
        },
#         transform       = libcamera.Transform(hflip=1, vflip=1) # \u5207\u308a\u5206\u3051\u306e\u305f\u3081\u3001\u51e6\u7406\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b
    )

    camera.configure(config)
    camera.start()

# timer\u3092\u53d7\u3051\u3066\u753b\u50cf\u3092\u53d6\u5f97\u3059\u308b\u95a2\u6570
def shutter(text):
    global time_log, time_log2, time_log3, frame_list, meta_data_list
    
    if len(frame_list) < number_max_frame:
        time_log.append(time.time())
        request = camera.capture_request()
        time_log2.append(time.time())
        frame_list.append(request.make_image("main"))
        meta_data_list.append(request.get_metadata())
        request.release()
        time_log3.append(time.time())
    else:
        movie_save()

# \u30e0\u30fc\u30d3\u30fc\u64ae\u5f71\u5f8c\u3001\u753b\u50cf\u3092\u9023\u7d50\u3057\u3066\u30e0\u30fc\u30d3\u30fc\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\u3059\u308b\u3002
def movie_save():
    #global recording_completed, frame_list, meta_data_list
    #recording_completed = False
    #print("save movie")
    #movie_file_path = share_folder_path + device_name + ".mp4"

    #video = cv2.VideoWriter(movie_file_path, codec, record_fps, (rec_width, rec_height))

    #if not video.isOpened():
    #    print("video writer can't be opened")
    #    sys.exit()
    #for i in range(len(frame_list)):
    #    frame = np.array(frame_list[i])
    #    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    #    video.write(frame)
    #print("movie rec finished")
    #video.release()
    print_log()

def print_log():
    global time_log, time_log2, frame_list, meta_data_list

    print("  frame interval / camera interval // capture request / extract data  (msec)"  )
    for i in range(len(time_log)-1):
        t1 = (time_log[i + 1] - time_log[i]) * 1000
        t2 = (meta_data_list[i + 1]["SensorTimestamp"] - meta_data_list[i]["SensorTimestamp"]) / 1000000
        t3 = (time_log2[i] - time_log[i]) * 1000
        t4 = (time_log3[i] - time_log2[i]) * 1000
        print(f'{t1:16.3f}' + ' / ' + f'{t2:15.3f}' + ' // ' + f'{t3:15.3f}' + ' / ' + f'{t4:12.3f}')

    
    time_log            = []
    time_log2           = []
    frame_list          = []
    meta_data_list      = []

# \u30e1\u30a4\u30f3
if __name__ == "__main__":

    set_camera_mode()
    
    # \u30b7\u30e3\u30c3\u30bf\u30fc\u52d5\u4f5c\u691c\u51fa\u6642\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570
    #GPIO.add_event_detect(pin_shutter,  GPIO.RISING,    callback = shutter, bouncetime = 1)

    for n in range(100):
        print(n)
        shutter("")

4Bでの実行結果は下記の通り。5ms程度の変動はあるが、比較的安定している。
Screenshot from 2024-03-20 21-56-11

@ysuito
Copy link
Author

ysuito commented Mar 20, 2024

システムパフォーマンスは、下記の通り。低負荷、Zero2でも問題ないと想定。

pi@RKT-0001:~ $ vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 1015416  77176 2623808    0    0     6    20   26   22  3  0 97  0  0
 0  0      0 1014912  77176 2623808    0    0     0    68 1509 1852  3  2 95  0  0
 0  0      0 1014660  77176 2623808    0    0     0     0 1446 1763  4  2 95  0  0
 0  0      0 1014408  77176 2623808    0    0     0     0 1589 1690  4  2 94  0  0
 0  0      0 1013652  77176 2623808    0    0     0     0 1651 1697  5  3 92  0  0
 0  0      0 1013400  77176 2623808    0    0     0     0 1601 1681  4  3 93  0  0

@airpocket-soundman
Copy link

こんなかんじですね。t2安定しています。

 frame interval / camera interval // capture request / extract data  (msec)
          74.599 /          39.929 //          68.925 /        5.552
          35.257 /          39.932 //          29.620 /        5.516
          39.286 /          39.930 //          34.231 /        4.931
          39.943 /          39.931 //          34.962 /        4.858
          39.925 /          39.931 //          34.945 /        4.854
          39.901 /          39.932 //          34.876 /        4.902
          39.721 /          39.931 //          34.727 /        4.868
          39.851 /          39.931 //          34.904 /        4.769
          39.987 /          39.932 //          35.043 /        4.737
          40.007 /          39.931 //          35.035 /        4.779
          40.000 /          39.931 //          34.985 /        4.844
          39.958 /          39.932 //          35.034 /        4.740
          39.763 /          39.930 //          34.829 /        4.811
          39.922 /          39.932 //          34.974 /        4.826
          39.896 /          39.933 //          34.984 /        4.789
          39.916 /          39.930 //          35.005 /        4.789
          39.959 /          39.931 //          35.015 /        4.824
          40.045 /          39.931 //          35.123 /        4.803
          39.853 /          39.929 //          34.881 /        4.851
          39.865 /          39.935 //          34.925 /        4.818
          39.934 /          39.929 //          35.025 /        4.791
          39.895 /          39.931 //          35.010 /        4.762
          40.291 /          39.931 //          35.344 /        4.830
          39.627 /          39.931 //          34.708 /        4.799
          39.956 /          39.931 //          35.019 /        4.821
          39.971 /          39.931 //          34.982 /        4.870
          39.884 /          39.931 //          34.966 /        4.800
          39.885 /          39.931 //          34.980 /        4.785
          41.559 /          39.932 //          35.683 /        5.746
          38.312 /          39.931 //          33.442 /        4.750
          39.906 /          39.930 //          35.061 /        4.721
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  frame interval / camera interval // capture request / extract data  (msec)
          31.764 /          39.929 //          26.574 /    -1267.390
          39.462 /          39.933 //          34.656 /    -1271.980
          40.079 /          39.929 //          35.254 /    -1272.756
          39.894 /          39.932 //          35.070 /    -1272.708
          40.053 /          39.931 //          35.127 /    -1272.737
          39.957 /          39.931 //          34.982 /    -1272.740
          39.778 /          39.931 //          34.961 /    -1272.957
          40.010 /          39.931 //          35.191 /    -1273.165
          39.786 /          39.932 //          34.976 /    -1273.003
          40.312 /          39.931 //          35.477 /    -1273.270
          39.575 /          39.931 //          34.782 /    -1272.866
          39.999 /          39.931 //          35.175 /    -1272.889
          39.999 /          39.931 //          35.186 /    -1273.075
          39.829 /          39.932 //          35.023 /    -1272.988
          39.975 /          39.931 //          35.145 /    -1273.042
          39.927 /          39.931 //          35.114 /    -1273.071
          40.705 /          39.930 //          35.868 /    -1273.791
          39.169 /          39.932 //          34.334 /    -1272.917
          39.957 /          39.931 //          35.091 /    -1272.990
          39.927 /          39.931 //          35.081 /    -1273.073
          39.866 /          39.932 //          35.014 /    -1272.995
          40.230 /          39.930 //          35.364 /    -1273.320
          39.664 /          39.932 //          34.747 /    -1272.636
          39.962 /          39.931 //          35.029 /    -1272.957
          39.937 /          39.932 //          35.023 /    -1272.955
          39.941 /          39.931 //          35.026 /    -1272.926
          39.875 /          39.931 //          34.929 /    -1272.883
          39.943 /          39.930 //          35.033 /    -1272.982
          40.046 /          39.932 //          35.109 /    -1271.452
          40.241 /          39.932 //          35.257 /    -1273.324
          39.784 /          39.931 //          34.836 /    -1273.241
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  frame interval / camera interval // capture request / extract data  (msec)
          32.177 /          39.929 //          26.941 /    -2545.336
          43.001 /          39.935 //          36.058 /    -2551.373
          37.711 /          39.929 //          32.720 /    -2551.753
          38.528 /          39.930 //          33.683 /    -2550.483
          39.969 /          39.932 //          35.099 /    -2550.505
          39.899 /          39.932 //          35.040 /    -2550.512
          40.154 /          39.930 //          35.240 /    -2550.893
          39.738 /          39.931 //          34.866 /    -2550.874
          39.835 /          39.933 //          34.989 /    -2550.777
          39.987 /          39.930 //          35.126 /    -2550.729
          39.968 /          39.931 //          35.093 /    -2550.661
          39.963 /          39.930 //          35.083 /    -2550.673
          40.732 /          39.933 //          35.775 /    -2551.505
          39.197 /          39.929 //          34.314 /    -2550.853
          39.847 /          39.933 //          34.961 /    -2550.802
          39.911 /          39.930 //          35.044 /    -2550.816
          40.072 /          39.933 //          35.189 /    -2550.910
          40.416 /          39.930 //          35.535 /    -2551.282
          39.336 /          39.931 //          34.435 /    -2550.747
          39.932 /          39.931 //          35.027 /    -2550.810
          43.170 /          39.936 //          36.904 /    -2552.681
          40.795 /          39.932 //          34.536 /    -2553.593
          39.230 /          39.930 //          32.804 /    -2552.360
          39.895 /          39.931 //          33.527 /    -2552.688
          39.810 /          39.932 //          33.531 /    -2552.629
          40.276 /          39.931 //          33.834 /    -2552.773
          40.200 /          39.928 //          33.393 /    -2552.722
          39.637 /          39.932 //          33.189 /    -2552.837
          39.863 /          39.933 //          33.434 /    -2551.171
          41.199 /          39.928 //          33.608 /    -2552.885
          39.862 /          39.932 //          32.318 /    -2552.892
99

@ysuito
Copy link
Author

ysuito commented Mar 20, 2024

以下のコードで、事象再現出来た。割り込みコールバックから、libcameraを呼び出すと、queueの挙動がおかしくなる。

import cv2
import os
import time
import RPi.GPIO as GPIO
import sys
from picamera2 import Picamera2
import libcamera
import numpy as np
from threading import Thread
import gpiozero

timelog = time.time()

# raw_width           = 640           #2304
# raw_height          = 480           #1296
rec_width           = 320
rec_height          = 240
time_log            = []
time_log2           = []
time_log3           = []
frame_list          = []
meta_data_list      = []
# exposure_time       = 5000              # 1000-100000  defo:5000
exposure_time       = 1000 # \u7269\u7406\u30b7\u30e3\u30c3\u30bf\u30fc\u3068\u306e\u5e72\u6e09\u3092\u5fb9\u5e95\u7684\u306b\u907f\u3051\u308b
analogue_gain       = 16	            # 1.0-20.0    defo:2.0

buffers             = 1
queue_control       = False

# Bolex
pin_shutter         = 25    # shutter timing picup 
pin_trigger         = 21

trigger = gpiozero.LED(pin_trigger)

# GPIO\u8a2d\u5b9a
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_shutter,     GPIO.IN, pull_up_down = GPIO.PUD_UP)

number_max_frame    = 32                 #\u9023\u7d9a\u64ae\u5f71\u53ef\u80fd\u306a\u6700\u5927\u30d5\u30ec\u30fc\u30e0\u6570\u3000\u3068\u308a\u3042\u3048\u305a16FPS x 60sec = 960\u30d5\u30ec\u30fc\u30e0
record_fps          = 16                #MP4\u3078\u5909\u63db\u3059\u308b\u969b\u306eFPS\u8a2d\u5b9a\u5024
#share_folder_path   = os.path.expanduser("~/share/")
device_name         = "bolex"
codec               = cv2.VideoWriter_fourcc(*'avc1')
camera              = Picamera2()

# \u64ae\u5f71\u30e2\u30fc\u30c9\u306b\u5fdc\u3058\u3066picamera\u306econfig\u3092\u8a2d\u5b9a\u3059\u308b
def set_camera_mode():
    global rec_width, rec_height, raw_width, raw_height
    camera.stop()

    config  = camera.create_still_configuration(
        main    = {
            "format"    : "BGR888", 
            "size"      : (rec_width, rec_height)
        }, 
#         raw     = {
#             "format"    : "SBGGR10_CSI2P",
#             "size"      : (raw_width, raw_height)
#         },
        buffer_count    = buffers,
        queue           = queue_control,
        controls        = {
            "ExposureTime"          : exposure_time, 
            "AnalogueGain"          : analogue_gain,
#             "FrameDurationLimits"   : (100, 100000)
            "FrameDurationLimits"   : (10000, 10000) # 10ms\u56fa\u5b9a\u306b\u3057\u3066\u53ef\u5909\u8981\u7d20\u3092\u6392\u9664\u3059\u308b
        },
#         transform       = libcamera.Transform(hflip=1, vflip=1) # \u5207\u308a\u5206\u3051\u306e\u305f\u3081\u3001\u51e6\u7406\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b
    )

    camera.configure(config)
    camera.start()

# timer\u3092\u53d7\u3051\u3066\u753b\u50cf\u3092\u53d6\u5f97\u3059\u308b\u95a2\u6570
def shutter(text):
    global time_log, time_log2, time_log3, frame_list, meta_data_list
    
    if len(frame_list) < number_max_frame:
        time_log.append(time.time())
        request = camera.capture_request()
        time_log2.append(time.time())
        frame_list.append(request.make_image("main"))
        meta_data_list.append(request.get_metadata())
        request.release()
        time_log3.append(time.time())
    else:
        movie_save()

# \u30e0\u30fc\u30d3\u30fc\u64ae\u5f71\u5f8c\u3001\u753b\u50cf\u3092\u9023\u7d50\u3057\u3066\u30e0\u30fc\u30d3\u30fc\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\u3059\u308b\u3002
def movie_save():
    #global recording_completed, frame_list, meta_data_list
    #recording_completed = False
    #print("save movie")
    #movie_file_path = share_folder_path + device_name + ".mp4"

    #video = cv2.VideoWriter(movie_file_path, codec, record_fps, (rec_width, rec_height))

    #if not video.isOpened():
    #    print("video writer can't be opened")
    #    sys.exit()
    #for i in range(len(frame_list)):
    #    frame = np.array(frame_list[i])
    #    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    #    video.write(frame)
    #print("movie rec finished")
    #video.release()
    print_log()

def print_log():
    global time_log, time_log2, frame_list, meta_data_list

    print("  frame interval / camera interval // capture request / extract data  (msec)"  )
    for i in range(len(time_log)-1):
        t1 = (time_log[i + 1] - time_log[i]) * 1000
        t2 = (meta_data_list[i + 1]["SensorTimestamp"] - meta_data_list[i]["SensorTimestamp"]) / 1000000
        t3 = (time_log2[i] - time_log[i]) * 1000
        t4 = (time_log3[i] - time_log2[i]) * 1000
        print(f'{t1:16.3f}' + ' / ' + f'{t2:15.3f}' + ' // ' + f'{t3:15.3f}' + ' / ' + f'{t4:12.3f}')

    
    time_log            = []
    time_log2           = []
    frame_list          = []
    meta_data_list      = []

# \u30e1\u30a4\u30f3
if __name__ == "__main__":

    set_camera_mode()
    
    # \u30b7\u30e3\u30c3\u30bf\u30fc\u52d5\u4f5c\u691c\u51fa\u6642\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570
    GPIO.add_event_detect(pin_shutter,  GPIO.RISING,    callback = shutter, bouncetime = 1)

    while True:
        trigger.on()
        time.sleep(0.01)
        trigger.off()
        time.sleep(0.05)

Screenshot from 2024-03-21 00-10-17

@ysuito
Copy link
Author

ysuito commented Mar 20, 2024

割り込みコールバックを使わない検証用コード→安定しているように見える。

import cv2
import os
import time
import RPi.GPIO as GPIO
import sys
from picamera2 import Picamera2
import libcamera
import numpy as np
from threading import Thread
import gpiozero

timelog = time.time()

# raw_width           = 640           #2304
# raw_height          = 480           #1296
rec_width           = 320
rec_height          = 240
time_log            = []
time_log2           = []
time_log3           = []
frame_list          = []
meta_data_list      = []
# exposure_time       = 5000              # 1000-100000  defo:5000
exposure_time       = 1000 # \u7269\u7406\u30b7\u30e3\u30c3\u30bf\u30fc\u3068\u306e\u5e72\u6e09\u3092\u5fb9\u5e95\u7684\u306b\u907f\u3051\u308b
analogue_gain       = 16	            # 1.0-20.0    defo:2.0

buffers             = 1
queue_control       = False

# Bolex
pin_shutter         = 25    # shutter timing picup 
pin_trigger         = 21

trigger = gpiozero.LED(pin_trigger)
button = gpiozero.Button(pin_shutter, pull_up=True)

# GPIO\u8a2d\u5b9a
#GPIO.setmode(GPIO.BCM)
#GPIO.setup(pin_shutter,     GPIO.IN, pull_up_down = GPIO.PUD_UP)

number_max_frame    = 32                 #\u9023\u7d9a\u64ae\u5f71\u53ef\u80fd\u306a\u6700\u5927\u30d5\u30ec\u30fc\u30e0\u6570\u3000\u3068\u308a\u3042\u3048\u305a16FPS x 60sec = 960\u30d5\u30ec\u30fc\u30e0
record_fps          = 16                #MP4\u3078\u5909\u63db\u3059\u308b\u969b\u306eFPS\u8a2d\u5b9a\u5024
#share_folder_path   = os.path.expanduser("~/share/")
device_name         = "bolex"
codec               = cv2.VideoWriter_fourcc(*'avc1')
camera              = Picamera2()

# \u64ae\u5f71\u30e2\u30fc\u30c9\u306b\u5fdc\u3058\u3066picamera\u306econfig\u3092\u8a2d\u5b9a\u3059\u308b
def set_camera_mode():
    global rec_width, rec_height, raw_width, raw_height
    camera.stop()

    config  = camera.create_still_configuration(
        main    = {
            "format"    : "BGR888", 
            "size"      : (rec_width, rec_height)
        }, 
#         raw     = {
#             "format"    : "SBGGR10_CSI2P",
#             "size"      : (raw_width, raw_height)
#         },
        buffer_count    = buffers,
        queue           = queue_control,
        controls        = {
            "ExposureTime"          : exposure_time, 
            "AnalogueGain"          : analogue_gain,
#             "FrameDurationLimits"   : (100, 100000)
            "FrameDurationLimits"   : (10000, 10000) # 10ms\u56fa\u5b9a\u306b\u3057\u3066\u53ef\u5909\u8981\u7d20\u3092\u6392\u9664\u3059\u308b
        },
#         transform       = libcamera.Transform(hflip=1, vflip=1) # \u5207\u308a\u5206\u3051\u306e\u305f\u3081\u3001\u51e6\u7406\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b
    )

    camera.configure(config)
    camera.start()

# timer\u3092\u53d7\u3051\u3066\u753b\u50cf\u3092\u53d6\u5f97\u3059\u308b\u95a2\u6570
def shutter():
    global time_log, time_log2, time_log3, frame_list, meta_data_list
    
    if len(frame_list) < number_max_frame:
        time_log.append(time.time())
        request = camera.capture_request()
        time_log2.append(time.time())
        frame_list.append(request.make_image("main"))
        meta_data_list.append(request.get_metadata())
        request.release()
        time_log3.append(time.time())
    else:
        movie_save()

# \u30e0\u30fc\u30d3\u30fc\u64ae\u5f71\u5f8c\u3001\u753b\u50cf\u3092\u9023\u7d50\u3057\u3066\u30e0\u30fc\u30d3\u30fc\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\u3059\u308b\u3002
def movie_save():
    #global recording_completed, frame_list, meta_data_list
    #recording_completed = False
    #print("save movie")
    #movie_file_path = share_folder_path + device_name + ".mp4"

    #video = cv2.VideoWriter(movie_file_path, codec, record_fps, (rec_width, rec_height))

    #if not video.isOpened():
    #    print("video writer can't be opened")
    #    sys.exit()
    #for i in range(len(frame_list)):
    #    frame = np.array(frame_list[i])
    #    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    #    video.write(frame)
    #print("movie rec finished")
    #video.release()
    print_log()

def print_log():
    global time_log, time_log2, frame_list, meta_data_list

    print("  frame interval / camera interval // capture request / extract data  (msec)"  )
    for i in range(len(time_log)-1):
        t1 = (time_log[i + 1] - time_log[i]) * 1000
        t2 = (meta_data_list[i + 1]["SensorTimestamp"] - meta_data_list[i]["SensorTimestamp"]) / 1000000
        t3 = (time_log2[i] - time_log[i]) * 1000
        t4 = (time_log3[i] - time_log2[i]) * 1000
        print(f'{t1:16.3f}' + ' / ' + f'{t2:15.3f}' + ' // ' + f'{t3:15.3f}' + ' / ' + f'{t4:12.3f}')

    
    time_log            = []
    time_log2           = []
    frame_list          = []
    meta_data_list      = []

def trigger_th():
    while True:
        trigger.on()
        time.sleep(0.01)
        trigger.off()
        time.sleep(0.05)

# \u30e1\u30a4\u30f3
if __name__ == "__main__":

    set_camera_mode()
    
    # \u30b7\u30e3\u30c3\u30bf\u30fc\u52d5\u4f5c\u691c\u51fa\u6642\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570
    #GPIO.add_event_detect(pin_shutter,  GPIO.RISING,    callback = shutter, bouncetime = 1)

    t = Thread(target=trigger_th, args=[])
    t.start()

    while True:
        if button.value:
            shutter()
  frame interval / camera interval // capture request / extract data  (msec)
          43.770 /          29.948 //          41.442 /        2.284
          27.449 /          29.949 //          25.534 /        1.874
          32.626 /          29.947 //          30.408 /        2.174
          27.269 /          29.949 //          25.349 /        1.879
          32.143 /          29.947 //          30.265 /        1.837
          27.714 /          29.949 //          25.863 /        1.811
          32.206 /          29.947 //          30.284 /        1.881
          27.604 /          19.965 //          25.760 /        1.804
          22.301 /          29.949 //          20.411 /        1.848
          33.348 /          29.947 //          25.799 /        1.899
          26.864 /          29.949 //          24.936 /        1.888
          38.727 /          29.947 //          25.435 /        1.900

結構、安定した感じ!?

@ysuito
Copy link
Author

ysuito commented Mar 20, 2024

検証用コードを踏まえての専用ハードウェア適用版。

import cv2
import os
import time
#import RPi.GPIO as GPIO
import sys
from picamera2 import Picamera2
import libcamera
import numpy as np
import gpiozero

timelog = time.time()

# raw_width           = 640           #2304
# raw_height          = 480           #1296
rec_width           = 320
rec_height          = 240
time_log            = []
time_log2           = []
time_log3           = []
frame_list          = []
meta_data_list      = []
# exposure_time       = 5000              # 1000-100000  defo:5000
exposure_time       = 1000 # 物理シャッターとの干渉を徹底的に避ける
analogue_gain       = 16	            # 1.0-20.0    defo:2.0

buffers             = 1
queue_control       = False

# Bolex
pin_shutter         = 25    # shutter timing picup 

trigger = gpiozero.Button(pin_shutter, pull_up=True)

# GPIO設定
#GPIO.setmode(GPIO.BCM)
#GPIO.setup(pin_shutter,     GPIO.IN, pull_up_down = GPIO.PUD_UP)

number_max_frame    = 32                 #連続撮影可能な最大フレーム数 とりあえず16FPS x 60sec = 960フレーム
record_fps          = 16                #MP4へ変換する際のFPS設定値
share_folder_path   = os.path.expanduser("~/share/")
device_name         = "bolex"
codec               = cv2.VideoWriter_fourcc(*'avc1')
camera              = Picamera2()

# 撮影モードに応じてpicameraのconfigを設定する
def set_camera_mode():
    global rec_width, rec_height, raw_width, raw_height
    camera.stop()

    config  = camera.create_still_configuration(
        main    = {
            "format"    : "BGR888", 
            "size"      : (rec_width, rec_height)
        }, 
#         raw     = {
#             "format"    : "SBGGR10_CSI2P",
#             "size"      : (raw_width, raw_height)
#         },
        buffer_count    = buffers,
        queue           = queue_control,
        controls        = {
            "ExposureTime"          : exposure_time, 
            "AnalogueGain"          : analogue_gain,
#             "FrameDurationLimits"   : (100, 100000)
            "FrameDurationLimits"   : (10000, 10000) # 10ms固定にして可変要素を排除する
        },
#         transform       = libcamera.Transform(hflip=1, vflip=1) # 切り分けのため、処理をスキップする
    )

    camera.configure(config)
    camera.start()

# timerを受けて画像を取得する関数
def shutter(text):
    global time_log, time_log2, time_log3, frame_list, meta_data_list
    
    if len(frame_list) < number_max_frame:
        time_log.append(time.time())
        request = camera.capture_request()
        time_log2.append(time.time())
        frame_list.append(request.make_image("main"))
        meta_data_list.append(request.get_metadata())
        request.release()
        time_log3.append(time.time())
    else:
        movie_save()

# ムービー撮影後、画像を連結してムービーファイルを保存する。
def movie_save():
    global recording_completed, frame_list, meta_data_list
    recording_completed = False
    print("save movie")
    movie_file_path = share_folder_path + device_name + ".mp4"

    video = cv2.VideoWriter(movie_file_path, codec, record_fps, (rec_width, rec_height))

    if not video.isOpened():
        print("video writer can't be opened")
        sys.exit()
    for i in range(len(frame_list)):
        frame = np.array(frame_list[i])
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        video.write(frame)
    print("movie rec finished")
    video.release()
    print_log()

def print_log():
    global time_log, time_log2, frame_list, meta_data_list

    print("  frame interval / camera interval // capture request / extract data  (msec)"  )
    for i in range(len(time_log)-1):
        t1 = (time_log[i + 1] - time_log[i]) * 1000
        t2 = (meta_data_list[i + 1]["SensorTimestamp"] - meta_data_list[i]["SensorTimestamp"]) / 1000000
        t3 = (time_log2[i] - time_log[i]) * 1000
        t4 = (time_log3[i] - time_log2[i]) * 1000
        print(f'{t1:16.3f}' + ' / ' + f'{t2:15.3f}' + ' // ' + f'{t3:15.3f}' + ' / ' + f'{t4:12.3f}')

    
    time_log            = []
    time_log2           = []
    frame_list          = []
    meta_data_list      = []

# メイン
if __name__ == "__main__":

    set_camera_mode()
    
    # シャッター動作検出時のコールバック関数
    # GPIO.add_event_detect(pin_shutter,  GPIO.RISING,    callback = shutter, bouncetime = 1)

    #while(True):
    #   time.sleep(10)

    while True:
        if trigger.value:
            shutter("dummy")

@ysuito
Copy link
Author

ysuito commented Mar 20, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment