Example #1
0
    def build_ground_truth_from_mask(self, img_id):
        fn = 'build_ground_truth_from_mask'

        #self.mylog(fn,"Generating ground truth...")

        img_path = self.imgdir + img_id
        myimg1 = None
        #self.mylog(fn,"processing img_id[%s] img_path[%s]..." % (img_id,img_path))

        if os.path.exists(img_path):
            myimg1 = myimg.myImg(imageid='t1',
                                 config=self.myImg_config,
                                 path=img_path)
        else:
            print("Image file [%s] does not exists..." % (img_path))
            sys.exit(-1)

        image_org = myimg1.getImage()
        masked_img = copy.copy(image_org)
        #print("**********",image_org.shape,type(image_org.shape),type(image_org))
        #self.mylog(fn,"org image shape [%d %d %d]" % (image_org.shape))
        truth_img = np.zeros(image_org.shape,
                             dtype='uint8')  #create 0's based truth
        #self.mylog(fn,"truth image shape [%d %d %d]" % (truth_img.shape))

        img_mask_df = self.get_mask_for_image_id(img_id=img_id)
        #loop image mask df loop
        cnt = 0  #processing counter
        _img_sum = 0
        for i, rec in img_mask_df.iterrows():
            #if cnt > 5:
            #  break
            #self.mylog(fn,"%d %s %d %d %d %d %s" % (cnt,rec.img_id, rec.x, rec.y, rec.w, rec.h, rec.mask_label))
            #truth_img[rec.x:(rec.x+rec.w),rec.y:(rec.y+rec.h),:] = self.truth_pixel
            truth_img[rec.y:(rec.y + rec.h),
                      rec.x:(rec.x + rec.w), :] = self.truth_pixel
            #self.mylog(fn,"%d %s patch sum %.1f image sum %.1f accu sum %.1f " %
            #                     (cnt,rec.img_id, rec.w*rec.h, truth_img.sum(),_img_sum))
            masked_img = cv2.rectangle(masked_img, (rec.x, rec.y),
                                       (rec.x + rec.w, rec.y + rec.h),
                                       (255, 0, 0), 1)

            cnt += 1
            _img_sum += rec.w * rec.h * truth_img.shape[-1]

        #myimg1.padImage( _w, _h)  #trim truth as per reduced size

        #return patch of DF for given img_id
        return image_org, truth_img, masked_img
Example #2
0
    def get_image_and_ground_truth(self, img_id):
        fn = 'get_image_and_ground_truth'

        #self.mylog(fn,"Generating ground truth...")

        img_path = self.imgdir + 'images/' + img_id + '.jpg'
        mask_path = self.imgdir + 'gt/' + img_id + '_HE.jpg'
        myimg1 = None
        #self.mylog(fn,"processing img_id[%s] img_path[%s]..." % (img_id,img_path))

        if os.path.exists(img_path):
            myimg1 = myimg.myImg(imageid=img_id,
                                 config=self.myImg_config,
                                 path=img_path)
        else:
            print("Image file [%s] does not exists..." % (img_path))
            sys.exit(-1)

        if os.path.exists(mask_path):
            myimg2 = myimg.myImg(imageid=img_id,
                                 config=self.myImg_config,
                                 path=mask_path)
        else:
            print("Image mask file [%s] does not exists..." % (mask_path))
            sys.exit(-1)

        image_org = myimg1.getImage()
        #print("**********",image_org.shape,type(image_org.shape),type(image_org))
        #self.mylog(fn,"org image shape [%d %d %d]" % (image_org.shape))

        #initialize truth image data
        truth_img = myimg2.getImage()
        #self.mylog(fn,"truth image shape [%d %d %d]" % (truth_img.shape))

        #return patch of DF for given img_id
        return image_org, truth_img
