0

I read nearly all of questions about this subject but I didn't solve it. I am working on Linux/Mint. I tried to open my webcam (or any video it does not matter) with opencv and read a frame from the video. And I write this code:

import cv2

cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()
    print ret #It returns always false

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

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

cap.release()
cv2.destroyAllWindows()

--> I read the problem might can be because of opencv installation. I checked the installation, it seems true. When I write 'pip freeze', the result list contained opencv-python module so I thought the installation is correct and it isn't the reason for my problem.

--> I tried to change the index in videoCapture(0) to -1 and 1, and it again failed.

--> And then, I read that the other cause can be FFmpeg or codec. But I also installed FFmpeg and the problem didn't change.

--> cv2.VideoCapture() didn't give any error but I want to install cv2 module on the Poject>Settings>Project Interpreter, it gave me an error (executed command: pip install cv2) But when I run this command, it gave me this error : Could not find a version that satisfies the requirement cv2 (from versions: ) No matching distribution found for cv2 I checked the cv2 module in my virtualenv and it seems there. (/cv_env/lib/python2.7/site-packages/cv2)

--> I also tried it with "skvideo.io.VideoCapture()" but I failed again.

So, I didn't understand the problem and I am really new in this subject. What is the problem?

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Selin Gök
  • 331
  • 1
  • 5
  • 20
  • Read this carefully https://stackoverflow.com/questions/47112642/permanent-fix-for-opencv-videocapture/47116576#47116576. – zindarod Nov 15 '17 at 18:31
  • can you open/play your camera with any other tool/application/player? – Micka Nov 15 '17 at 18:52
  • @zindarod I read that answer. "python -c "import cv2; print(cv2.getBuildInformation())" | grep -i FFmpeg" this command gave me NO result and then I follow [these steps](https://askubuntu.com/questions/699502/ffmpeg-command-not-found) for installation FFmpeg but it still gives NO result. – Selin Gök Nov 15 '17 at 18:58
  • @Micka Yes, I tested it with Cheese. It is opening. – Selin Gök Nov 15 '17 at 19:02
  • Your opencv does not have ffmpeg (probably lib4l2 as well) support. Installing ffmpeg alone won't help. You've to build opencv again. – zindarod Nov 15 '17 at 19:45

1 Answers1

0
sudo modprobe bcm2835-v4l2

will "enable" the your pi camera for opencv automatically.

Isa Gul
  • 56
  • 3