def prep_image(image, target): if (image.mode != "RGB"): image = image.convert("RGB") image = image.resize(target) image = img_to_array(image) image = np.expand_dims(image, axis=0) image = imagenet_utils.preprocess_input(image) return image
def color_classification(single_path): """ This function does color classification for a certain path of a photo (of a clothes) Input is a path on your computer Output is a color """ image = Image.open(single_path) image = image.convert('RGB') return get_cloth_color(image)
def return_predictions(model, image): if image.mode != "RGB": image = image.convert("RGB") adding_dimensions = np.expand_dims(image, axis=0) prediction = model.predict(adding_dimensions) if prediction > 0.5: return 'uninfected' else: return 'parasitized' return prediction
def preprocess_image(image, target_size): '''function to preprocess input image''' if image.mode != "RGB": image = image.convert("RGB") # if image is not RGB then convert to RGB image = image.resize(target_size) # resize image to defined target size # image = image.img_to_array(image) # convert image to an array of numbers image = asarray(image) image = image.astype(np.float32) # convert from uint8 to float32 image = np.expand_dims(image, axis=0) # expand the dimension of the image return image
def process_image(image): #read image image = Image.open(BytesIO(image)) if image.mode != "RGB": image = image.convert("RGB") # resize and convert to tensor image = image.resize((96, 96)) image = img_to_array(image) image = preprocess_input(image) image = np.expand_dims(image, axis=0) return image
def prepare_image(image, target): # if the image mode is not RGB, convert it if image.mode != "RGB": image = image.convert("RGB") # resize the input image and preprocess it image = image.resize(target) image = img_to_array(image) image = np.expand_dims(image, axis=0) image = imagenet_utils.preprocess_input(image) # return the processed image return image
def get(self, user_index): credentials_json = open("credentials.json", "rb") config = pickle.load(credentials_json) credentials_json.close() MODEL_PATH = 'models/covid_new_model1.model' # Load your own trained model model = load_model(MODEL_PATH) model._make_predict_function() firebase = pyrebase.initialize_app(config) db = firebase.database() nm = db.child("Appointments").child(user_index).get() res = nm.val() xray_frame = res['xraybase64'] image = base64_to_pil(xray_frame) if image.mode != "RGB": image = image.convert("RGB") image = image.resize((224, 224)) image = img_to_array(image) image /= 255 image = np.expand_dims(image, axis=0) # Be careful how your trained model deals with the input # otherwise, it won't make correct prediction! preds = model.predict(image) pred_proba = "{:.3f}".format(np.amax(preds)) # Max probability pred_class = preds print(pred_proba) print(pred_class) if pred_class[0][0] >= 0.5: result = "Covid-19 tested positive. [Confidence ratio: " + str( round(pred_class[0][0] * 100, 2)) + "%]" # result += str(pred_class[0][0]) else: result = "Covid-19 tested negative. [Confidence ratio: " + str( round((1 - pred_class[0][0]) * 100, 2)) + "%]" # result += str(pred_class[0][1]) print(result) db.child("Appointments").child(user_index).update({"report": result}) return (1)
def upload_file(): if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: flash('No file part') return redirect(request.url) file = request.files['file'] # if user does not select file, browser also # submit an empty part without filename if file.filename == '': flash('No selected file') return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) # model = load_model('./animal_cnn_aug.h5') model = VGG16(weights='imagenet') image = Image.open(filepath) image = image.convert('RGB') image = image.resize((image_size, image_size)) data = np.asarray(image) X = [] X.append(data) X = np.array(X) result = model.predict([X])[0] predicted = result.argmax() percentage = int(result[predicted] * 100) return "ラベル: " + classes[predicted] + ", 確率:"+ str(percentage) + " %" # return redirect(url_for('uploaded_file', # filename=filename)) return '''
def predict(features_array): df = pd.DataFrame(columns=[ 'dst_port', 'protocol', 'timestamp', 'flow_duration', 'tot_fwd_pkts', 'tot_bwd_pkts', 'totlen_fwd_pkts', 'totlen_bwd_pkts', 'flow_byts/s', 'flow_pkts/s', 'fwd_iat_tot', 'bwd_iat_tot', 'fwd_psh_flags', 'bwd_psh_flags', 'fwd_urg_flags', 'bwd_urg_flags', 'fwd_header_len', 'bwd_header_len', 'fwd_pkts/s', 'bwd_pkts/s', 'pkt_len_var', 'fin_flag_cnt', 'syn_flag_cnt', 'rst_flag_cnt', 'psh_flag_cnt', 'ack_flag_cnt', 'urg_flag_cnt', 'cwe_flag_count', 'ece_flag_cnt', 'down/up_ratio', 'pkt_size_avg', 'fwd_seg_size_avg', 'bwd_seg_size_avg', 'fwd_byts/b_avg', 'fwd_pkts/b_avg', 'fwd_blk_rate_avg', 'bwd_byts/b_avg', 'bwd_pkts/b_avg', 'bwd_blk_rate_avg', 'subflow_fwd_pkts', 'subflow_fwd_byts', 'subflow_bwd_pkts', 'subflow_bwd_byts', 'init_fwd_win_byts', 'init_bwd_win_byts', 'fwd_act_data_pkts' ]) df.loc[len(df), :] = features_array x_1 = numeric_features(df) image = convert_to_image(x_1) image = image.convert("RGB") img = np.array(image) img = np.expand_dims(img, 0) all_models = list() all_models.append(model_res) all_models.append(model_vgg) yhats = [model.predict(img) for model in all_models] # sum across ensemble members summed = np.sum(yhats, axis=0) # argmax across classes result = np.argmax(summed, axis=1) if (result == 0): return False else: return True