ish028792 commited on
Commit
73f0df0
·
verified ·
1 Parent(s): 424e9d9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from tensorflow.keras.models import load_model
3
+ import numpy as np
4
+ import cv2
5
+ import os
6
+ from flask import Flask, request, jsonify
7
+ from werkzeug.utils import secure_filename
8
+
9
+ # Initialize Flask app
10
+ app = Flask(_name_)
11
+
12
+ # Load the trained Keras model
13
+ MODEL_PATH = "model.weights.h5"
14
+ model = load_model(MODEL_PATH)
15
+
16
+ # Define allowed extensions for image upload
17
+ ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
18
+
19
+ def allowed_file(filename):
20
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
21
+
22
+ def preprocess_image(image_path):
23
+ """ Preprocess the uploaded image to match the model's input shape """
24
+ img = cv2.imread(image_path)
25
+ img = cv2.resize(img, (224, 224)) # Resize to model's expected input size
26
+ img = img / 255.0 # Normalize pixel values
27
+ img = np.expand_dims(img, axis=0) # Add batch dimension
28
+ return img
29
+
30
+ @app.route('/predict', methods=['POST'])
31
+ def predict():
32
+ """ API endpoint to predict melanoma from an uploaded image """
33
+ if 'file' not in request.files:
34
+ return jsonify({"error": "No file uploaded"}), 400
35
+
36
+ file = request.files['file']
37
+ if file.filename == '':
38
+ return jsonify({"error": "No selected file"}), 400
39
+
40
+ if file and allowed_file(file.filename):
41
+ filename = secure_filename(file.filename)
42
+ file_path = os.path.join("uploads", filename)
43
+ file.save(file_path)
44
+
45
+ # Preprocess and predict
46
+ image = preprocess_image(file_path)
47
+ prediction = model.predict(image)
48
+ os.remove(file_path) # Remove the file after prediction
49
+
50
+ # Assuming model outputs a probability (0 = not melanoma, 1 = melanoma)
51
+ result = "Melanoma" if prediction[0][0] > 0.5 else "Not Melanoma"
52
+
53
+ return jsonify({"prediction": result, "confidence": float(prediction[0][0])})
54
+
55
+ return jsonify({"error": "Invalid file format"}), 400
56
+
57
+ if _name_ == '_main_':
58
+ os.makedirs("uploads", exist_ok=True) # Ensure upload directory exists
59
+ app.run(host='0.0.0.0', port=5000, debug=True)