101 / models /download_models.py
midokhaled927's picture
Create models/download_models.py
9ceead5 verified
import os
import urllib.request
import zipfile
import gzip
import shutil
from pathlib import Path
def download_face_detection_model():
"""تحميل نموذج كشف الوجوه المصغر من MediaPipe"""
model_path = Path("models/face_detection.tflite")
model_path.parent.mkdir(parents=True, exist_ok=True)
if model_path.exists():
print("✅ نموذج كشف الوجوه موجود بالفعل")
return
print("📥 جاري تحميل نموذج كشف الوجوه...")
# استخدام نموذج MediaPipe BlazeFace
url = "https://storage.googleapis.com/mediapipe-models/face_detector/blaze_face_short_range/float32/1/face_detection_short_range.tflite"
try:
urllib.request.urlretrieve(url, model_path)
print(f"✅ تم التحميل: {model_path}")
except Exception as e:
print(f"❌ خطأ في التحميل: {e}")
# نموذج بديل
backup_url = "https://github.com/opencv/opencv_zoo/raw/main/models/face_detection_yunet/face_detection_yunet_2022mar.onnx"
print("🔄 محاولة تحميل نموذج بديل...")
urllib.request.urlretrieve(backup_url, model_path)
def download_face_recognition_model():
"""تحميل نموذج التعرف على الوجوه المصغر (MobileFaceNet)"""
model_path = Path("models/face_recognition.tflite")
model_path.parent.mkdir(parents=True, exist_ok=True)
if model_path.exists():
print("✅ نموذج التعرف على الوجوه موجود بالفعل")
return
print("📥 جاري تحميل نموذج التعرف على الوجوه...")
# نموذج MobileFaceNet مضغوط
url = "https://github.com/serengil/deepface_models/releases/download/v1.0/facenet_model.h5"
try:
urllib.request.urlretrieve(url, "models/face_recognition.h5")
print("⚠️ تم تحميل نموذج Keras، سيتم تحويله إلى TFLite...")
# تحويل إلى TFLite إذا كان TensorFlow متاحاً
try:
import tensorflow as tf
model = tf.keras.models.load_model("models/face_recognition.h5")
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open(model_path, 'wb') as f:
f.write(tflite_model)
os.remove("models/face_recognition.h5")
print(f"✅ تم التحويل والتحميل: {model_path}")
except:
print("⚠️ تعذر التحويل إلى TFLite، سيتم استخدام نموذج افتراضي")
except Exception as e:
print(f"❌ خطأ في التحميل: {e}")
print("📝 سيتم استخدام نموذج بسيط بديل")
def create_dummy_models():
"""إنشاء نماذج وهمية للتجربة"""
import numpy as np
# نموذج كشف بسيط
dummy_detection = np.random.rand(1, 128, 128, 3).astype(np.float32)
dummy_detection_path = Path("models/face_detection.tflite")
dummy_detection_path.parent.mkdir(parents=True, exist_ok=True)
if not dummy_detection_path.exists():
print("📝 إنشاء نموذج كشف تجريبي...")
dummy_detection.tofile(dummy_detection_path)
# نموذج تعرف بسيط
dummy_recognition = np.random.rand(1, 112, 112, 3).astype(np.float32)
dummy_recognition_path = Path("models/face_recognition.tflite")
if not dummy_recognition_path.exists():
print("📝 إنشاء نموذج تعرف تجريبي...")
dummy_recognition.tofile(dummy_recognition_path)
if __name__ == "__main__":
print("🚀 بدء تحميل النماذج...")
download_face_detection_model()
download_face_recognition_model()
# إذا فشل التحميل، استخدم نماذج تجريبية
if not Path("models/face_detection.tflite").exists() or \
not Path("models/face_recognition.tflite").exists():
print("⚠️ سيتم إنشاء نماذج تجريبية للتشغيل")
create_dummy_models()
print("✨ اكتمل إعداد النماذج")