回答編集履歴
1
サンプルコード追加
answer
CHANGED
@@ -2,4 +2,46 @@
|
|
2
2
|
扱う動画のファイル形式にもよりますが、大抵の動画は再生できるんじゃないかと。
|
3
3
|
|
4
4
|
[【Python/OpenCV】動画ファイルの読み込み・再生
|
5
|
-
](https://algorithm.joho.info/programming/python/opencv-videocapture-mp4-movie-py/)
|
5
|
+
](https://algorithm.joho.info/programming/python/opencv-videocapture-mp4-movie-py/)
|
6
|
+
|
7
|
+
-追記-
|
8
|
+
|
9
|
+
以下、サンプルコードになります。
|
10
|
+
|
11
|
+
```ここに言語を入力
|
12
|
+
import pygame
|
13
|
+
from pygame.locals import *
|
14
|
+
import cv2
|
15
|
+
import numpy as np
|
16
|
+
import sys
|
17
|
+
|
18
|
+
camera = cv2.VideoCapture(0)
|
19
|
+
pygame.init()
|
20
|
+
pygame.display.set_caption("OpenCV camera stream on Pygame")
|
21
|
+
screen = pygame.display.set_mode([1280,720])
|
22
|
+
|
23
|
+
try:
|
24
|
+
while True:
|
25
|
+
|
26
|
+
ret, frame = camera.read()
|
27
|
+
|
28
|
+
screen.fill([0,0,0])
|
29
|
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
30
|
+
frame = np.rot90(frame)
|
31
|
+
frame = pygame.surfarray.make_surface(frame)
|
32
|
+
screen.blit(frame, (0,0))
|
33
|
+
pygame.display.update()
|
34
|
+
|
35
|
+
for event in pygame.event.get():
|
36
|
+
if event.type == KEYDOWN:
|
37
|
+
sys.exit(0)
|
38
|
+
except KeyboardInterrupt:
|
39
|
+
pygame.quit()
|
40
|
+
cv2.destroyAllWindows()
|
41
|
+
|
42
|
+
except SystemExit:
|
43
|
+
pygame.quit()
|
44
|
+
cv2.destroyAllWindows()
|
45
|
+
```
|
46
|
+
|
47
|
+
[opencv_video_to_pygame.py](https://gist.github.com/radames/1e7c794842755683162b)
|