step = 0 for imagePath in glob.glob(datasetDir + "/*.*g"): photoName = imagePath[imagePath.rfind("/") + 1:] image = cv2.imread(imagePath) size = image.shape[:2] coefficient = size[0] / size[1] # Resize image width = INIT_SIZE height = int(INIT_SIZE * coefficient) image = cv2.resize(image, (width, height)) # Crop center image = image[height // 8:(7 * height) // 8, width // 8:(7 * width) // 8] # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) features = desc.describe(image) index[photoName] = features if step % 20 == 0: print("progress: %d" % step) step += 1 f = open(args["index"], "wb") f.write(pickle.dumps(index)) f.close() print("Done. Indexed %d images" % (len(index)))
list_images = glob.glob("../data/flowers/*.jpg") # initialize our image descriptor -- a 3D RGB histogram with # 8 bins per channel index = {} i = 0 # use list_images to grab the image paths and loop over them for imagePath in list_images: i+=1 # extract our unique image ID (i.e. the filename) k = imagePath[imagePath.rfind("/") + 1:] # load the image, describe it using our RGB histogram # descriptor, and update the index img = cv2.imread(imagePath) features = desc.describe(img) index[k] = [round(digit) for digit in features * 10e2] # index[k] = features # print(k, ":", index[k]) # if (i >= 10): # break # tao 1 feature-values dua vao descList desc_list = {} # for i, feature in enumerate(index.values()): #load het cac features cua tat ca anh # for fi,value in enumerate(feature): # voi moi anh, duyet het 512 features,, tao inverted index # tao 1 dict # check xem fi (feature_value Fi) co ton tai, if not init, if yes add # sub_desc == Vij cua Features_value, sub_desc[gia tri tai Fi] = index cua tam anh
from RGBHistogram import RGBHistogram from ZernikeMoments import ZernikeMoments import cv2 from matplotlib import pyplot as plt import glob import pickle from Searcher import Searcher # desc = RGBHistogram([8 8, 8]) desc = RGBHistogram([4, 4, 4]) test_image_path = '../data/flowers/image_0030.jpg' query_image = cv2.imread(test_image_path) query_feature = desc.describe(query_image) # print(query_feature) # quit() # load the index and initialize our searcher index = pickle.load(open("Histogram_only_index_4bins.cpickle", "rb")) print(index.items()) searcher = Searcher(index) results = searcher.search(query_feature) print(results) for i in range(0, 5): ret_path = '../data/' + results[i][1].replace('\\','/') img = cv2.imread(ret_path) cv2.imshow(str(i), img) cv2.waitKey() exit()
imagePaths = sorted(glob.glob("dataset/images/*.png")) maskPaths = sorted(glob.glob("dataset/masks/*.png")) imagePath = "TestImg/picTac.png" data = [] target = [] desc = RGBHistogram([8, 8, 8]) counter = 0 for imagePath, maskPath in zip(imagePaths, maskPaths): image = cv2.imread(imagePath) mask = cv2.imread(maskPath) mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) features = desc.describe(image, mask) data.append(features) target.append(imagePath.split('_')[-2]) #print target, '\n' targetNames = np.unique(target) le = LabelEncoder() target = le.fit_transform(target) (trainData, testData, trainTarget, testTarget) = train_test_split(data, target, test_size = 0.3, random_state = 42) model = RandomForestClassifier(n_estimators=25, random_state=84) model.fit(trainData, trainTarget)