0

This is my first time using OpenCV and I'm having some difficulties. I just made a simple program to see if it was imported correctly:

import cv2
import numpy as np

print("hi")
cap = cv2.VideoCapture(0)
print(cap.isOpened())
print("hi")
while True:
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break


cap.release()
cv2.destroyAllWindows()

When I run this file on the MacOS terminal, it works fine and opens up the video window, but when I run this file with IDLE nothing happens. No errors or warnings, the first "hi" prints but nothing else and the program ends. It's like as soon as I try to use cv2 directly after the first "hi", the program just ends. I believe I'm using the same version of python in both my terminal and IDLE, and I've tried uninstalling and reinstalling OpenCV using pip. Did I not install OpenCV correctly to be using with IDLE? Any help is greatly appreciated!

Edit: Not sure if this is causing any problems, but when I first ran it from IDLE, it had a few "Python quit unexpectedly" windows before I tried it in terminal. When I tried it there, it asked for access to my computer's webcam. Since then, the "Python quit unexpectedly" windows haven't showed up when I tried IDLE, so maybe it just needs access to my camera? I don't see an option for that in my Mac settings though, there's no IDLE app option to give access to.

14
  • 1
    Does it work if you start IDLE from the terminal? If it's a problem that happens only when you don't have environment variables &c set by your shell dotfiles, that gives us a place to look. Commented Feb 15, 2023 at 21:18
  • That's so weird, it worked when I started IDLE from the terminal! Do you know what could be causing that? Commented Feb 15, 2023 at 21:22
  • 1
    See my first comment ("If it's a problem that happens only when you don't have environment variables &c set by your shell dotfiles") -- the details depend a little based on which operating system you're on (for example, on MacOS it's launchd that controls the environment for things you start from the GUI, IIRC), but in general your shell dotfiles are only guaranteed to run when you start things from a shell. (It didn't used to be that way on UNIXlike systems -- you used to have the GUI started from a login shell so your .profile or equivalent was run -- but that was rather a long time ago). Commented Feb 15, 2023 at 21:24
  • 1
    A good place to start is to compare os.environ between the working and broken environments, and set variables from the working environment into the broken one one-by-one until you find out which one, when set, solves the problem. The set of environment variables isn't the only possible problem, but it's by far the most likely one. Commented Feb 15, 2023 at 21:31
  • 1
    Once you've figured out which variable needs to be set, you might see Setting environment variables in OS X for GUI applications on Super User. Commented Feb 15, 2023 at 21:33

1 Answer 1

0

Can you try this.

Snippet:

# -*- coding: utf-8 -*-
import numpy as np
import cv2

cap = cv2.VideoCapture(0)
print(cap.isOpened())

while(cap.isOpened()):
    ret, frame = cap.read()

    cv2.imshow('frame', frame)
    if (cv2.waitKey(1) & 0xFF == ord('q')):
        break
cap.release()
cv2.destroyAllWindows()

Or:

snippet:

import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

rval, frame = vc.read()

while True:

  if frame is not None:   
     cv2.imshow("preview", frame)
  rval, frame = vc.read()

  if cv2.waitKey(1) & 0xFF == ord('q'):
     break
Sign up to request clarification or add additional context in comments.

1 Comment

Nothing happened for either of those, the shell just restarts after a second or two when I try to run those.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.