Click here to Skip to main content
15,918,303 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
import cv2
import numpy as np
from datetime import datetime
import os

from PIL import Image
import hashlib, os, math, time
import Image
#from PIL import Image
import ImageEnhance
from pytesser import *
from urllib import urlretrieve
import math
import random

iconset = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

responses =[]

for letter in iconset:
        for img in os.listdir('iconset/%s/'%(letter)):              
#for j in range(1,10):
        #for i in range(1,10):
                #n= str(j)+str(i)+'.jpg'
                img = cv2.imread(img)
                im3 = img.copy()
                height, width, depth = im3.shape
                gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
                blur = cv2.GaussianBlur(gray,(5,5),0)
                thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)
                roi = thresh[0:height, 0:width]
                roismall = cv2.resize(roi,(10,10))
                sample = roismall.reshape((1,100))
                samples = np.append(samples,sample,0)
print "training complete"
[no name] 14-Jun-14 13:28pm

1 solution

Are you sure each directory is populated?

If they are not, you can fix your code by checking that img exists/ is not none and breaking the loop if it is none.

img = cv2.imread(img)
im3 = img.copy()

img = cv2.imread(img)
if img is None:
im3 = img.copy()

The break will kill the inner loop but not the outer loop, allowing you to go to the next directory.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900