12

I am trying to read an image in opencv python

import cv2
import numpy as np

# Read images
image=cv2.imread(cv2.samples.findFile("lena.jpg"))
cv2.imshow("image",image)
cv2.waitKey(0)

and it gives the following error

[ WARN:0] global C:\projects\opencv-python\opencv\modules\core\src\utils\samples.cpp (59) cv::samples::findFile cv::samples::findFile('lena.jpg') => ''
Traceback (most recent call last):
  File "D:/all_libraries/main.py", line 5, in <module>
    image=cv2.imread(cv2.samples.findFile("lena.jpg"))
cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\core\src\utils\samples.cpp:62: error: (-2:Unspecified error) OpenCV samples: Can't find required data file: lena.jpg in function 'cv::samples::findFile'

where as the C++ version of this does not give any error


#include <iostream>

#include <opencv2/core.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/highgui.hpp>

using namespace std;
using namespace cv;
using namespace cv::ml;
int main()
{

    Mat image;
    image=imread(samples::findFile("lena.jpg"));
    imshow("lena.jpg",image);
    waitKey(0);

    return 0;
}

I have installed OpenCV 4.1.1 in pycharm c++ version is also 4.1.1 operating system Windows

Gourav Roy
  • 163
  • 1
  • 2
  • 5
  • 1
    it seems Python doesn't know where is this image. Maybe it doesn't have it on disk. You may download from GitHub: https://github.com/opencv/opencv/tree/master/samples/data – furas Dec 15 '19 at 07:08
  • where in the pycharm virtual environment should I put this data file – Gourav Roy Dec 15 '19 at 09:05
  • 3
    you can put it in any place and then you can use `cv2.imread("/path/to/image/lena.jpg")` and you don't need `cv2.samples.findFile()` – furas Dec 15 '19 at 09:30

4 Answers4

8

I had the same problem running the first OpenCV tutorial, the following worked for me. I am using PyCharm Community and python. addSamplesDataSearchSubDirectory did not work.

cv.samples.addSamplesDataSearchPath("C:\\......\\opencv\\sources\\samples\\data")
img = cv.imread(cv.samples.findFile("starry_night.jpg"))
Roo
  • 141
  • 2
  • 6
  • The problem I encountered was that neither `pip install opencv-python` nor `pip install opencv-contrib-python` actually installed the sample images. I had to `git clone https://github.com/opencv/opencv` to get the sample files locally and then this solution worked by specifying the full path to the samples directory in my GitHub clone. – DocOc Jun 02 '23 at 23:03
3

In Python, the findFile function uses an environment variable to define the search path. When you installed OpenCV, you would have a created a folder with a name similar to C:\something\samples\data (I'm guessing a little bit here because I use Linux not Windows). So you need to set the environment variable OPENCV_SAMPLES_DATA_PATH to the value C:\something\samples\data and then try running your code.

There's a little bit of relevant documentation (but not really enough to be useful) at https://docs.opencv.org/3.4/d6/dba/group__core__utils__samples.html

Alexander Hanysz
  • 791
  • 5
  • 15
0

instead of image=cv2.imread(cv2.samples.findFile("lena.jpg")) try using image=cv2.imread(args["image1"]) i.e you are giving file path as argument. Here is the full code that should work for your problem:

import cv2
import numpy as np
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to the image")
args = vars(ap.parse_args())

# Read images
image=cv2.imread(args["image"])
cv2.imshow("image",image)
cv2.waitKey(0)

now try running your code by the following command:
python filename.py -i "./lena.jpg"

nirojshrestha019
  • 2,068
  • 1
  • 10
  • 14
0

Try this!

  1. go to environment variable create a new parameter "OPENCV_SAMPLES_DATA_PATH"
  2. add the location "...\sample\data" to your value
  3. restart your IDE
  4. you can run the sample data automatically.
mosc9575
  • 5,618
  • 2
  • 9
  • 32
DC. Cheng
  • 13
  • 4