以下のコードで動かすと、retが出てきてしまい上手く画像が取れません。
解決方法をお分かりの方、ご教授いただければ幸いです。
python
1コード 2```import datetime 3from multiprocessing import Process, Queue 4import os 5import time 6 7import cv2 8import numpy as np 9 10# カメラのIDを指定。 11cam_id=0 12 13# 画像を保存するかと、測定時間を設定。 14SAVE_IMG = True 15live_time = 300 # seconds(0 or less is endless) 16parent_path = [] 17 18 19start_time = 0 20parent_path += [datetime.datetime.now().strftime('%y%m%d_%H%M')] 21path = os.path.join(*parent_path) 22if SAVE_IMG: 23 os.makedirs(path, exist_ok=True) 24w, h =1280, 960 25 26def get_img(q): 27 """ 28 カメラからデータを受信する関数。 29 """ 30 print('initialize...') 31 try: 32 # 初期設定を行う。 33 cap = cv2.VideoCapture(cam_id,cv2.CAP_MSMF) 34 35 cap.set(3, w) 36 cap.set(4, h) 37 ret = False 38 while not ret: 39 time.sleep(0.2) 40 ret, frame0 = cap.read() 41 print(frame0) 42 print(frame0.shape) 43 44 t = 0 45 print('start') 46 47 start_time = datetime.datetime.now() 48 i = 0 49 50 # カメラからデータを受信しつづけるループ。 51 while True: 52 end_time = datetime.datetime.now() 53 ret, frame = cap.read() 54 #ret, frame = True, frame0 55 if not ret or frame is None: 56 continue 57 58 # 測定時間が終わったらループを抜ける。 59 if live_time > 0 and end_time.timestamp() - start_time.timestamp() > live_time: 60 while not q.empty(): 61 time.sleep(1) 62 print("end") 63 cap.release() 64 if SAVE_IMG: 65 with open(os.path.join(path,"settings.txt"), "w") as f: 66 f.write(f"livetime{live_time}\nstart{start_time}\nend {end_time}\n") 67 return 0 68 if ret: 69 i += 1 70 print(f"ret{i}") 71 q.put((i, frame)) 72 73 # C-cまたはエラーで終了。 74 except KeyboardInterrupt: 75 cap.release() 76 except Exception as e: 77 print(e) 78 cap.release() 79 80def show_img(q,qf): 81 """ 82 画像データを保存する。 83 """ 84 t = 0 85 g = 1 86 f = 0 87 show=True 88 fname='frames' 89 frame_b = np.zeros((1,)) 90 frame_s = np.zeros((1,)) 91 try: 92 while True: 93 if q.empty(): 94 time.sleep(0.01) 95 continue 96 i, frame = q.get() 97 if frame.size < 2: 98 print("reset") 99 frame_b = np.zeros_like(frame_b) 100 #frame_b += 255 101 continue 102 print(f"qsize:{qf.qsize()}/{q.qsize()}, max={np.amax(frame)}") 103 104 # ファイル名はフレームごとの連番。 105 if SAVE_IMG and np.amax(frame) > 10: 106 cv2.imwrite(os.path.join(path, f'{i}.png'), frame) 107 108 if g>0: 109 frame = np.average(frame, axis=2) 110 frame = frame.astype(np.uint8) 111 112 if show: 113 qf.put((i, frame_b)) 114 t = time.time() 115 if np.amax(frame) < 10: 116 continue 117 #frame[frame<10] = 255 118 119 if frame_b.shape != frame.shape: 120 frame_b = frame 121 print(np.amax(frame)) 122 frame_b = np.maximum(frame, frame_b) 123 except KeyboardInterrupt: 124 cv2.destroyAllWindows() 125 return None 126 except Exception as e: 127 print(e) 128 129def draw_img(q,qf): 130 """ 131 重ね合わせた画像をリアルタイムで表示する。 132 実行中にRキーで表示を初期化。 133 """ 134 cv2.namedWindow("frame", cv2.WINDOW_NORMAL) 135 t = 0 136 show_max = True 137 frame_b = np.array([]) 138 139 while qf.empty(): 140 time.sleep(0.01) 141 print("start") 142 last_i = 0 143 start_time = datetime.datetime.now().timestamp() 144 try: 145 while True: 146 if qf.empty(): 147 time.sleep(0.01) 148 continue 149 else: 150 t = time.time() 151 i, frame = qf.get() 152 if i < last_i: 153 continue 154 last_i = i 155 if show_max and frame_b.shape == frame.shape: 156 frame_b = np.maximum(frame, frame_b) 157 else: 158 frame_b = frame 159 frame_s = frame_b.copy() 160 fps= i / max(1,datetime.datetime.now().timestamp() - start_time) 161 # 現在のフレームレートを表示する画像に書き込み。 162 cv2.putText(frame_s,text=f'{fps:.2f} fps', org=(40,40),fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale=1, color=(255,255,255)) 163 cv2.imshow('frame',frame_s)#cv2.cvtColor(frame, cv2.COLOR_YUV420p2BGR)) 164 if cv2.waitKey(1) == ord('r'): 165 frame_b = np.zeros_like(frame) 166 #q.put(np.ndarray([0,])) 167 except KeyboardInterrupt: 168 cv2.destroyAllWindows() 169 return None 170 171 except Exception as e: 172 print(e) 173 cv2.destroyAllWindows() 174 175if __name__ == '__main__': 176 # 高速化のため6重に起動。 177 try: 178 q = Queue(1000) 179 qf = Queue(10) 180 for n in range(6): 181 p = Process(target=show_img, args=(q,qf)) 182 p.start() 183 pd = Process(target=draw_img, args=(q,qf)) 184 pd.start() 185 get_img(q) 186 p.terminate() 187 pd.terminate() 188 except Exception as e: 189 print(e) 190 p.terminate() 191 pd.terminate()
実行すると、以下のように出ます
initialize...
[[[106 106 106]
[106 106 106]
[106 106 106]
...
[214 214 214]
[214 214 214]
[214 214 214]]
[[106 106 106]
[106 106 106]
[106 106 106]
...
[214 214 214]
[214 214 214]
[214 214 214]]
[[106 106 106]
[106 106 106]
[106 106 106]
...
[214 214 214]
[214 214 214]
[214 214 214]]
...
[[ 61 61 61]
[ 61 61 61]
[ 61 61 61]
...
[146 146 146]
[146 146 146]
[146 146 146]]
[[ 61 61 61]
[ 61 61 61]
[ 61 61 61]
...
[146 146 146]
[146 146 146]
[146 146 146]]
[[ 61 61 61]
[ 61 61 61]
[ 61 61 61]
...
[146 146 146]
[146 146 146]
[146 146 146]]]
(960, 1280, 3)
start
ret1
ret2
ret3
ret4
ret5
ret6
ret7
ret8
ret9
ret10
ret11
ret12
ret13
ret14
ret15
ret16
ret17
ret18
ret19
ret20
・・・
のように、ret1001まで続きます
あなたの回答
tips
プレビュー