File size: 1,895 Bytes
b65d1cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
from glob import glob
import cv2 as cv
import sklearn as sk
import pandas as pd
class utils():
    def __init__(self,txtPath,imagePath):
        self.image,self.txtFeature , self.label = self.loadData(txtPath,imagePath)
        self.const = None

    def split(self):
        split = sk.modelselection.train_test_split(self.Data,test_size = 0.2)
        return split
    def loadData(self,txtPath,imgPath):
        txtFeatur = pd.DataFrame([[float(t) for t in x.split('\n')[0].split(' ')] for x in open(txtPath).readlines()])
        txtFeatur.T.loc[[0,1,2,3],:]
        label = txtFeatur.loc[:,4] 
        txtFeatur = txtFeatur.T.loc[[0,1,2,3],:].T 
        max = txtFeatur.max()
        txtFeatur = txtFeatur/max
        self.const = np.max(label)
        label = label/self.const


        for i,p in enumerate(glob(imgPath+'/*')):
            bedroom = []
            bathroom = []
            frontal = []
            kitchen = []
            place = p.split('/')[-1].split('.')[0].split('_')[1]
            if place == 'bedroom':
                bedroom.append(self.preProcess(cv.imread(p)))
            elif place == 'bathroom':
                bathroom.append(self.preProcess(cv.imread(p)))
            elif place == 'frontal':
                frontal.append(self.preProcess(cv.imread(p)))
            else:
                kitchen.append(self.preProcess(cv.imread(p)))
            if i%500 == 0:
                print('[INFO] {}th image loaded'.format(i))
        # bedroom = np.array(bedroom)
        # bathroom = np.array(bathroom)
        # frontal = np.array(frontal)
        # kitchen = np.array(kitchen)
        return ([bedroom,bathroom,frontal,kitchen],txtFeatur, label)
    @staticmethod
    def preProcess(image):
        image = cv.resize(image,(128,128))
        image = cv.cvtColor(image,cv.COLOR_BGR2RGB)
        image = image/255.0
        return image