Example #3
0
    def image_generator(self):
        #initialize all variables...
        mname = 'image_data_generator'

        fd = open(self.train_data_dir + self.data_file + '_df.csv', 'r')

        while True:
            n_img_w = self.img_width
            n_img_h = self.img_heigth
            channels = self.channels

            #x_train = np.zeros(( tot_cnt, n_img_w, n_img_h, 3), dtype='uint8')
            #x_img_buf = np.empty(( n_img_w, n_img_h), dtype='uint8')
            x_buf = np.zeros((n_img_w, n_img_h, channels), dtype='uint8')
            y_buf = 0

            #loop in through dataframe.
            #self.log( mname, "[{}] recs for set.".format(img_cnt), level=3)

            cnt = 0
            file_missing = 0
            #while cnt < img_cnt:
            #if cnt >= tot_cnt:
            #  break

            line = fd.readline()
            if line == "":
                fd.seek(0)
                line = fd.readline()
            line = line.strip().split(',')
            image_id = line[0]
            label = line[1]

            imgpath = self.img_dir_path + image_id + self.img_filename_ext

            if os.path.exists(imgpath):
                myimg1 = myimg.myImg(imageid=image_id,
                                     config=self.myImg_config,
                                     path=imgpath,
                                     channels=channels)
                #myimg1.getGreyScaleImage2(convertFlag=True)
                #self.channels = 1
                #myimg1.padImage(n_img_w,n_img_h)

                #x_img_buf[ 0, :, :] = myimg1.getImage()
                if self.channels == 1:
                    x_buf[:, :, 0] = myimg1.getImage()
                else:
                    x_buf[:, :, :] = myimg1.getImage()

                #y_labels.append(label)

                #self.log( mname, "Image file [{}] doesn't exists!!!".format(imgpath), level=2)
                cnt += 1
                self.processing_cnt += 1
            else:
                print(mname,
                      "****Image file [{}] doesn't exists!!!".format(imgpath))
                self.log(mname,
                         "Image file [{}] doesn't exists!!!".format(imgpath),
                         level=2)
                file_missing += 1

            #create y array as required
            #y_buf = np.array( label, dtype='uint8')
            #y_buf = y_buf.astype('float32')
            #y_buf = np.reshape( y_buf, (1))
            y_buf = keras.utils.to_categorical(label, self.no_classes)
            #y_buf = np.reshape( y_buf, (self.no_classes,1))
            #print("XXXXX",image_id,label,y_buf,y_buf.shape)

            #x_buf = x_buf.astype('float32') / 255
            x_buf = x_buf.astype('float32')
            x_buf /= 255.0
            x_buf -= np.min(x_buf)
            x_buf /= np.max(x_buf)

            # Crop the central [height, width] of the image.
            #x_buf = tf.cast( x_buf, tf.float32)
            #x_buf = tf.image.resize_image_with_crop_or_pad(x_buf,n_img_h,n_img_w)

            # Subtract off the mean and divide by the variance of the pixels.
            #x_buf = tf.image.per_image_standardization(x_buf)

            # Set the shapes of tensors.
            #x_buf.set_shape([n_img_w, n_img_h, channels])
            ''' 
      m = re.findall('(^\d+)_(.*?)$',image_id)
      _id = tf.cast(m[0][0],tf.int64)
      '''

            yield (image_id, x_buf, y_buf)
