0

I have been attempting to produce an OCR tool following this tutorial on youtube, and using the following script:

import os
import sys

import cv2
import numpy as np

input_f = 'letter.data'

img_resize_factor = 12
start, end = 6, -1
height, width = 16, 8

with open(input_f, 'r') as f:
    for line in f.readlines():
        data = np.array([255*float(x) for x in line.split('\t')[start:end]])
        img = np.reshape(data, (height, width))
        img_scaled = cv2.resize(img, None, fx=img_resize_factor, fy=img_resize_factor)
        print(line)
        cv2.imshow('img', img_scaled)

        c = cv2.waitKey()
        if c == 27:
            break

The code falls over when attempting to use cv2.imshow('img', img_scaled) the window appears however is non responding and the image is not loaded into it.

I am using the most up to date version of OpenCV, I am running this in VisualStudio, and have had to add "python.linting.pylintArgs": ["--extension-pkg-whitelist=cv2"] to the user settings.

The error I get is:

Exception has occurred: cv2.error OpenCV(4.0.0) c:\projects\opencv-python\opencv\modules\imgproc\src\color.hpp:261: error: (-2:Unspecified error) in function '__cdecl cv::CvtHelper,struct cv::Set<3,4,-1>,struct cv::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)' > Unsupported depth of input image: >
'VDepth::contains(depth)' > where > 'depth' is 6 (CV_64F) File "C:\Users\aofarrell\Desktop\Python\NeuralNetworks\SimpleOCR.py", line 23, in break

Ahmad Khan
  • 2,655
  • 19
  • 25
TisButaScratch
  • 163
  • 1
  • 5
  • 17
  • 1
    @Miki Didn't you misread that? To me it looks more like single channel, based on the code that creates it, but with datatype of 64bit floats (since OP never specifies a datatype when creating the array). (Also the message explicitly mentions `CV_64F`... not sure why it would say that if this was a channel count.) – Dan Mašek Jan 31 '19 at 13:50
  • 1
    Since you do not specify datatype when creating the numpy array, it gets created as `np.float64`. OpenCV doesn't seem to like that. Pick an appropriate datatype that OpenCV supports (maybe `np.uint8` will be enough here, or possibly `np.float32`), and either specify that when creating the array, or add a cast. – Dan Mašek Jan 31 '19 at 13:55

1 Answers1

0

Everything in your script is wrong.

Solution

1) If you are opening a file, open just file, get data and get out of with statement

2) The error you are experiencing is due to wrong shape

I opened the file and extracted images

import os
import sys
import cv2
import numpy as np

input_f = 'letter.data'
start, end = 6, -1



def file_opener(input_f):
    with open(input_f,'r') as fl:
        for line in fl.readlines():
            yield np.array([255*float(x) for x in line.split('\t')[start:-1]])

iterator = file_opener(input_f)
images = np.array([row for row in iterator]).reshape(52152,16,8)  # array with 52152 images


for image_index in range(images.shape[0]):
    IMAGE = cv2.resize(images[image_index,:],(0,0),fx=5,fy=5)
    cv2.imshow('image {}/{}'.format(image_index,images.shape[0]),IMAGE)
    cv2.waitKey(0)
cv2.destroyAllWindows(0)

enter image description here

Martin
  • 3,333
  • 2
  • 18
  • 39