sey / app.py
syd24's picture
Update app.py
a8115ae verified
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
from werkzeug.utils import secure_filename
import os
import face_recognition
import numpy as np
app = Flask(__name__)
CORS(app) # تمكين CORS للسماح بالطلبات من frontend
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# إنشاء مجلد uploads إذا لم يكن موجودًا
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
@app.route('/')
def home():
return send_from_directory('.', 'index.html')
@app.route('/api/search', methods=['POST'])
def search():
if 'image' not in request.files:
return jsonify({'success': False, 'message': 'لم يتم العثور على صورة'}), 400
file = request.files['image']
if file.filename == '':
return jsonify({'success': False, 'message': 'لم يتم اختيار ملف'}), 400
if file:
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
try:
# معالجة الصورة وإرجاع نتائج حقيقية
results = process_image(filepath)
return jsonify({'success': True, 'results': results})
except Exception as e:
return jsonify({'success': False, 'message': str(e)}), 500
return jsonify({'success': False, 'message': 'نوع ملف غير مسموح'}), 400
def process_image(image_path):
# تحميل الصورة
image = face_recognition.load_image_file(image_path)
# البحث عن الوجوه
face_locations = face_recognition.face_locations(image)
if not face_locations:
return []
# استخراج بصمة الوجه
face_encodings = face_recognition.face_encodings(image, face_locations)
target_encoding = face_encodings[0]
# مقارنة مع قاعدة بيانات وهمية (استبدلها بقاعدة بيانات حقيقية)
# مثال: مقارنة مع صور مخزنة مسبقًا
dummy_database = [
{'name': 'أحمد', 'encoding': np.array([...])}, # أضف encoding حقيقي
{'name': 'سارة', 'encoding': np.array([...])}
]
results = []
for person in dummy_database:
# حساب المسافة بين البصمات
distance = np.linalg.norm(target_encoding - person['encoding'])
if distance < 0.6: # threshold للتشابه
results.append({
'name': person['name'],
'confidence': round((1 - distance) * 100, 2)
})
return results
if __name__ == '__main__':
app.run(debug=True)
import gradio as gr
import requests
from PIL import Image
import numpy as np
def process_image(image):
# هنا ضع منطق معالجة الصورة (مثال: مقارنة الوجوه)
# مثال: إرجاع نتائج وهمية
return [
{"name": "أحمد", "source": "فيسبوك", "confidence": 85.5},
{"name": "سارة", "source": "تويتر", "confidence": 78.2}
]
# واجهة Gradio
demo = gr.Interface(
fn=process_image,
inputs=gr.Image(type="pil"),
outputs=gr.JSON(),
title="FaceCheck - Reverse Image Search",
description="Upload a photo to find matching profiles"
)
demo.launch()
<iframe
src="https://huggingface.co/spaces/syd24/sey"
width="100%"
height="600px"
frameborder="0"
></iframe>
# In app.py
@app.route('/')
def home():
return send_from_directory('.', 'index.html') # Serve index.html