回答編集履歴

1

追記

2018/05/31 10:46

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -20,14 +20,68 @@
20
20
 
21
21
  Returns true if video capturing has been initialized already.
22
22
 
23
-
24
-
25
- > If the previous call to VideoCapture constructor or VideoCapture::open() succeeded, the method returns true.
23
+ If the previous call to VideoCapture constructor or VideoCapture::open() succeeded, the method returns true.
26
24
 
27
25
 
28
26
 
29
27
  引用元: [cv::VideoCapture Class Reference - isOpened()](https://docs.opencv.org/3.2.0/d8/dfe/classcv_1_1VideoCapture.html#a9d2ca36789e7fcfe7a7be3b328038585)
30
28
 
29
+ サンプルコードが不適切ってことになるんですかねぇ...
31
30
 
32
31
 
32
+
33
+ 書くなら
34
+
35
+ ---
36
+
37
+ 私なら、こんな感じで書きます。
38
+
39
+ ```Python
40
+
41
+ import numpy as np
42
+
43
+ import cv2
44
+
45
+
46
+
33
- サンプコードが不適切ってことになるんですかねぇ...
47
+ cap = cv2.VideoCapture(ファイ名)
48
+
49
+ assert cap.isOpened(), 'Failed to open video file.'
50
+
51
+
52
+
53
+ ret, frame = cap.read()
54
+
55
+ while ret:
56
+
57
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
58
+
59
+
60
+
61
+ cv2.imshow('frame', gray)
62
+
63
+ if cv2.waitKey(1) & 0xFF == ord('q'):
64
+
65
+ break
66
+
67
+
68
+
69
+ ret, frame = cap.read()
70
+
71
+ else:
72
+
73
+ print('Finish to play video.')
74
+
75
+
76
+
77
+ cap.release()
78
+
79
+ cv2.destroyAllWindows()
80
+
81
+ ```
82
+
83
+
84
+
85
+ 再生を 中断したとき/完遂したとき で異なった処理が出来るようにしてみました。
86
+
87
+ また、ファイルのオープンに失敗したときは早めに例外を投げるようにしています。