File size: 5,578 Bytes
51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 51dad1c c30f358 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# routes.py
from flask import Blueprint, jsonify, request
import io
from app.utils import OCRModel
import logging
# إعداد التسجيل
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
main = Blueprint('main', __name__)
ocr_model = OCRModel()
# تحديد امتدادات الملفات المسموح بها
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
# قائمة الحساسيات المعروفة
KNOWN_ALLERGENS = {
'gluten': ['wheat', 'barley', 'rye', 'oats', 'gluten', 'flour', 'bread', 'pasta'],
'dairy': ['milk', 'yogurt', 'cheese', 'lactose', 'cream', 'butter', 'whey'],
'nuts': ['nuts', 'peanuts', 'almonds', 'walnuts', 'cashews', 'pistachios'],
'eggs': ['eggs', 'egg', 'albumin', 'mayonnaise'],
'soy': ['soy', 'soybeans', 'tofu', 'edamame'],
'fish': ['fish', 'salmon', 'tuna', 'cod', 'tilapia'],
'shellfish': ['shellfish', 'shrimp', 'crab', 'lobster', 'oyster', 'mussels'],
'sesame': ['sesame', 'tahini'],
'mustard': ['mustard'],
'celery': ['celery'],
'lupin': ['lupin'],
'sulfites': ['sulfites', 'sulphites']
}
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 = {}
allergen_locations = {}
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
# تخزين موقع الكلمة في النص
allergen_locations[allergen] = text.index(variant.lower())
elif allergen in text:
found_allergens.add(allergen)
allergen_details[allergen] = allergen
allergen_locations[allergen] = text.index(allergen)
return found_allergens, allergen_details, allergen_locations
@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",
"/api/allergens": "GET - List of known allergens"
},
"supported_formats": list(ALLOWED_EXTENSIONS),
"known_allergens": list(KNOWN_ALLERGENS.keys())
})
@main.route('/api/ocr', methods=['POST'])
def process_image():
try:
# التحقق من وجود الملف
if 'file' not in request.files:
logger.warning("No file uploaded")
return jsonify({"error": "No file uploaded"}), 400
# التحقق من وجود قائمة الحساسيات
if 'allergens' not in request.form:
logger.warning("Allergens not specified")
return jsonify({"error": "Allergens not specified"}), 400
file = request.files['file']
if file.filename == '':
logger.warning("No file selected")
return jsonify({"error": "No file selected"}), 400
# التحقق من نوع الملف
if not allowed_file(file.filename):
logger.warning(f"Invalid file type: {file.filename}")
return jsonify({
"error": "File type not supported",
"supported_formats": list(ALLOWED_EXTENSIONS)
}), 400
# تحضير قائمة الحساسيات
user_allergens = request.form['allergens'].split(',')
logger.info(f"Processing image for allergens: {user_allergens}")
# قراءة الصورة
file_bytes = file.read()
file_stream = io.BytesIO(file_bytes)
# معالجة الصورة
extracted_text = ocr_model.process_image(file_stream)
logger.info(f"Extracted text: {extracted_text}")
# البحث عن الحساسيات
found_allergens, allergen_details, allergen_locations = find_allergens(extracted_text, user_allergens)
# تحضير الرد
response = {
"success": True,
"extracted_text": extracted_text,
"analysis": {
"found_allergens": list(found_allergens),
"allergen_details": allergen_details,
"allergen_locations": allergen_locations,
"has_allergens": len(found_allergens) > 0,
"warning": "⚠️ Warning: Allergens found!" if found_allergens else "✅ No allergens found",
"severity": "high" if len(found_allergens) > 0 else "none"
}
}
logger.info(f"Analysis completed successfully: {found_allergens}")
return jsonify(response)
except Exception as e:
logger.error(f"Error processing request: {str(e)}", exc_info=True)
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,
"total_count": len(KNOWN_ALLERGENS),
"last_updated": "2024-03-24" # تحديث هذا التاريخ عند تحديث القائمة
}) |