处理用于骨折检测的 Flask 应用程序中的不相关上传

问题描述 投票:0回答:1

我正在开发一个用于骨折检测的 Flask 应用程序,用户可以上传 X 射线图像,该应用程序可以预测是否存在骨折。但是,我在处理不相关的上传时遇到问题,例如肘部、手和肩膀以外的身体部位的图像。

以下是我的 Flask 应用程序代码的概述:

Flask 应用程序代码

import os
from flask import Flask, request, jsonify
from werkzeug.utils import secure_filename
from predictions import predict
from flask_cors import CORS

app = Flask(__name__)
CORS(app)  # Enable CORS for all routes
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg'}

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']

@app.route('/predict', methods=['POST'])
def predict_bone_fracture():
    if 'file' not in request.files:
        return jsonify({'error': 'No file part'})

    file = request.files['file']

    if file.filename == '':
        return jsonify({'error': 'No selected file'})

    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        file.save(filepath)

        # Perform prediction using the predict function from predictions.py
        bone_type_result = predict(filepath)
        result = predict(filepath, bone_type_result)

        # You can customize the response based on your requirements
        return jsonify({'bone_type': bone_type_result, 'result': result})

    return jsonify({'error': 'Invalid file format'})

if __name__ == "__main__":
    app.run()

predict_bone_fracture() 函数接收上传的图像,将其保存到指定文件夹,然后使用外部模块 (predictions.py) 中的 Predict() 函数执行预测。如果上传的文件不是图像或格式不受支持,则会返回错误响应。

我主要关心的是如何处理用户上传与指定身体部位(即肘部、手部、肩膀)不对应的图像的情况。例如,如果用户上传眼睛而不是骨头的图像,应用程序应拒绝上传并提供适当的错误消息。

我相信我需要采用一种机制来检测上传图像中的相关身体部位,并验证它们是否与预期的预测身体部位相匹配(即肘部、手部、肩膀)。但是,我不确定实现此目的的最佳方法。

您能否提供有关如何解决此问题的建议或想法?具体来说,我正在寻找以下方面的指导:

实施一种机制来检测上传图像中的相关身体部位。 检查检测到的身体部位是否与预期的预测身体部位相匹配。 为不相关的上传提供适当的错误处理和消息。 任何见解或代码示例将不胜感激。谢谢!

python machine-learning flask deep-learning
1个回答
0
投票

为了解决不相关上传的问题并确保仅接受包含相关身体部位(肘部、手部和肩膀)的图像进行预测,您可以集成基于深度学习的对象检测模型来识别和定位这些身体部位上传的图像。一旦检测到相关的身体部位,您可以将它们与预期的身体部位进行比较以进行预测,并拒绝不符合条件的上传。

以下是如何在 Flask 应用程序中实现此功能:

  1. 集成目标检测模型:您可以使用 YOLO(You Only Look Once)、SSD(Single Shot MultiBox Detector)或 Faster R-CNN 等预训练模型进行目标检测。这些模型能够检测图像中的各种物体,包括身体部位。

  2. 定义预期身体部位:创建用于预测的预期身体部位列表(即肘部、手部、肩膀)。

  3. 检查检测到的身体部位:执行物体检测后,检查检测到的物体是否与预期的身体部位相对应。

  4. 处理不相关的上传:如果没有检测到相关的身体部位或者检测到的身体部位与预期不匹配,则拒绝上传并提供适当的错误消息。

这是包含以下步骤的 Flask 应用程序代码的修改版本:

import os
from flask import Flask, request, jsonify
from werkzeug.utils import secure_filename
from object_detection import detect_body_parts
from predictions import predict
from flask_cors import CORS

app = Flask(__name__)
CORS(app)  # Enable CORS for all routes
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg'}
EXPECTED_BODY_PARTS = {'elbow', 'hand', 'shoulder'}

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']

@app.route('/predict', methods=['POST'])
def predict_bone_fracture():
    if 'file' not in request.files:
        return jsonify({'error': 'No file part'})

    file = request.files['file']

    if file.filename == '':
        return jsonify({'error': 'No selected file'})

    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        file.save(filepath)

        # Perform object detection to detect body parts
        detected_body_parts = detect_body_parts(filepath)

        # Check if any detected body parts match the expected ones
        if any(part in EXPECTED_BODY_PARTS for part in detected_body_parts):
            # Perform prediction using the predict function from predictions.py
            bone_type_result = predict(filepath)
            result = predict(filepath, bone_type_result)
            return jsonify({'bone_type': bone_type_result, 'result': result})
        else:
            # Reject upload if no relevant body parts are detected
            return jsonify({'error': 'Irrelevant upload. Please upload images containing elbows, hands, or shoulders.'})

    return jsonify({'error': 'Invalid file format'})

if __name__ == "__main__":
    app.run()

在此修改后的代码中:

  • detect_body_parts
    是外部模块 (
    object_detection.py
    ) 的一项功能,负责执行对象检测以识别上传图像中的相关身体部位。
  • EXPECTED_BODY_PARTS
    是包含用于预测的预期身体部位的集合。
  • 如果没有检测到相关的身体部位或者检测到的身体部位与预期不匹配,则返回相应的错误消息。

确保您有一个

object_detection.py
模块,其中包含对象检测所需的代码,并且它返回检测到的身体部位的列表。您可能需要在包含肘部、手部和肩部图像的数据集上训练或微调对象检测模型,以实现准确的检测。此外,调整代码以使用适当的对象检测模型,并确保其正确配置并集成到您的 Flask 应用程序中。

© www.soinside.com 2019 - 2024. All rights reserved.