Example #4
0
    def get_test_data(self):
        #initialize all variables...
        mname = 'get_test_data'
        n_img_w = self.img_width
        n_img_h = self.img_heigth

        #x_train = np.zeros(( tot_cnt, n_img_w, n_img_h, 3), dtype='uint8')
        x_img_buf = np.empty((1, n_img_w, n_img_h), dtype='uint8')
        x_test = None
        y_test = None
        y_test_buf = []

        test_cnt = self.test_df.level.count()
        if self.channels == 1:
            x_test = np.zeros((test_cnt, n_img_w, n_img_h), dtype='uint8')
        else:
            x_test = np.zeros((test_cnt, n_img_w, n_img_h, self.channels),
                              dtype='uint8')

        y_test = np.zeros((0, 1), dtype='uint8')

        #loop in through dataframe.
        self.log(mname, "[{}] recs for testing.".format(test_cnt), level=3)

        cnt = 0
        file_missing = 0
        for i, rec in self.test_df.iterrows():
            #if cnt >= tot_cnt:
            #  break

            progress_sts = "%6d out of %6d" % (cnt, test_cnt)
            sys.stdout.write(progress_sts)
            sys.stdout.write(
                "\b" * len(progress_sts))  # return to start of line, after '['
            sys.stdout.flush()

            imgpath = self.img_dir_path + rec.image + self.img_filename_ext
            self.test_df.loc[i, 'imgpath'] = imgpath

            if os.path.exists(imgpath):
                myimg1 = myimg.myImg(imageid=rec.image,
                                     config=self.myImg_config,
                                     path=imgpath)

                #x_img_buf[ 0, :, :] = myimg1.getImage()
                if self.channels == 1:
                    x_test[test_cnt, :, :] = myimg1.getImage()
                else:
                    x_test[test_cnt, :, :, :] = myimg1.getImage()

                y_test_buf.append(rec.level)
                #x_test = np.vstack( (x_test, x_img_buf))
                #self.log( mname, "[{}] [{}] x_test[{}] x_img_buf[{}]".format(cnt,test_cnt,x_test.shape,x_img_buf.shape), level=2)

                #self.log( mname, "Image file [{}] doesn't exists!!!".format(imgpath), level=2)
            else:
                #self.log( mname, "Image file [{}] doesn't exists!!!".format(imgpath), level=2)
                file_missing += 1

            self.processing_cnt += 1
            cnt += 1

        #create y array as required
        y_test = np.array(y_test_buf, dtype='uint8')
        y_test = np.reshape(y_test, (y_test.size, 1))
        #print final dimensionf or x_train and y_train
        self.log(mname,
                 "x_test [{}] y_test [{}]".format(x_test.shape, y_test.shape),
                 level=3)
        #print( mname, "####x_test [{}] y_test [{}] y_buf[{}]".format(x_test.shape,y_test.shape,len(y_test_buf)))

        self.log(mname, "Process dataset [{}]".format(cnt), level=3)
        self.log(mname, "File missing [{}]".format(file_missing), level=3)
        self.log(mname,
                 "Max image width[{}] heigth[{}]".format(
                     self.df['w'].max(), self.df['h'].max()),
                 level=3)
        #print(self.df.head(10))

        return (x_test, y_test)
