|
from flask import Blueprint, jsonify, request |
|
import io |
|
from app.utils import OCRModel |
|
|
|
main = Blueprint('main', __name__) |
|
ocr_model = OCRModel() |
|
|
|
|
|
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'} |
|
|
|
|
|
KNOWN_ALLERGENS = { |
|
'gluten': ['wheat', 'barley', 'gluten'], |
|
'dairy': ['milk', 'yogurt', 'cheese', 'lactose'], |
|
'nuts': ['nuts', 'peanuts', 'almonds', 'walnuts'], |
|
'eggs': ['eggs'], |
|
'soy': ['soy'], |
|
'fish': ['fish'], |
|
'shellfish': ['oyster', 'shrimp', 'shrimp'], |
|
} |
|
|
|
def allowed_file(filename): |
|
"""التحقق من صحة امتداد الملف""" |
|
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS |
|
|
|
def find_allergens(text, user_allergens): |
|
"""البحث عن الحساسيات في النص""" |
|
text = text.lower() |
|
found_allergens = set() |
|
allergen_details = {} |
|
|
|
for allergen in user_allergens: |
|
allergen = allergen.strip().lower() |
|
|
|
if allergen in KNOWN_ALLERGENS: |
|
for variant in KNOWN_ALLERGENS[allergen]: |
|
if variant.lower() in text: |
|
found_allergens.add(allergen) |
|
allergen_details[allergen] = variant |
|
|
|
elif allergen in text: |
|
found_allergens.add(allergen) |
|
allergen_details[allergen] = allergen |
|
|
|
return found_allergens, allergen_details |
|
|
|
@main.route('/') |
|
def index(): |
|
return jsonify({ |
|
"message": "Welcome to the Text Recognition and Sensitivity Checking Service", |
|
"endpoints": { |
|
"/api/ocr": "POST - Image analysis and sensitivity testing", |
|
}, |
|
"supported_formats": list(ALLOWED_EXTENSIONS), |
|
"known_allergens": list(KNOWN_ALLERGENS.keys()) |
|
}) |
|
|
|
@main.route('/api/ocr', methods=['POST']) |
|
def process_image(): |
|
|
|
if 'file' not in request.files: |
|
return jsonify({"error": "No file uploaded"}), 400 |
|
|
|
|
|
if 'allergens' not in request.form: |
|
return jsonify({"error": "Sensitivities not specified"}), 400 |
|
|
|
file = request.files['file'] |
|
if file.filename == '': |
|
return jsonify({"error": "No file selected"}), 400 |
|
|
|
|
|
if not allowed_file(file.filename): |
|
return jsonify({ |
|
"error": "File type not supported", |
|
"supported_formats": list(ALLOWED_EXTENSIONS) |
|
}), 400 |
|
|
|
|
|
user_allergens = request.form['allergens'].split(',') |
|
|
|
try: |
|
|
|
file_bytes = file.read() |
|
file_stream = io.BytesIO(file_bytes) |
|
|
|
|
|
extracted_text = ocr_model.process_image(file_stream) |
|
|
|
|
|
found_allergens, allergen_details = find_allergens(extracted_text, user_allergens) |
|
|
|
|
|
response = { |
|
"success": True, |
|
"extracted_text": extracted_text, |
|
"analysis": { |
|
"found_allergens": list(found_allergens), |
|
"allergen_details": allergen_details, |
|
"has_allergens": len(found_allergens) > 0, |
|
"warning": "Warning: Allergens found!" if found_allergens else "No allergens found" |
|
} |
|
} |
|
|
|
return jsonify(response) |
|
|
|
except Exception as e: |
|
return jsonify({ |
|
"error": "An error occurred while processing the image.", |
|
"details": str(e) |
|
}), 500 |
|
|
|
@main.route('/api/allergens', methods=['GET']) |
|
def get_known_allergens(): |
|
return jsonify({ |
|
"allergens": KNOWN_ALLERGENS |
|
}) |