V
V
vlad17532021-01-13 17:54:38
Python
vlad1753, 2021-01-13 17:54:38

How to correctly specify the path to the file?

There is a function:

def handle(path, save):
    img = cv2.imread(r'C:\progs\ocr3\gdz\form2\ukrainian\ГДЗ Українська мова 2 клас.Н.В Гавриш, Т.С. Маркотенко\numbers\1.jpg')

    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

    thresh = 127   
    im_b = cv2.threshold(img_gray, thresh, 255, cv2.THRESH_BINARY)[1]

    contours, hierarchy = cv2.findContours(image = im_b, mode = cv2.RETR_TREE, method = cv2.CHAIN_APPROX_SIMPLE)

    contours = sorted(contours, key = cv2.contourArea, reverse= True)

    mask = np.ones(img.shape[:2], np.uint8)
    mask.fill(255)
    cv2.drawContours(mask, contours, contourIdx =0 , color =0, thickness = -1)
    new_img = cv2.add(im_b, mask)
    cv2.imwrite(save,new_img)


It removes the background from an image. The problem is that I can't open this image...
Here is this line of code:
img = cv2.imread(r'C:\progs\ocr3\gdz\form2\ukrainian\ГДЗ Українська мова 2 клас.Н.В Гавриш, Т.С. Маркотенко\numbers\1.jpg')

If you change the file name:
img = cv2.imread(r'C:\progs\ocr3\gdz\form2\ukrainian\1\numbers\1.jpg')

Everything opens up just fine.

Error:

Traceback (most recent call last):
File "c:\progs\ocr3\main.py", line 30, in
handle('images/p4ex1.jpg', 'masked/mas112312323.jpg')
File "c: \progs\ocr3\main.py", line 10, in handle
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\ 1\pip-req-build-wvn_it83\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' Help


would be appreciated .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-01-13
@galaxy

It does not know how to open files with non-ASCII names under Windows, forget it. Open with python. Internet example:

f = open(path, "rb")
chunk = f.read()
chunk_arr = np.frombuffer(chunk, dtype=np.uint8)
img = cv2.imdecode(chunk_arr, cv2.IMREAD_COLOR)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question