Example #5
0
    def image_data_generator(self, mode="train"):
        #initialize all variables...
        mname = 'image_data_generator'

        fd = open(self.train_data_dir + mode + '_df.csv', 'r')

        while True:
            n_img_w = self.img_width
            n_img_h = self.img_heigth

            #x_train = np.zeros(( tot_cnt, n_img_w, n_img_h, 3), dtype='uint8')
            x_img_buf = np.empty((1, n_img_w, n_img_h), dtype='uint8')
            x_buf = None
            y_buf = None
            y_labels = []

            img_cnt = self.batch_size
            '''
      if self.channels == 1:
        x_buf = np.zeros((img_cnt, n_img_w, n_img_h), dtype='uint8')
      else:
        x_buf = np.zeros((img_cnt, n_img_w, n_img_h, self.channels), dtype='uint8')
      '''
            x_buf = np.zeros((img_cnt, n_img_w, n_img_h, self.channels),
                             dtype='uint8')

            y_buf = np.zeros((0, 1), dtype='uint8')

            #loop in through dataframe.
            #self.log( mname, "[{}] recs for set.".format(img_cnt), level=3)

            cnt = 0
            file_missing = 0
            while cnt < img_cnt:
                #if cnt >= tot_cnt:
                #  break

                line = fd.readline()
                if line == "":
                    fd.seek(0)
                    line = fd.readline()
                line = line.strip().split(',')
                image_id = line[0]
                label = line[1]

                imgpath = self.img_dir_path + image_id + self.img_filename_ext
                '''       
        progress_sts = "[%.3fG] [%6d] %6d out of %6d [%45s]" % (self.proc.memory_full_info()[1]/(1024**3),self.processing_cnt,cnt,img_cnt,imgpath)
        sys.stdout.write(progress_sts)
        sys.stdout.write("\b" * len(progress_sts)) # return to start of line, after '['
        sys.stdout.flush()
        '''

                if os.path.exists(imgpath):
                    myimg1 = myimg.myImg(imageid=image_id,
                                         config=self.myImg_config,
                                         path=imgpath)
                    #myimg1.getGreyScaleImage2(convertFlag=True)
                    #self.channels = 1
                    #myimg1.padImage(n_img_w,n_img_h)

                    #x_img_buf[ 0, :, :] = myimg1.getImage()
                    if self.channels == 1:
                        x_buf[cnt, :, :, 0] = myimg1.getImage()
                    else:
                        x_buf[cnt, :, :, :] = myimg1.getImage()

                    y_labels.append(label)
                    #x_test = np.vstack( (x_test, x_img_buf))
                    #self.log( mname, "[{}] [{}] x_test[{}] x_img_buf[{}]".format(cnt,test_cnt,x_test.shape,x_img_buf.shape), level=2)

                    #self.log( mname, "Image file [{}] doesn't exists!!!".format(imgpath), level=2)
                    cnt += 1
                    self.processing_cnt += 1
                else:
                    print(
                        mname, "****Image file [{}] doesn't exists!!!".format(
                            imgpath))
                    self.log(
                        mname,
                        "Image file [{}] doesn't exists!!!".format(imgpath),
                        level=2)
                    file_missing += 1

            #create y array as required
            y_buf = np.array(y_labels, dtype='uint8')
            y_buf = np.reshape(y_buf, (y_buf.size, 1))
            #print final dimensionf or x_train and y_train
            #self.log( mname, "x_buf [{}] y_buf [{}]".format(x_buf.shape,y_buf.shape), level=3)
            #print( mname, "####x_test [{}] y_test [{}] y_buf[{}]".format(x_test.shape,y_test.shape,len(y_test_buf)))

            #self.log( mname, "Process dataset [{}]".format(cnt), level=3)
            #self.log( mname, "File missing [{}]".format(file_missing), level=3)
            #self.log( mname, "Max image width[{}] heigth[{}]".format(self.df['w'].max(),self.df['h'].max()), level=3)
            #print(self.df.head(10))

            # Normalize data.
            x_buf = x_buf.astype('float32') / 255

            # Convert class vectors to binary class matrices.
            #y_buf = keras.utils.to_categorical(y_buf, self.no_classes)
            y_buf = y_buf.astype('uint8')

            yield (x_buf, y_buf)
