|
|
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) |
|
|
UPLOAD_FOLDER = 'uploads' |
|
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER |
|
|
|
|
|
|
|
|
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([...])}, |
|
|
{'name': 'سارة', 'encoding': np.array([...])} |
|
|
] |
|
|
|
|
|
results = [] |
|
|
for person in dummy_database: |
|
|
|
|
|
distance = np.linalg.norm(target_encoding - person['encoding']) |
|
|
if distance < 0.6: |
|
|
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} |
|
|
] |
|
|
|
|
|
|
|
|
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> |
|
|
|
|
|
@app.route('/') |
|
|
def home(): |
|
|
return send_from_directory('.', 'index.html') |