midokhaled927 commited on
Commit
9ceead5
·
verified ·
1 Parent(s): 0a0438c

Create models/download_models.py

Browse files
Files changed (1) hide show
  1. models/download_models.py +99 -0
models/download_models.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import urllib.request
3
+ import zipfile
4
+ import gzip
5
+ import shutil
6
+ from pathlib import Path
7
+
8
+ def download_face_detection_model():
9
+ """تحميل نموذج كشف الوجوه المصغر من MediaPipe"""
10
+ model_path = Path("models/face_detection.tflite")
11
+ model_path.parent.mkdir(parents=True, exist_ok=True)
12
+
13
+ if model_path.exists():
14
+ print("✅ نموذج كشف الوجوه موجود بالفعل")
15
+ return
16
+
17
+ print("📥 جاري تحميل نموذج كشف الوجوه...")
18
+ # استخدام نموذج MediaPipe BlazeFace
19
+ url = "https://storage.googleapis.com/mediapipe-models/face_detector/blaze_face_short_range/float32/1/face_detection_short_range.tflite"
20
+
21
+ try:
22
+ urllib.request.urlretrieve(url, model_path)
23
+ print(f"✅ تم التحميل: {model_path}")
24
+ except Exception as e:
25
+ print(f"❌ خطأ في التحميل: {e}")
26
+ # نموذج بديل
27
+ backup_url = "https://github.com/opencv/opencv_zoo/raw/main/models/face_detection_yunet/face_detection_yunet_2022mar.onnx"
28
+ print("🔄 محاولة تحميل نموذج بديل...")
29
+ urllib.request.urlretrieve(backup_url, model_path)
30
+
31
+ def download_face_recognition_model():
32
+ """تحميل نموذج التعرف على الوجوه المصغر (MobileFaceNet)"""
33
+ model_path = Path("models/face_recognition.tflite")
34
+ model_path.parent.mkdir(parents=True, exist_ok=True)
35
+
36
+ if model_path.exists():
37
+ print("✅ نموذج التعرف على الوجوه موجود بالفعل")
38
+ return
39
+
40
+ print("📥 جاري تحميل نموذج التعرف على الوجوه...")
41
+ # نموذج MobileFaceNet مضغوط
42
+ url = "https://github.com/serengil/deepface_models/releases/download/v1.0/facenet_model.h5"
43
+
44
+ try:
45
+ urllib.request.urlretrieve(url, "models/face_recognition.h5")
46
+ print("⚠️ تم تحميل نموذج Keras، سيتم تحويله إلى TFLite...")
47
+
48
+ # تحويل إلى TFLite إذا كان TensorFlow متاحاً
49
+ try:
50
+ import tensorflow as tf
51
+ model = tf.keras.models.load_model("models/face_recognition.h5")
52
+ converter = tf.lite.TFLiteConverter.from_keras_model(model)
53
+ tflite_model = converter.convert()
54
+
55
+ with open(model_path, 'wb') as f:
56
+ f.write(tflite_model)
57
+
58
+ os.remove("models/face_recognition.h5")
59
+ print(f"✅ تم التحويل والتحميل: {model_path}")
60
+ except:
61
+ print("⚠️ تعذر التحويل إلى TFLite، سيتم استخدام نموذج افتراضي")
62
+
63
+ except Exception as e:
64
+ print(f"❌ خطأ في التحميل: {e}")
65
+ print("📝 سيتم استخدام نموذج بسيط بديل")
66
+
67
+ def create_dummy_models():
68
+ """إنشاء نماذج وهمية للتجربة"""
69
+ import numpy as np
70
+
71
+ # نموذج كشف بسيط
72
+ dummy_detection = np.random.rand(1, 128, 128, 3).astype(np.float32)
73
+ dummy_detection_path = Path("models/face_detection.tflite")
74
+ dummy_detection_path.parent.mkdir(parents=True, exist_ok=True)
75
+
76
+ if not dummy_detection_path.exists():
77
+ print("📝 إنشاء نموذج كشف تجريبي...")
78
+ dummy_detection.tofile(dummy_detection_path)
79
+
80
+ # نموذج تعرف بسيط
81
+ dummy_recognition = np.random.rand(1, 112, 112, 3).astype(np.float32)
82
+ dummy_recognition_path = Path("models/face_recognition.tflite")
83
+
84
+ if not dummy_recognition_path.exists():
85
+ print("📝 إنشاء نموذج تعرف تجريبي...")
86
+ dummy_recognition.tofile(dummy_recognition_path)
87
+
88
+ if __name__ == "__main__":
89
+ print("🚀 بدء تحميل النماذج...")
90
+ download_face_detection_model()
91
+ download_face_recognition_model()
92
+
93
+ # إذا فشل التحميل، استخدم نماذج تجريبية
94
+ if not Path("models/face_detection.tflite").exists() or \
95
+ not Path("models/face_recognition.tflite").exists():
96
+ print("⚠️ سيتم إنشاء نماذج تجريبية للتشغيل")
97
+ create_dummy_models()
98
+
99
+ print("✨ اكتمل إعداد النماذج")