I used this example to record a video, but there is no output. Any help, please:

import cv2

if __ name __ == " __ main __ ":

    # find the webcam

    capture = cv2.VideoCapture(0)

    # video recorder
    fourcc = cv2.cv.CV_FOURCC(*'XVID')  # cv2.VideoWriter_fourcc() does not exist
    videoOut = cv2.VideoWriter("output.avi", fourcc, 20.0, (640, 480))

    # record video
    while (capture.isOpened()):
        ret, frame = capture.read()
        if ret:
            cv2.imshow('Video Stream', frame)


        # Tiny Pause
        key = cv2.waitKey(1)


I got this error:


(Video Stream:2758): Gtk-WARNING **: cannot open display: 

You're trying to run it in headless mode. A bug/feature of some OpenCV things is that it's a must-have requirement to be run from Xterm of already working X11/GUI. Weird, but the root of the problem is that it uses some external components like video codecs in your case via GUI-related component HighUI. If you have a working and running X11/GUI but still have this error - make sure to set the environment variable DISPLAY to your X11 display and after setting it via export command, run your program like DISPLAY=xx:yy your_binary via env importing on-exec.

  • I tried it with a HDMI cable but I got the same error – happy May 6 '16 at 21:36
  • @happy from GUI Xterm? – Alexey Vesnin May 6 '16 at 21:40
  • no,but normally with hdmi we can display it no?? – happy May 6 '16 at 21:44
  • @happy if you haven't exported a DISPLAY variable - no, a headless call is headless literally – Alexey Vesnin May 6 '16 at 21:45
  • haw can I exported it ??? – happy May 6 '16 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.