0

I have been working on this image classification(watermark detection) assignment I am trying to load a folder of images

from keras.preprocessing.image import load_img
import cv2 
import matplotlib.pyplot as plt
DATADIR = 'F:\IMP.DATA\Task\Watermark_test_data'
CATEGORIES=['Watermark','No Watermark']

for category in CATEGORIES:
        path= os.path.join(DATADIR,category)#path to test folder    
        for img in os.listdir(path):
            img_array = cv2.imread(os.path.dirname(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
            plt.imshow(img_array,cmap="gray")
            plt.show(img_array)
            break
        break

img = load_img('F:/IMP.DATA/Task/Watermark_test_data/Watermark/1.jpg')
print(type(img))
print(img.format)
print(img.mode)
print(img.size)
img.show()
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense,Dropout,Flatten
from keras.layers.convolutional import Conv2D,MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
import os 
import cv2

DATADIR = 'F:\IMP.DATA\Task\Watermark_test_data'
CATEGORIES=['Watermark','No Watermark']

for category in CATEGORIES:
    path= os.path.join(DATADIR,category)#path to test folder    
    for img in os.listdir(path):
        img_array = cv2.imread(os.path.dirname(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
        plt.imshow(img_array,cmap="gray")
        plt.show(img_array)
        break
    break

I have been using cv2 and load_img to load the image but in both the cases I get error in matplotlib plt.imshow (function) This is the error that I get

File "<ipython-input-51-2b07cb64d5a1>", line 11
    plt.imshow(img_array,cmap="gray")
      ^
SyntaxError: invalid syntax

I can't see anything wrong in the syntax

1 Answers1

0

The SyntaxError is caused by a missing closing parenthesis in this line:

img_array = cv2.imread(os.path.dirname(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)

Add ) after os.path.dirname(os.path.join(path,img) to solve it.

Jonathan Feenstra
  • 2,534
  • 1
  • 15
  • 22
  • That worked fine but now I'm getting an that `TypeError: Image data of dtype object cannot be converted to float` on `plt.imshow(img_array, cmap="gray")` so I'm assuming that `img_array = cv2.imread(os.path.dirname(os.path.join(path,img)), cv2.IMREAD_GRAYSCALE)` is not able to read the file – Pratyush Jain Dec 03 '19 at 14:29
  • @PratyushJain that would also be my guess. Make sure `os.path.dirname(os.path.join(path,img))` is returning the correct path. – Jonathan Feenstra Dec 03 '19 at 14:39
  • `os.access(r'F:\IMP.DATA\Task\Watermark_test_data\1.jpg',os.R_OK)` returns `False` And I'm also getting this error `PermissionError: [Errno 13] Permission denied: 'F:/IMP.DATA/Task/Watermark_test_data/Watermark'` which I tried solving from this thread `https://stackoverflow.com/questions/36434764/permissionerror-errno-13-permission-denied` But I still get the same error ,I also get `OSError: [WinError 123]` With some other methods I also tried – Pratyush Jain Dec 04 '19 at 06:46
  • @PratyushJain is that the path returned by `os.path.dirname(os.path.join(path,img))`? Have you tested with `os.F_OK` instead of `os.R_OK` to see if the file even exists? If it does and you still get the error, you can post a separate question so other people can help you (this post was about the `SyntaxError` which seems to be resolved now, so consider [accepting my answer](https://stackoverflow.com/help/accepted-answer)). – Jonathan Feenstra Dec 04 '19 at 10:11
  • 1
    Sorry ,I'm new to the website.Thank you so much ,I will post a new question – Pratyush Jain Dec 04 '19 at 12:16