Example #6
0
    def load_img_data(self):
        mname = "load_greyscale_data"

        self.log(mname,
                 "Loading Dataframe from [{}]".format(
                     self.train_label_data_file),
                 level=3)
        self.df = pd.read_csv(self.train_label_data_file)
        self.log(mname,
                 "Loaded [{}] recs".format(self.df['level'].count()),
                 level=3)

        #create & set all myImg Config
        self.myImg_config = cutil.Config(configid="myConfId", cdir=self.cdir)
        self.myImg_config.setDdir(self.train_data_dir)
        self.myImg_config.setOdir(self.img_croped_dir_path)
        self.myImg_config.setIdir(self.img_dir_path)

        self.df['h'] = 0
        self.df['w'] = 0
        self.df['imgpath'] = ""
        self.df['imgexists'] = False

        #initialize all variables...
        n_img_w = self.img_width
        n_img_h = self.img_heigth

        tot_cnt = self.img_processing_capacity
        if tot_cnt == 0:
            tot_cnt = self.df['level'].count()
        cnt = 0
        file_missing = 0

        #generate dataset for handling train : test
        np.random.seed(self.random_seed)
        train_dataset_sample = np.random.choice(
            range(0, tot_cnt),
            int(tot_cnt * self.training_dataset_ratio),
            replace=False)
        train_dataset_indicies = dict(
            zip(train_dataset_sample, train_dataset_sample))

        #x_train = np.zeros(( tot_cnt, n_img_w, n_img_h, 3), dtype='uint8')
        x_img_buf = np.empty((1, n_img_w, n_img_h), dtype='uint8')
        x_train = None
        y_train = None
        x_test = None
        y_test = None
        y_train_buf = []
        y_test_buf = []

        if self.channels == 1:
            x_train = np.zeros((len(train_dataset_sample), n_img_w, n_img_h),
                               dtype='uint8')
            x_test = np.zeros(
                ((tot_cnt - len(train_dataset_sample)), n_img_w, n_img_h),
                dtype='uint8')
        else:
            x_train = np.zeros(
                (len(train_dataset_sample), n_img_w, n_img_h, self.channels),
                dtype='uint8')
            x_test = np.zeros(((tot_cnt - len(train_dataset_sample)), n_img_w,
                               n_img_h, self.channels),
                              dtype='uint8')

        y_train = np.zeros((0, 1), dtype='uint8')
        y_test = np.zeros((0, 1), dtype='uint8')

        #loop in through dataframe.
        train_cnt = 0
        test_cnt = 0
        train_samples_cnt = len(train_dataset_sample)
        test_samples_cnt = tot_cnt - len(train_dataset_sample)
        self.log(mname,
                 "[{}] recs for training.".format(train_samples_cnt),
                 level=3)
        self.log(mname,
                 "[{}] recs for test.".format(test_samples_cnt),
                 level=3)

        for i, rec in self.df.iterrows():
            if cnt >= tot_cnt:
                break

            progress_sts = "%6d out of %6d" % (cnt, tot_cnt)
            sys.stdout.write(progress_sts)
            sys.stdout.write(
                "\b" * len(progress_sts))  # return to start of line, after '['
            sys.stdout.flush()

            imgpath = self.img_dir_path + rec.image + self.img_filename_ext
            self.df.loc[i, 'imgpath'] = imgpath

            if os.path.exists(imgpath):
                myimg1 = myimg.myImg(imageid=rec.image,
                                     config=self.myImg_config,
                                     path=imgpath)

                #x_img_buf[ 0, :, :] = myimg1.getImage()
                if train_dataset_indicies.get(cnt, False):
                    #x_train = np.vstack( (x_train, x_img_buf))
                    if train_cnt < train_samples_cnt:
                        if self.channels == 1:
                            x_train[train_cnt, :, :] = myimg1.getImage()
                        else:
                            x_train[train_cnt, :, :, :] = myimg1.getImage()
                    y_train_buf.append(rec.level)
                    train_cnt += 1
                else:
                    #x_test = np.vstack( (x_test, x_img_buf))
                    #self.log( mname, "[{}] [{}] x_test[{}] x_img_buf[{}]".format(cnt,test_cnt,x_test.shape,x_img_buf.shape), level=2)
                    if test_cnt < test_samples_cnt:
                        if self.channels == 1:
                            x_test[test_cnt, :, :] = myimg1.getImage()
                        else:
                            x_test[test_cnt, :, :, :] = myimg1.getImage()
                    y_test_buf.append(rec.level)
                    test_cnt += 1

                #self.log( mname, "Image file [{}] doesn't exists!!!".format(imgpath), level=2)
            else:
                #self.log( mname, "Image file [{}] doesn't exists!!!".format(imgpath), level=2)
                file_missing += 1

            cnt += 1

        #create y array as required
        y_train = np.array(y_train_buf, dtype='uint8')
        y_train = np.reshape(y_train, (y_train.size, 1))
        y_test = np.array(y_test_buf, dtype='uint8')
        y_test = np.reshape(y_test, (y_test.size, 1))
        #print final dimensionf or x_train and y_train
        self.log(mname,
                 "x_train [{}] y_train [{}]".format(x_train.shape,
                                                    y_train.shape),
                 level=3)
        self.log(mname,
                 "x_test [{}] y_test [{}]".format(x_test.shape, y_test.shape),
                 level=3)

        self.log(mname, "Process dataset [{}]".format(cnt), level=3)
        self.log(mname, "File missing [{}]".format(file_missing), level=3)
        self.log(mname,
                 "Max image width[{}] heigth[{}]".format(
                     self.df['w'].max(), self.df['h'].max()),
                 level=3)
        #print(self.df.head(10))
        #self.df.to_csv( self.train_data_dir + 'u_img_set.csv')

        return (x_train, y_train), (x_test, y_test)
