質問編集履歴

1

コードを追加

2020/04/23 04:54

投稿

mojyagorio
mojyagorio

スコア17

test CHANGED
File without changes
test CHANGED
@@ -15,3 +15,101 @@
15
15
  以上
16
16
 
17
17
  よろしくお願いいたします。
18
+
19
+
20
+
21
+ ```Python
22
+
23
+
24
+
25
+ ef detect_video(yolo, video_path, output_path=""):
26
+
27
+ import cv2
28
+
29
+ vid = cv2.VideoCapture(video_path)
30
+
31
+ if not vid.isOpened():
32
+
33
+ raise IOError("Couldn't open webcam or video")
34
+
35
+ video_FourCC = int(vid.get(cv2.CAP_PROP_FOURCC))
36
+
37
+ video_fps = vid.get(cv2.CAP_PROP_FPS)
38
+
39
+ video_size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)),
40
+
41
+ int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)))
42
+
43
+ isOutput = True if output_path != "" else False
44
+
45
+ if isOutput:
46
+
47
+ print("!!! TYPE:", type(output_path), type(video_FourCC), type(video_fps), type(video_size))
48
+
49
+ out = cv2.VideoWriter(output_path, video_FourCC, video_fps, video_size)
50
+
51
+ accum_time = 0
52
+
53
+ curr_fps = 0
54
+
55
+ fps = "FPS: ??"
56
+
57
+ prev_time = timer()
58
+
59
+ while True:
60
+
61
+ return_value, frame = vid.read()
62
+
63
+ image = Image.fromarray(frame)
64
+
65
+ image = yolo.detect_image(image)
66
+
67
+ result = np.asarray(image)
68
+
69
+ curr_time = timer()
70
+
71
+ exec_time = curr_time - prev_time
72
+
73
+ prev_time = curr_time
74
+
75
+ accum_time = accum_time + exec_time
76
+
77
+ curr_fps = curr_fps + 1
78
+
79
+ if accum_time > 1:
80
+
81
+ accum_time = accum_time - 1
82
+
83
+ fps = "FPS: " + str(curr_fps)
84
+
85
+ curr_fps = 0
86
+
87
+ cv2.putText(result, text=fps, org=(3, 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
88
+
89
+ fontScale=0.50, color=(255, 0, 0), thickness=2)
90
+
91
+ cv2.namedWindow("result", cv2.WINDOW_NORMAL)
92
+
93
+ cv2.imshow("result", result)
94
+
95
+ if isOutput:
96
+
97
+ out.write(result)
98
+
99
+ if cv2.waitKey(1) & 0xFF == ord('q'):
100
+
101
+ break
102
+
103
+ yolo.close_session()
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+ ```