def preprocess_datasets(self, path_dataset, groupStage): PATH_DATA = os_path.join(path_dataset, "4") print("(INFO) EVALUATING DATASET ...") path_img = sorted(listdir(PATH_DATA)) if path_img == []: return -1, -1 num_img = len(path_img) # Histogram of all images in folder hChannel = [] sChannel = [] vChannel = [] for image_path in path_img: img = imread(os_path.join(PATH_DATA, image_path)) img = resize(img, (6000, 4000)) img = img[500:-500, 750:-750, :] # HSV channel img = cvtColor(img, COLOR_BGR2HSV) # HSV histogram h = calcHist([img], [0], None, [256], [0, 256]).reshape(256, ) s = calcHist([img], [1], None, [256], [0, 256]).reshape(256, ) v = calcHist([img], [2], None, [256], [0, 256]).reshape(256, ) hChannel.append(h) sChannel.append(s) vChannel.append(v) # Compute dissimilarity maxI = 0 for i in range(num_img): one = [] for j in range(num_img): c1 = np_sum( np_absolute(hChannel[j] - hChannel[i])) / (HEIGHT * WIDTH) c2 = np_sum( np_absolute(sChannel[j] - sChannel[i])) / (HEIGHT * WIDTH) c = (c1 + c2) / 2 if c > maxI: maxI = c save = [i, j] img0 = path_img[save[0]] img1 = path_img[save[1]] imgSample1 = os_path.join(PATH_DATA, img0) imgSample2 = os_path.join(PATH_DATA, img1) return imgSample1, imgSample2
def predict_image(path_of_image, groupStage): path_of_model = os_path.join("./CUSTOMIZE_4_USER/MODEL_TRAINING", groupStage, groupStage + ".pth") path_of_feature = os_path.join("./CUSTOMIZE_4_USER/MODEL_TRAINING", groupStage, groupStage + ".npz") start_time = time() model = NeuralNet(input_size, hidden_size, num_classes).to(device) model.load_state_dict(load(path_of_model)) data = np_load(path_of_feature) [h_max, s_max, v_max] = data['data_max'] [h_min, s_min, v_min] = data['data_min'] img = imread(path_of_image) img = resize(img, (6000, 4000)) img = img[500:-500, 750:-750, :] img = cvtColor(img, COLOR_BGR2HSV) hchan, schan, vchan = split(img) h_hist = calcHist([img], [0], None, [256], [0, 256]).reshape(256, ) s_hist = calcHist([img], [1], None, [256], [0, 256]).reshape(256, ) v_hist = calcHist([img], [2], None, [256], [0, 256]).reshape(256, ) hMean = np_mean(hchan) / 255 DPV_h_max = np_sum(np_absolute(h_hist - h_max)) / (HEIGHT * WIDTH) DPV_h_min = np_sum(np_absolute(h_hist - h_min)) / (HEIGHT * WIDTH) sMean = np_mean(schan) / 255 DPV_s_max = np_sum(np_absolute(s_hist - s_max)) / (HEIGHT * WIDTH) DPV_s_min = np_sum(np_absolute(s_hist - s_min)) / (HEIGHT * WIDTH) vMean = np_mean(vchan) / 255 DPV_v_max = np_sum(np_absolute(v_hist - v_max)) / (HEIGHT * WIDTH) DPV_v_min = np_sum(np_absolute(v_hist - v_min)) / (HEIGHT * WIDTH) correlation = np_corrcoef(h_hist, s_hist)[0][1] #image_feature = np_array((hMean, DPV_h_max, DPV_h_min, sMean, DPV_s_max, DPV_s_min, vMean, DPV_v_max, DPV_v_min)) image_feature = np_array((hMean, DPV_h_max, DPV_h_min, sMean, DPV_s_max, DPV_s_min, correlation)) image_feature = from_numpy(image_feature).to(device).float().view( 1, input_size) with no_grad(): out_predict = model(image_feature) _, predicted_result = torch_max(out_predict.data, 1) original = Tensor([[1, 33, 66, 99]]) # Round xx.xx % percentage_result = np_round( mm(out_predict.view(1, num_classes), original.view(num_classes, 1)).item(), 2) # Processed time processedTime = np_round(time() - start_time, 2) #print("Time ",processedTime) return percentage_result, processedTime
def process_feature(list_path, labelFeature): list_dir = sorted(listdir(list_path)) if list_dir == []: return -1 for image_path in list_dir: name_image = os_path.join(list_path, image_path) if name_image == imgSample1 or name_image == imgSample2: continue img = imread(name_image) img = resize(img, (6000, 4000)) img = img[500:-500, 750:-750, :] img = cvtColor(img, COLOR_BGR2HSV) hchan, schan, vchan = split(img) h_hist = calcHist([img], [0], None, [256], [0, 256]).reshape(256, ) s_hist = calcHist([img], [1], None, [256], [0, 256]).reshape(256, ) v_hist = calcHist([img], [2], None, [256], [0, 256]).reshape(256, ) hMean = np_mean(hchan) / 255 DPV_h_max = np_sum( np_absolute(h_hist - h_max)) / (HEIGHT * WIDTH) DPV_h_min = np_sum( np_absolute(h_hist - h_min)) / (HEIGHT * WIDTH) sMean = np_mean(schan) / 255 DPV_s_max = np_sum( np_absolute(s_hist - s_max)) / (HEIGHT * WIDTH) DPV_s_min = np_sum( np_absolute(s_hist - s_min)) / (HEIGHT * WIDTH) vMean = np_mean(vchan) / 255 DPV_v_max = np_sum( np_absolute(v_hist - v_max)) / (HEIGHT * WIDTH) DPV_v_min = np_sum( np_absolute(v_hist - v_min)) / (HEIGHT * WIDTH) correlation = np_corrcoef(h_hist, s_hist)[0][1] # variable = [hMean, DPV_h_max, DPV_h_min, sMean, DPV_s_max, DPV_s_min, vMean, DPV_v_max, DPV_v_min] variable = [ hMean, DPV_h_max, DPV_h_min, sMean, DPV_s_max, DPV_s_min, correlation ] feature.append(variable) labels.append([labelFeature])
def process_feature(list_path, labelFeature): print("Extracting...") list_dir = sorted(listdir(list_path)) if list_dir == []: return -1 for image_path in list_dir: name_image = os_path.join(list_path, image_path) if name_image == imgSample1 or name_image == imgSample2: continue img = imread(name_image) img = resize(img, (6000,4000)) img = img[500:-500, 750:-750, :] img = cvtColor(img, COLOR_BGR2HSV) hchan, schan, vchan = split(img) h_hist = calcHist([img], [0], None, [256], [0,256]).reshape(256,) s_hist = calcHist([img], [1], None, [256], [0,256]).reshape(256,) v_hist = calcHist([img], [2], None, [256], [0,256]).reshape(256,) # 7 feature consist of : # + Compute mean value pixel of H channel # + Dissilarity with H channel of "max" image # + Dissilarity with H channel of "min" image # + Compute mean value pixel of S channel # + Dissilarity with S channel of "max" image # + Dissilarity with S channel of "min" image # + Correlation between histogram of H and S channel hMean = np_mean(hchan)/255 DPV_h_max = np_sum(np_absolute(h_hist - h_max))/(HEIGHT*WIDTH) DPV_h_min = np_sum(np_absolute(h_hist - h_min))/(HEIGHT*WIDTH) sMean = np_mean(schan)/255 DPV_s_max = np_sum(np_absolute(s_hist - s_max))/(HEIGHT*WIDTH) DPV_s_min = np_sum(np_absolute(s_hist - s_min))/(HEIGHT*WIDTH) vMean = np_mean(vchan)/255 DPV_v_max = np_sum(np_absolute(v_hist - v_max))/(HEIGHT*WIDTH) DPV_v_min = np_sum(np_absolute(v_hist - v_min))/(HEIGHT*WIDTH) correlation = np_corrcoef(h_hist, s_hist)[0][1] # variable = [hMean, DPV_h_max, DPV_h_min, sMean, DPV_s_max, DPV_s_min, vMean, DPV_v_max, DPV_v_min] variable = [hMean, DPV_h_max, DPV_h_min, sMean, DPV_s_max, DPV_s_min, correlation] feature.append(variable) labels.append([labelFeature])
def AmpTrim(self): return np_absolute(self.ampTrim)
def Amp(self): return np_absolute(self.amp)
def predict_image(path_of_image, groupStage): path_of_model = os_path.join("./CUSTOMIZE_4_USER/MODEL_TRAINING", groupStage, groupStage+".pth") path_of_feature = os_path.join("./CUSTOMIZE_4_USER/MODEL_TRAINING", groupStage, groupStage+".npz") hidden_path = os_path.join("./CUSTOMIZE_4_USER/MODEL_TRAINING", groupStage, groupStage+"_hidden.txt") # if hidden number file is not existing, return 0 try: with open(hidden_path,"r") as f: hidden_size = int(f.readline()) except FileNotFoundError: print("ERROR: No hidden number file in folder") return 0.0,0 # Calculate processing time start_time = time() model = NeuralNet(input_size, hidden_size, num_classes).to(DEVICE) model.load_state_dict(load(path_of_model)) data = np_load(path_of_feature) [h_max, s_max, v_max] = data['data_max'] [h_min, s_min, v_min] = data['data_min'] img = imread(path_of_image) img = resize(img, (6000,4000)) img = img[500:-500, 750:-750, :] img = cvtColor(img, COLOR_BGR2HSV) hchan, schan, vchan = split(img) h_hist = calcHist([img], [0], None, [256], [0,256]).reshape(256,) s_hist = calcHist([img], [1], None, [256], [0,256]).reshape(256,) v_hist = calcHist([img], [2], None, [256], [0,256]).reshape(256,) # 7 features consist of : # + Compute mean value pixel of H channel # + Dissilarity with H channel of "max" image # + Dissilarity with H channel of "min" image # + Compute mean value pixel of S channel # + Dissilarity with S channel of "max" image # + Dissilarity with S channel of "min" image # + Correlation between histogram of H and S channel hMean = np_mean(hchan)/255 DPV_h_max = np_sum(np_absolute(h_hist - h_max))/(HEIGHT*WIDTH) DPV_h_min = np_sum(np_absolute(h_hist - h_min))/(HEIGHT*WIDTH) sMean = np_mean(schan)/255 DPV_s_max = np_sum(np_absolute(s_hist - s_max))/(HEIGHT*WIDTH) DPV_s_min = np_sum(np_absolute(s_hist - s_min))/(HEIGHT*WIDTH) vMean = np_mean(vchan)/255 DPV_v_max = np_sum(np_absolute(v_hist - v_max))/(HEIGHT*WIDTH) DPV_v_min = np_sum(np_absolute(v_hist - v_min))/(HEIGHT*WIDTH) correlation = np_corrcoef(h_hist, s_hist)[0][1] #image_feature = np_array((hMean, DPV_h_max, DPV_h_min, sMean, DPV_s_max, DPV_s_min, vMean, DPV_v_max, DPV_v_min)) image_feature = np_array((hMean, DPV_h_max, DPV_h_min, sMean, DPV_s_max, DPV_s_min, correlation)) image_feature = from_numpy(image_feature).to(DEVICE).float().view(1, input_size) with no_grad(): out_predict = model(image_feature) # Round xx.xx % percentage_result = np_round(out_predict.item()*99, 2) if percentage_result >99.99: percentage_result = 99.99 if percentage_result <1.0: percentage_result = 1.0 # Processed time processedTime = np_round(time()-start_time, 2) return percentage_result, processedTime