質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

0回答

199閲覧

jupyter notebookで上手く起動しません

yk0918

総合スコア0

Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2022/06/16 11:38

jupyter notebookでpythonを起動し、以下のコードを入れても、In【*】の状態で動きません。
原因が分からず困っています。
ご教授いただければ幸いです。

python

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

meg_

2022/06/16 14:21

動作しないのは質問のコードだけですか?
yk0918

2022/06/16 16:21

そうです
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問