回答編集履歴

2

d

2020/08/24 07:07

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -25,3 +25,133 @@
25
25
 
26
26
 
27
27
  ちなみに、Youtube の動画は一般的に30FPSです。
28
+
29
+
30
+
31
+ ## 追記
32
+
33
+
34
+
35
+ ```python
36
+
37
+ def detect_video(yolo, video_path, output_path=""):
38
+
39
+ import cv2
40
+
41
+
42
+
43
+ vid = cv2.VideoCapture(video_path)
44
+
45
+ if not vid.isOpened():
46
+
47
+ raise IOError("Couldn't open webcam or video")
48
+
49
+ video_FourCC = int(vid.get(cv2.CAP_PROP_FOURCC))
50
+
51
+ video_fps = vid.get(cv2.CAP_PROP_FPS)
52
+
53
+ video_size = (
54
+
55
+ int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)),
56
+
57
+ int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)),
58
+
59
+ )
60
+
61
+ isOutput = True if output_path != "" else False
62
+
63
+ if isOutput:
64
+
65
+ print(
66
+
67
+ "!!! TYPE:",
68
+
69
+ type(output_path),
70
+
71
+ type(video_FourCC),
72
+
73
+ type(video_fps),
74
+
75
+ type(video_size),
76
+
77
+ )
78
+
79
+ out = cv2.VideoWriter(output_path, video_FourCC, video_fps, video_size)
80
+
81
+ accum_time = 0
82
+
83
+ curr_fps = 0
84
+
85
+ fps = "FPS: ??"
86
+
87
+ prev_time = timer()
88
+
89
+ i = 0
90
+
91
+ while True:
92
+
93
+ return_value, frame = vid.read()
94
+
95
+ image = Image.fromarray(frame)
96
+
97
+
98
+
99
+ if i % 3 == 0:
100
+
101
+ image = yolo.detect_image(image)
102
+
103
+ result = np.asarray(image)
104
+
105
+ curr_time = timer()
106
+
107
+ exec_time = curr_time - prev_time
108
+
109
+ prev_time = curr_time
110
+
111
+ accum_time = accum_time + exec_time
112
+
113
+ curr_fps = curr_fps + 1
114
+
115
+ if accum_time > 1:
116
+
117
+ accum_time = accum_time - 1
118
+
119
+ fps = "FPS: " + str(curr_fps)
120
+
121
+ curr_fps = 0
122
+
123
+ cv2.putText(
124
+
125
+ result,
126
+
127
+ text=fps,
128
+
129
+ org=(3, 15),
130
+
131
+ fontFace=cv2.FONT_HERSHEY_SIMPLEX,
132
+
133
+ fontScale=0.50,
134
+
135
+ color=(255, 0, 0),
136
+
137
+ thickness=2,
138
+
139
+ )
140
+
141
+ cv2.namedWindow("result", cv2.WINDOW_NORMAL)
142
+
143
+ cv2.imshow("result", result)
144
+
145
+ if isOutput:
146
+
147
+ out.write(result)
148
+
149
+ if cv2.waitKey(1) & 0xFF == ord("q"):
150
+
151
+ break
152
+
153
+ i += 1
154
+
155
+ yolo.close_session()
156
+
157
+ ```

1

修正

2020/08/24 07:06

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -16,6 +16,12 @@
16
16
 
17
17
  その頻度の検出でも使いたい用途で問題ないのであれば、それでいいのではないでしょうか。
18
18
 
19
- 動画は30FPSで流したい場合は「3フレームに1回だけ検出す」とか工夫すれば、動画自体は30FPSで流せます。
19
+ 動画は30FPSで流したい場合は「3フレームに1回だけ検出して、残り2フレームは前回検出した結果を使い回す」とか工夫すれば、動画自体は30FPSで流せます。
20
20
 
21
+
22
+
23
+ ----
24
+
25
+
26
+
21
- 例えば、Youtube の動画は一般的に30FPSです。
27
+ ちなみに、Youtube の動画は一般的に30FPSです。