Example #7
0
    def preprocess_images(self,
                          convert_to_greyscale=True,
                          from_index=0,
                          batch_size=10):
        mname = "preprocess_images"

        self.log(mname,
                 "Loading Dataframe from [%s] from_index[%d] batch_size[%d]" %
                 (self.train_label_data_file, from_index, batch_size),
                 level=3)
        print(
            mname,
            "Loading Dataframe from [%s] from_index[%d] batch_size[%d]" %
            (self.train_label_data_file, from_index, batch_size))
        self.df = pd.read_csv(self.train_label_data_file)
        self.log(mname,
                 "Loaded [{}] recs".format(self.df['level'].count()),
                 level=3)

        #create & set all myImg Config
        self.myImg_config = cutil.Config(configid="myConfId", cdir=self.cdir)
        self.myImg_config.setDdir(self.train_data_dir)
        self.myImg_config.setOdir(self.img_croped_dir_path)
        self.myImg_config.setIdir(self.img_dir_path)

        self.df['h'] = 0
        self.df['w'] = 0
        self.df['imgpath'] = ""
        self.df['imgexists'] = False

        #initialize all variables...
        n_img_w = self.img_width
        n_img_h = self.img_heigth

        tot_cnt = self.df['level'].count()
        cnt = 0
        file_missing = 0

        #loop in through dataframe.
        for i, rec in self.df.iterrows():
            if cnt < from_index:
                progress_sts = "Skipping %6d" % (cnt)
                sys.stdout.write(progress_sts)
                sys.stdout.write(
                    "\b" *
                    len(progress_sts))  # return to start of line, after '['
                sys.stdout.flush()
                cnt += 1

                continue
            else:
                if cnt >= (from_index + batch_size):
                    break

            progress_sts = "%6d out of %6d" % (cnt, tot_cnt)
            #sys.stdout.write("%6d out of %6d" % (cnt,tot_cnt))
            sys.stdout.write(progress_sts)
            sys.stdout.write(
                "\b" * len(progress_sts))  # return to start of line, after '['
            sys.stdout.flush()

            imgpath = self.img_dir_path + rec.image + '_oi' + self.img_filename_ext
            self.df.loc[i, 'imgpath'] = imgpath

            #skip already processed data
            if os.path.exists(self.img_croped_dir_path + rec.image +
                              self.img_filename_ext):
                cnt += 1
                continue

            if os.path.exists(imgpath):
                myimg1 = myimg.myImg(imageid=rec.image,
                                     config=self.myImg_config,
                                     path=imgpath)

                #check if image needs to be converted to greyscale.
                if convert_to_greyscale:
                    #myimg1.getGreyScaleImage2(convertFlag=True)
                    myimg1.getImageFromSpecificChannel(channel=2,
                                                       convertFlag=True)

                #curtail image to specific frame size.
                myimg1.padImage(n_img_w, n_img_h)

                #x_img_buf[ 0, :, :] = myimg1.getImage()
                #Save the transformed image.
                myimg1.saveImage(img_type_ext='.jpeg', gen_new_filename=True)

                #self.log( mname, "Croped Image [{}] [{}] [{}] [{}]".format(myimg1.getImage().shape,croped_img_arr.shape,x_train.shape,x_img_buf.shape), level=4)

                #x_train = np.vstack( (x_train, x_img_buf))
                #x_train[cnt,:,:,:] = croped_img_arr
                #y_buf.append(rec.level)

                self.df.loc[i, 'imgexists'] = True
                self.df.loc[i, 'w'], self.df.loc[i, 'h'] = myimg1.getImageDim()
                #self.df.loc[i,'_w'], self.df.loc[i,'_h'] = croped_img_arr.shape[0],croped_img_arr.shape[1]
                #self.log( mname, "Image file [{}] doesn't exists!!!".format(imgpath), level=2)
            else:
                #self.log( mname, "Image file [{}] doesn't exists!!!".format(imgpath), level=2)
                file_missing += 1

            cnt += 1
        ''' 
    #create y array as required
    y_train = np.array( y_buf, dtype='uint8')
    y_train = np.reshape( y_train, (y_train.size,1))
    #print final dimensionf or x_train and y_train
    self.log( mname, "x_train [{}] y_train [{}]".format(x_train.shape,y_train.shape), level=3)
    '''

        self.log(mname, "Process dataset [{}]".format(cnt), level=3)
        self.log(mname, "File missing [{}]".format(file_missing), level=3)
        self.log(mname,
                 "Max image width[{}] heigth[{}]".format(
                     self.df['w'].max(), self.df['h'].max()),
                 level=3)
        #print(self.df.head(10))
        self.df.to_csv(self.train_data_dir + 'u_img_set.csv')
