0

Error for the camera in PyCharm. What can I do in this case?

import cv2
import numpy as np
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(1)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10,150)
while True:
    success, img = cap.read()
    cv2.imshow("Result", img)
    if cv2.waitKey(1) and 0xFF == ord('q'):
     break
C:\Users\user\PycharmProjects\OpenCvPython\venv\Scripts\python.exe C:/Users/user/PycharmProjects/OpenCvPython/chapter7.py
[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ep71p_ws\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/OpenCvPython/chapter7.py", line 11, in <module>
    cv2.imshow("Result", img)
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ep71p_ws\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'


Process finished with exit code 1
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
six dogs
  • 11
  • 1
  • Does this answer your question? [(-215:Assertion failed) !\_src.empty() in function 'cv::cvtColor'](https://stackoverflow.com/questions/53926657/215assertion-failed-src-empty-in-function-cvcvtcolor) – Christoph Rackwitz Jul 31 '21 at 19:19

1 Answers1

0

Check if the video capture frame was successfully retrieved first, for example:

import cv2
import numpy as np
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(1)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10,150)
while True:
    success, img = cap.read()

    if not success:
      print('error retrieving frame')
      continue

    cv2.imshow("Result", img)
    if cv2.waitKey(1) and 0xFF == ord('q'):
     break
George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • This program shows me 'error retrieving frame' so it wasn't succesfully. The code is wrong or something else ? For example the version of Python or i have to add something on Python Interpreter. Thank you for your time! – six dogs Jul 30 '21 at 17:39
  • How many cameras do you have on your system ? Currently you're attempting to open the one at index 1, implying there are at least two cameras setup ? what do you get if you `print(cap.isOpened())` ? What kind of camera is it ? – George Profenza Jul 30 '21 at 21:16
  • I have a camera which is Logitech C920 USB HD Pro Webcam with Auto-Focus and Microphone Black. I get False with print(cap.isOpened()) , so camera is not turned on ? What can i do to open the camera using OpenCV with Python. – six dogs Jul 31 '21 at 09:40
  • Ok, it looks like a bigger issue: OpenCV can't open your webcam. Normally it should: I've used this exact model many times in the past using OpenCV (both C++ and Python). Is this the only webcam on your system ? Have you tried `cap = cv2.VideoCapture(0)` ? What operating system are you using it on and how did you install OpenCV Python ? – George Profenza Jul 31 '21 at 22:33