0

I'm using python-OpenCV version 4.4 and when running just a basic code block:

import cv2

camera_capture = cv2.VideoCapture(0)
fps = 30
size = int(camera_capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),\
        int(camera_capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
video_writer = cv2.VideoWriter('MyOutputVid.avi', cv2.cv.CV_FOURCC('I','4','2','0'),
fps, size)

I get AttributeError: module 'cv2.cv2' has no attribute 'cv' I understand there has been a transition from OpenCV to OpenCV2 but how the imports were affected ?

I found similar topic here but removing second cv2 from the code didn't help in my case. It just threw another error AttributeError: module 'cv2.cv2' has no attribute 'CV_CAP_PROP_FRAME_WIDTH'

How can I use the attributes ?

Mark
  • 1,385
  • 3
  • 16
  • 29
  • See https://stackoverflow.com/q/51853018/11667949 – Shivam Jha Sep 26 '20 at 19:07
  • @ShivamJha How does ^ this help ? I don't have problem with installation. – Mark Sep 26 '20 at 19:12
  • You could test a couple of combinations i guess. It probably should be " cv2.CAP_PROP_FRAME_HEIGHT ". See some more examples here : https://stackoverflow.com/questions/31076403/updating-code-from-opencv-to-opencv2 – Grebtsew Sep 26 '20 at 20:44
  • @Grebtsew I've tried that one already. Got the error `cv2 has no attribute CAP_PROP_FRAME_HEIGHT` – Mark Sep 26 '20 at 20:46
  • Okej, It seems to be working for me on python 3.8, cv2 4.4.0.42. Tried reinstalling it? – Grebtsew Sep 26 '20 at 20:51
  • You should also replace cv2.cv.CV_FOURCC(...) with cv2.VideoWriter_fourcc(...) – Grebtsew Sep 26 '20 at 20:54

1 Answers1

0

So this code seems to be working. Hopefully doing what you want. Works on windows 10, python3 3.8.6 64-bit, opencv-python 4.4.0.42.

import cv2

camera_capture = cv2.VideoCapture(0)

fps = 30
size = int(camera_capture.get(cv2.CAP_PROP_FRAME_WIDTH)),\
        int(camera_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))

vidwrite = cv2.VideoWriter('testvideo.avi', cv2.VideoWriter_fourcc('I','4','2','0'), fps, 
           size,True)


# Write and show recording
while camera_capture.isOpened():

        _, frame =  camera_capture.read()
        vidwrite.write(frame)

        cv2.imshow("showlive", frame)
        cv2.waitKey(1)

If you still have issues with CAP_PROP_FRAME_HEIGHT, I would suggest reinstalling opencv. Some functions in OpenCV is not supported for 32-bit version of python3, might be worth checking aswell. An alternative ofcourse is to instead use height, width, _ = frame.shape see frame in the code above.

Grebtsew
  • 192
  • 5
  • 13
  • 1
    So the main issue was `CV_CAP_PROP_FRAME_WIDTH` vs `CAP_PROP_FRAME_WIDTH` during the transition from cv1 to cv2 they apparently changed names of the methods as well. I'm learning from a book that was published 2016 and uses cv2 but some of the methods the book uses remained unchanged and throw an error. – Mark Sep 27 '20 at 09:21