Example #8
0
    def load_train_data(self):
        mname = "load_train_data"

        self.log(mname,
                 "Loading Dataframe from [{}]".format(
                     self.train_label_data_file),
                 level=3)
        self.df = pd.read_csv(self.train_label_data_file)

        #create & set all myImg Config
        self.myImg_config = cutil.Config(configid="myConfId", cdir=self.cdir)
        self.myImg_config.setDdir(self.train_data_dir)
        self.myImg_config.setOdir(self.img_croped_dir_path)
        self.myImg_config.setIdir(self.img_dir_path)

        self.df['h'] = 0
        self.df['w'] = 0
        self.df['imgpath'] = ""
        self.df['imgexists'] = False

        #initialize all variables...
        n_img_w = self.img_width
        n_img_h = self.img_heigth

        tot_cnt = self.df['level'].count()
        cnt = 0
        file_missing = 0

        #x_train = np.zeros(( tot_cnt, n_img_w, n_img_h, 3), dtype='uint8')
        x_train = np.zeros((0, n_img_w, n_img_h, 3), dtype='uint8')
        x_img_buf = np.empty((1, n_img_w, n_img_h, 3), dtype='uint8')
        y_buf = []
        y_train = np.empty((0, 1), dtype='uint8')

        #loop in through dataframe.
        for i, rec in self.df.iterrows():
            #if cnt > 50:
            #  break

            progress_sts = "%6d out of %6d" % (cnt, tot_cnt)
            sys.stdout.write("%6d out of %6d" % (cnt, tot_cnt))
            sys.stdout.write(
                "\b" * len(progress_sts))  # return to start of line, after '['
            sys.stdout.flush()

            imgpath = self.img_dir_path + rec.image + self.img_filename_ext
            self.df.loc[i, 'imgpath'] = imgpath

            #skip already processed data
            if os.path.exists(self.img_croped_dir_path + rec.image +
                              self.img_filename_ext):
                cnt += 1
                continue

            if os.path.exists(imgpath):
                myimg1 = myimg.myImg(imageid=str(i),
                                     config=self.myImg_config,
                                     path=imgpath)

                i_w, i_h = myimg1.getImageDim()
                croped_img_arr = np.zeros((n_img_w, n_img_h, 3), dtype='uint8')
                calc_img_w_offset = int((n_img_w - i_w) / 2)
                calc_img_h_offset = int((n_img_h - i_h) / 2)
                croped_img_arr[calc_img_w_offset:(calc_img_w_offset + i_w),
                               calc_img_h_offset:(calc_img_h_offset +
                                                  i_h), :] = myimg1.getImage()
                ''' 
        croped_img = tf.image.resize_image_with_crop_or_pad( myimg1.getImage(), n_img_w, n_img_h)
        init = tf.global_variables_initializer()
        croped_img_arr = 0
        with tf.Session() as sess:
          sess.run(init)
          croped_img_arr = sess.run(croped_img)
          print(v.shape,type(v))  # will show you your variable.
          v = np.reshape( v, ( n_img_w, n_img_h, 3))
          print(v.shape,type(v))  # will show you your variable.
        '''

                x_img_buf[0, :, :, :] = croped_img_arr

                #'''#use below block of code to debug croped image with original.
                #myimg1.showImage()
                #myimg1.saveImage(img_type_ext='.jpeg',gen_new_filename=False)
                myimg2 = myimg.myImg(imageid=str(i),
                                     config=self.myImg_config,
                                     path=rec.image + self.img_filename_ext,
                                     img=croped_img_arr)
                myimg2.saveImage(img_type_ext='.jpeg', gen_new_filename=True)
                #myimg2.saveImage()
                #'''

                #self.log( mname, "Croped Image [{}] [{}] [{}] [{}]".format(myimg1.getImage().shape,croped_img_arr.shape,x_train.shape,x_img_buf.shape), level=4)

                #x_train = np.vstack( (x_train, x_img_buf))
                #x_train[cnt,:,:,:] = croped_img_arr
                y_buf.append(rec.level)

                self.df.loc[i, 'imgexists'] = True
                self.df.loc[i, 'w'], self.df.loc[i, 'h'] = myimg1.getImageDim()
                self.df.loc[i, '_w'], self.df.loc[
                    i, '_h'] = croped_img_arr.shape[0], croped_img_arr.shape[1]
                #self.log( mname, "Image file [{}] doesn't exists!!!".format(imgpath), level=2)
            else:
                #self.log( mname, "Image file [{}] doesn't exists!!!".format(imgpath), level=2)
                file_missing += 1

            cnt += 1

        #create y array as required
        y_train = np.array(y_buf, dtype='uint8')
        y_train = np.reshape(y_train, (y_train.size, 1))
        #print final dimensionf or x_train and y_train
        self.log(mname,
                 "x_train [{}] y_train [{}]".format(x_train.shape,
                                                    y_train.shape),
                 level=3)

        self.log(mname, "Process dataset [{}]".format(cnt), level=3)
        self.log(mname, "File missing [{}]".format(file_missing), level=3)
        self.log(mname,
                 "Max image width[{}] heigth[{}]".format(
                     self.df['w'].max(), self.df['h'].max()),
                 level=3)
        #print(self.df.head(10))
        self.df.to_csv(self.train_data_dir + 'u_img_set.csv')
