Based on the error message it seems OpenCV is failing to convert a grayscale image to “gray” in:
x = cv2.cvtColor(np.float32(x), cv2.COLOR_BGR2GRAY)

since the input image already contains a single input channel.
You might need to load all images using 3 channels and convert them to gray afterwards in case you are working with a mixed dataset (or just skip the already gray images).