HousePricePrediction: utils file
Browse files
utils.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from glob import glob
|
| 3 |
+
import cv2 as cv
|
| 4 |
+
import sklearn as sk
|
| 5 |
+
import pandas as pd
|
| 6 |
+
class utils():
|
| 7 |
+
def __init__(self,txtPath,imagePath):
|
| 8 |
+
self.image,self.txtFeature , self.label = self.loadData(txtPath,imagePath)
|
| 9 |
+
self.const = None
|
| 10 |
+
|
| 11 |
+
def split(self):
|
| 12 |
+
split = sk.modelselection.train_test_split(self.Data,test_size = 0.2)
|
| 13 |
+
return split
|
| 14 |
+
def loadData(self,txtPath,imgPath):
|
| 15 |
+
txtFeatur = pd.DataFrame([[float(t) for t in x.split('\n')[0].split(' ')] for x in open(txtPath).readlines()])
|
| 16 |
+
txtFeatur.T.loc[[0,1,2,3],:]
|
| 17 |
+
label = txtFeatur.loc[:,4]
|
| 18 |
+
txtFeatur = txtFeatur.T.loc[[0,1,2,3],:].T
|
| 19 |
+
max = txtFeatur.max()
|
| 20 |
+
txtFeatur = txtFeatur/max
|
| 21 |
+
self.const = np.max(label)
|
| 22 |
+
label = label/self.const
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
for i,p in enumerate(glob(imgPath+'/*')):
|
| 26 |
+
bedroom = []
|
| 27 |
+
bathroom = []
|
| 28 |
+
frontal = []
|
| 29 |
+
kitchen = []
|
| 30 |
+
place = p.split('/')[-1].split('.')[0].split('_')[1]
|
| 31 |
+
if place == 'bedroom':
|
| 32 |
+
bedroom.append(self.preProcess(cv.imread(p)))
|
| 33 |
+
elif place == 'bathroom':
|
| 34 |
+
bathroom.append(self.preProcess(cv.imread(p)))
|
| 35 |
+
elif place == 'frontal':
|
| 36 |
+
frontal.append(self.preProcess(cv.imread(p)))
|
| 37 |
+
else:
|
| 38 |
+
kitchen.append(self.preProcess(cv.imread(p)))
|
| 39 |
+
if i%500 == 0:
|
| 40 |
+
print('[INFO] {}th image loaded'.format(i))
|
| 41 |
+
# bedroom = np.array(bedroom)
|
| 42 |
+
# bathroom = np.array(bathroom)
|
| 43 |
+
# frontal = np.array(frontal)
|
| 44 |
+
# kitchen = np.array(kitchen)
|
| 45 |
+
return ([bedroom,bathroom,frontal,kitchen],txtFeatur, label)
|
| 46 |
+
@staticmethod
|
| 47 |
+
def preProcess(image):
|
| 48 |
+
image = cv.resize(image,(128,128))
|
| 49 |
+
image = cv.cvtColor(image,cv.COLOR_BGR2RGB)
|
| 50 |
+
image = image/255.0
|
| 51 |
+
return image
|