Example #9
0
    def process_img_by_id(self, img_id=None):
        fn = 'process_img'
        _w = self.img_size
        _h = self.img_size
        myimg1 = None

        if img_id is None:
            print("Image path cannot be null...")
            return False
        else:
            # load initial image
            img_path = self.imgdir + img_id
            if os.path.exists(img_path):
                myimg1 = myimg.myImg(imageid='t1',
                                     config=self.myImg_config,
                                     path=img_path)
            else:
                print("Image file [%s] does not exists..." % (img_path))
                sys.exit(-1)

            # set parameters
            patch_size = (self.patch_size, self.patch_size)
            stride = self.patch_stride

            image_org = myimg1.getImage()
            print('img_path ', img_path, ' img shape ', image_org.shape)

            extracted_patches, block_shape = self.get_img_patches2(image_org)
            self.mylog(fn,
                       "extracted image shape %s" % (extracted_patches.shape))
            # block_shape is the number of patches extracted in the x and in the y dimension
            # extracted_patches.shape = (1, block_shape[0] * block_shape[1], patch_size[0], patch_size[1], 3)
            extracted_patches = tf.reshape(extracted_patches, [
                1, block_shape[0] * block_shape[1], patch_size[0],
                patch_size[1], 3
            ])

            reconstructed_img, stratified_img = self.reconstruct_img_from_patches(
                extracted_patches, block_shape, stride)  # Reconstruct Image

            with tf.Session() as sess:
                ep, bs, ri, si = sess.run(
                    [extracted_patches, block_shape, reconstructed_img])
                #ep, bs, ri, si = sess.run([extracted_patches, block_shape, reconstructed_img, stratified_img], feed_dict={input_img: image})
                #ep, bs = sess.run([extracted_patches, block_shape], feed_dict={input_img: image})
                #ep, bs = sess.run([extracted_patches, block_shape])
                # print(bs)
            #si = si.astype(np.int32)

            print(
                '*****************************************************************'
            )
            print('original image ', image_org.shape)
            print('extracted patches ', ep.shape)
            print('block  ', bs)
            #print('reconstructed image  ',ri.shape)
            #print('stratified image  ',si.shape)
            print(
                '*****************************************************************'
            )
            '''