Skip to content

Commit 516ec8f

Browse files
committed
Fix cap prop frame width/height
Update code so it works in both OpenCV 2 and 3 re: cv2.cv.CV_CAP_PROP_FRAME_WIDTH, see issue jBenes#13.
1 parent 548c6b1 commit 516ec8f

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

chapter1/chapter1.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,13 @@ def main():
8787
capture = cv2.VideoCapture(0)
8888
if not(capture.isOpened()):
8989
capture.open()
90-
capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
91-
capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
90+
91+
if hasattr(cv2, 'cv'):
92+
capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
93+
capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
94+
else:
95+
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
96+
capture.set(cv2.CV_CAP_PROP_FRAME_HEIGHT, 480)
9297

9398
# start graphical user interface
9499
app = wx.App()

chapter2/chapter2.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def _process_frame(self, frame):
5858

5959
# draw some helpers for correctly placing hand
6060
height, width = frame.shape[:2]
61-
cv2.circle(img_draw, (width/2, height/2), 3, [255, 102, 0], 2)
62-
cv2.rectangle(img_draw, (width/3, height/3), (width*2/3, height*2/3),
61+
cv2.circle(img_draw, (width / 2, height / 2), 3, [255, 102, 0], 2)
62+
cv2.rectangle(img_draw, (width / 3, height / 3), (width * 2 / 3, height * 2 / 3),
6363
[255, 102, 0], 2)
6464

6565
# print number of fingers on image
@@ -75,8 +75,12 @@ def main():
7575
if not(capture.isOpened()):
7676
capture.open(device)
7777

78-
capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
79-
capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
78+
if hasattr(cv2, 'cv'):
79+
capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
80+
capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
81+
else:
82+
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
83+
capture.set(cv2.CV_CAP_PROP_FRAME_HEIGHT, 480)
8084

8185
# start graphical user interface
8286
app = wx.App()

chapter3/chapter3.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class FeatureMatchingLayout(BaseLayout):
2323
Each captured frame is passed to the FeatureMatching class, so that an
2424
object of interest can be tracked.
2525
"""
26+
2627
def _init_custom_layout(self):
2728
"""Initializes feature matching class"""
2829
self.matching = FeatureMatching(train_image='salinger.jpg')
@@ -46,8 +47,12 @@ def main():
4647
if not(capture.isOpened()):
4748
capture.open()
4849

49-
capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
50-
capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
50+
if hasattr(cv2, 'cv'):
51+
capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
52+
capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
53+
else:
54+
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
55+
capture.set(cv2.CV_CAP_PROP_FRAME_HEIGHT, 480)
5156

5257
# start graphical user interface
5358
app = wx.App()

chapter7/chapter7.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,12 @@ def main():
274274
if not(capture.isOpened()):
275275
capture.open()
276276

277-
capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
278-
capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
277+
if hasattr(cv2, 'cv'):
278+
capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
279+
capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
280+
else:
281+
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
282+
capture.set(cv2.CV_CAP_PROP_FRAME_HEIGHT, 480)
279283

280284
# start graphical user interface
281285
app = wx.App()

0 commit comments

Comments
 (0)