teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

ソースコードの改善

2020/10/22 01:11

投稿

yuumura
yuumura

スコア3

title CHANGED
File without changes
body CHANGED
@@ -6,107 +6,22 @@
6
6
  ### 発生している問題・エラーメッセージ
7
7
 
8
8
  ```
9
- (オリジナルのエラーメッセージVideoCaptureができてないこを示しる)
9
+ VideoCaptureが読み込めずFalseなっしまう。
10
- failed in opening VideoCapture
11
10
  ```
12
11
 
13
12
  ### 該当のソースコード
14
13
 
15
14
  ```python
16
15
 
17
- #いっぱい書いてあるけどできてないのはcv2.VideoCapture(0)
16
+
18
- import sys, os
19
- from PyQt5.QtWidgets import *
20
- from PyQt5.QtCore import *
21
- from PyQt5.QtGui import *
22
17
  import cv2
23
- import numpy as np
24
18
 
25
- class VideoCaptureView(QGraphicsView):
19
+ cap_cam = cv2.VideoCapture(0)
20
+ print(type(cap_cam))
26
- """ ビデオキャプチャ """
21
+ # <class 'cv2.VideoCapture'>
27
- repeat_interval = 200 # ms 間隔で画像更新
28
22
 
29
- def __init__(self, parent = None):
23
+ print(cap_cam.isOpened())
30
- """ コンストラクタ(インスタンスが生成される時に呼び出される) """
31
- super(VideoCaptureView, self).__init__(parent)
32
-
33
- # 変数を初期化
34
- self.pixmap = None
35
- self.item = None
36
24
 
37
-
38
-
39
-
40
- #できてないとこ
41
-
42
-
43
- # VideoCapture (カメラからの画像取り込み)を初期化
44
- self.capture = cv2.VideoCapture(0)
45
-
46
- if self.capture.isOpened() is False:
47
- raise IOError("failed in opening VideoCapture")
48
-
49
-
50
-
51
-
52
-
53
-
54
- # ウィンドウの初期化
55
- self.scene = QGraphicsScene() # 描画用キャンバスを作成
56
- self.setScene(self.scene)
57
- self.setVideoImage()
58
-
59
- # タイマー更新 (一定間隔でsetVideoImageメソッドを呼び出す)
60
- self.timer = QTimer(self)
61
- self.timer.timeout.connect(self.setVideoImage)
62
- self.timer.start(self.repeat_interval)
63
-
64
- def setVideoImage(self):
65
- """ ビデオの画像を取得して表示 """
66
- ret, cv_img = self.capture.read() # ビデオキャプチャデバイスから画像を取得
67
- if ret == False:
68
- return
69
- cv_img = cv2.cvtColor(cv_img,cv2.COLOR_BGR2RGB) # 色変換 BGR->RGB
70
-
71
- cv_img = self.processing(cv_img)
72
-
73
- height, width, dim = cv_img.shape
74
- bytesPerLine = dim * width # 1行辺りのバイト数
75
-
76
- #cv_img = self.processing(cv_img)
77
-
78
- self.image = QImage(cv_img.data, width, height, bytesPerLine, QImage.Format_RGB888)
79
- if self.pixmap == None: # 初回はQPixmap, QGraphicPixmapItemインスタンスを作成
80
- self.pixmap = QPixmap.fromImage(self.image)
81
- self.item = QGraphicsPixmapItem(self.pixmap)
82
- self.scene.addItem(self.item) # キャンバスに配置
83
- else:
84
- self.pixmap.convertFromImage(self.image) # 2回目以降はQImage, QPixmapを設定するだけ
85
- self.item.setPixmap(self.pixmap)
86
-
87
- def processing(self, src):
88
- """ 画像処理 """
89
- im = src.copy()
90
-
91
-
92
-
93
- dst = im
94
-
95
- return dst
96
-
97
- if __name__ == '__main__':
98
- app = QApplication(sys.argv)
99
- app.aboutToQuit.connect(app.deleteLater)
100
-
101
- main = QMainWindow() # メインウィンドウmainを作成
102
- main.setWindowTitle("Video Capture")
103
- viewer = VideoCaptureView() # VideoCaptureView ウィジエットviewを作成
104
- main.setCentralWidget(viewer) # mainにviewを埋め込む
105
- main.show()
106
-
107
- app.exec_()
108
-
109
- viewer.capture.release()
110
25
  ```
111
26
 
112
27
  ### 試したこと