回答編集履歴

1

サンプルコード追加

2019/06/08 13:31

投稿

amahara_waya
amahara_waya

スコア1029

test CHANGED
@@ -7,3 +7,87 @@
7
7
  [【Python/OpenCV】動画ファイルの読み込み・再生
8
8
 
9
9
  ](https://algorithm.joho.info/programming/python/opencv-videocapture-mp4-movie-py/)
10
+
11
+
12
+
13
+ -追記-
14
+
15
+
16
+
17
+ 以下、サンプルコードになります。
18
+
19
+
20
+
21
+ ```ここに言語を入力
22
+
23
+ import pygame
24
+
25
+ from pygame.locals import *
26
+
27
+ import cv2
28
+
29
+ import numpy as np
30
+
31
+ import sys
32
+
33
+
34
+
35
+ camera = cv2.VideoCapture(0)
36
+
37
+ pygame.init()
38
+
39
+ pygame.display.set_caption("OpenCV camera stream on Pygame")
40
+
41
+ screen = pygame.display.set_mode([1280,720])
42
+
43
+
44
+
45
+ try:
46
+
47
+ while True:
48
+
49
+
50
+
51
+ ret, frame = camera.read()
52
+
53
+
54
+
55
+ screen.fill([0,0,0])
56
+
57
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
58
+
59
+ frame = np.rot90(frame)
60
+
61
+ frame = pygame.surfarray.make_surface(frame)
62
+
63
+ screen.blit(frame, (0,0))
64
+
65
+ pygame.display.update()
66
+
67
+
68
+
69
+ for event in pygame.event.get():
70
+
71
+ if event.type == KEYDOWN:
72
+
73
+ sys.exit(0)
74
+
75
+ except KeyboardInterrupt:
76
+
77
+ pygame.quit()
78
+
79
+ cv2.destroyAllWindows()
80
+
81
+
82
+
83
+ except SystemExit:
84
+
85
+ pygame.quit()
86
+
87
+ cv2.destroyAllWindows()
88
+
89
+ ```
90
+
91
+
92
+
93
+ [opencv_video_to_pygame.py](https://gist.github.com/radames/1e7c794842755683162b)