Upload 2 files
Browse files- app.py +59 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
from tensorflow.keras.models import load_model
|
| 4 |
+
from flask import Flask, request, jsonify
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from transformers import AutoFeatureExtractor, AutoTokenizer, VisionEncoderDecoderModel
|
| 7 |
+
from flask_cors import CORS
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
CORS(app)
|
| 11 |
+
# Load the first model - Classfication -
|
| 12 |
+
loaded_model = load_model('C:/files/kolia/GP/ml/model.h5')
|
| 13 |
+
|
| 14 |
+
# Load the second model and tokenizer -- Genrate Report --
|
| 15 |
+
encoder_checkpoint = "google/vit-base-patch16-224-in21k"
|
| 16 |
+
decoder_checkpoint = "ahmedabdo/facebook-bart-base-finetuned"
|
| 17 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(encoder_checkpoint)
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(decoder_checkpoint)
|
| 19 |
+
model = VisionEncoderDecoderModel.from_pretrained(decoder_checkpoint).to('cpu')
|
| 20 |
+
|
| 21 |
+
def preprocess_image(image_path):
|
| 22 |
+
image = cv2.imread(image_path)
|
| 23 |
+
resized_image = cv2.resize(image, (224, 224))
|
| 24 |
+
input_image = np.expand_dims(resized_image, axis=0)
|
| 25 |
+
return input_image
|
| 26 |
+
|
| 27 |
+
def predict_image(image_path):
|
| 28 |
+
input_image = preprocess_image(image_path)
|
| 29 |
+
predictions = loaded_model.predict(input_image)
|
| 30 |
+
return predictions
|
| 31 |
+
|
| 32 |
+
def get_predicted_label(predictions):
|
| 33 |
+
int_to_label = {0: 'Arm', 1: 'Chest', 2: 'Knee', 3: 'Vertebrae'}
|
| 34 |
+
predicted_label = int_to_label[np.argmax(predictions)]
|
| 35 |
+
return predicted_label
|
| 36 |
+
|
| 37 |
+
def predict_second_model(image):
|
| 38 |
+
features = feature_extractor(image, return_tensors="pt").pixel_values.to("cpu")
|
| 39 |
+
caption = tokenizer.decode(model.generate(features, max_length=1024)[0], skip_special_tokens=True)
|
| 40 |
+
return caption
|
| 41 |
+
|
| 42 |
+
# Route to predict image label using the first model
|
| 43 |
+
@app.route('/predict', methods=['POST'])
|
| 44 |
+
def predict():
|
| 45 |
+
file = request.files['image']
|
| 46 |
+
if file.filename == '':
|
| 47 |
+
return jsonify({'error': 'No selected file. Please upload a chest X-ray image.'})
|
| 48 |
+
image_path = file.filename
|
| 49 |
+
file.save(image_path)
|
| 50 |
+
predictions = predict_image(image_path)
|
| 51 |
+
predicted_label = get_predicted_label(predictions)
|
| 52 |
+
|
| 53 |
+
if predicted_label == 'Chest':
|
| 54 |
+
img = Image.open(image_path).convert("RGB")
|
| 55 |
+
caption = predict_second_model(img)
|
| 56 |
+
return jsonify({'predicted_label': predicted_label,'caption': caption})
|
| 57 |
+
else:
|
| 58 |
+
return jsonify({'predicted_label': predicted_label, 'caption': 'Please upload a chest X-ray image.'})
|
| 59 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask==3.0.2
|
| 2 |
+
numpy==1.26.4
|
| 3 |
+
transformers==4.38.2
|
| 4 |
+
Flask-Cors==4.0.0
|
| 5 |
+
Pillow==10.2.0
|
| 6 |
+
gunicorn
|