Esempio n. 1
0
def predict_plant_disease(request):
    # try:
    if request.method == "GET":
        return_data = {"error": "0", "message": "Working!!!"}
    else:
        if request.body:
            request_data = request.data["plant_image"]
            header, image_data = request_data.split(';base64,')
            image_array, err_msg = image_converter.convert_image(image_data)

            if err_msg == None:
                model_file = f"{BASE_DIR}/ml_files/sequential.pkl"
                saved_classifier_model = pickle.load(open(model_file, 'rb'))
                prediction = saved_classifier_model.predict(image_array)
                label_binarizer = pickle.load(
                    open(f"{BASE_DIR}/ml_files/label_bin.pkl", 'rb'))
                disease_name = label_binarizer.inverse_transform(prediction)[0]

                f = open(f"cure_data/{disease_name}.txt", "r+")
                suggestion = f.read()

                return_data = {
                    "error": "0",
                    "data": f"{disease_name}",
                    "suggestion": f"{suggestion}"
                }

    return HttpResponse(json.dumps(return_data),
                        content_type='application/json; charset=utf-8')
Esempio n. 2
0
def predict_plant_disease(request):
    try:
        if request.method == "GET":
            return_data = {
                "error":
                "0",
                "message":
                "Plant Disease Recognition API. Built by Lichen Wang @Team 13 CS260"
            }
        else:
            if request.body:
                request_data = request.data["plant_image"]
                request_data += "=" * ((4 - len(request_data) % 4) % 4)
                image_array, err_msg = image_converter.convert_image(
                    request_data)
                print(err_msg)
                if err_msg == None:

                    with graph.as_default():
                        prediction = model.predict(image_array)

                    prediction_flatten = prediction.flatten()
                    max_val_index = np.argmax(prediction_flatten)
                    result = output_list[max_val_index]
                    prob = max(prediction_flatten) * 100
                    # return Response({'result': result})

                    # model_file = f"{BASE_DIR}/ml_files/cnn_model.pkl"
                    # saved_classifier_model = pickle.load(open(model_file,'rb'))
                    # prediction = saved_classifier_model.predict(image_array)
                    # label_binarizer = pickle.load(open(f"{BASE_DIR}/ml_files/label_transform.pkl",'rb'))

                    return_data = {"prob": '%.2f%%' % prob, "data": result}
                else:
                    return_data = {
                        "error": "4",
                        "message": f"Error : {err_msg}"
                    }
            else:
                return_data = {
                    "error": "1",
                    "message": "Request Body is empty",
                }
    except Exception as e:
        return_data = {
            "error": "3",
            "message": f"Error : {str(e)}",
        }
    return HttpResponse(json.dumps(return_data),
                        content_type='application/json; charset=utf-8')
Esempio n. 3
0
def predict_plant_disease(request):
    with open(f"{BASE_DIR}/download.jpg", "rb") as f:
        image_data = base64.b64encode(f.read())
    image_array, err_msg = image_converter.convert_image(image_data)
    model_file = f"{BASE_DIR}/ml_files/cnn_model.pkl"
    saved_classifier_model = pickle.load(open(model_file, 'rb'))
    prediction = saved_classifier_model.predict(image_array)
    label_binarizer = pickle.load(
        open(f"{BASE_DIR}/ml_files/label_transform.pkl", 'rb'))
    return_data = {
        "error": "0",
        "data": f"{label_binarizer.inverse_transform(prediction)[0]}"
    }
    return HttpResponse(json.dumps(return_data),
                        content_type='application/json; charset=utf-8')
Esempio n. 4
0
def predict_plant_disease(request):
    try:
        if request.method == "GET":
            return_data = {
                "error":
                "0",
                "message":
                "Plant Disease Recognition Api. Built for NaijaHacks 2018"
            }
        else:
            if request.body:
                request_data = request.data["plant_image"]
                header, image_data = request_data.split(';base64,')
                image_array, err_msg = image_converter.convert_image(
                    image_data)
                if err_msg == None:
                    model_file = f"{BASE_DIR}/ml_files/cnn_model.pkl"
                    saved_classifier_model = pickle.load(open(
                        model_file, 'rb'))
                    prediction = saved_classifier_model.predict(image_array)
                    label_binarizer = pickle.load(
                        open(f"{BASE_DIR}/ml_files/label_transform.pkl", 'rb'))
                    return_data = {
                        "error":
                        "0",
                        "data":
                        f"{label_binarizer.inverse_transform(prediction)[0]}"
                    }
                else:
                    return_data = {
                        "error": "4",
                        "message": f"Error : {err_msg}"
                    }
            else:
                return_data = {
                    "error": "1",
                    "message": "Request Body is empty",
                }
    except Exception as e:
        return_data = {
            "error": "3",
            "message": f"Error : {str(e)}",
        }
    return HttpResponse(json.dumps(return_data),
                        content_type='application/json; charset=utf-8')
Esempio n. 5
0
def predict_plant_disease(request):
    print('Runing predict_plant_disease')
    try:
        if request.method == "GET" :
            return_data = {
                "error" : "0",
                "message" : "SinghSahani Enterprise"
            }
        else:
            if request.body:
                request_data = request.data["plant_image"]
                header, image_data = request_data.split(';base64,')
                image_array, err_msg = image_converter.convert_image(image_data)
                if err_msg == None :
                    model_file = f'{BASE_DIR}/ml_files/cnn.h5'
                    saved_classifier_model = tf.keras.models.load_model(model_file)
                    prediction = saved_classifier_model.predict(image_array) 
                    label_binarizer = pickle.load(open(f"{BASE_DIR}/ml_files/label_transform.pkl",'rb'))
                    return_data = {
                        "error" : "0",
                        "data" : f"{label_binarizer.inverse_transform(prediction)[0]}"
                    }
                else :
                    return_data = {
                        "error" : "4",
                        "message" : f"Error : {err_msg}"
                    }
            else :
                return_data = {
                    "error" : "1",
                    "message" : "Request Body is empty",
                }
    except Exception as e:
        return_data = {
            "error" : "3",
            "message" : f"Error : {str(e)}",
        }
    return HttpResponse(json.dumps(return_data), content_type='application/json; charset=utf-8')