-
Notifications
You must be signed in to change notification settings - Fork 0
/
AI5.py
49 lines (37 loc) · 1.55 KB
/
AI5.py
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
import cv2
# تعليقات لشرح المراحل المختلفة في الكود
# تحميل ملف تعريف الوجه
face_detector = cv2.CascadeClassifier('myfacedetector.xml')
# فتح ملف الفيديو
cape = cv2.VideoCapture('Elon Musk.mp4')
# التحقق من نجاح فتح ملف الفيديو
if not cape.isOpened():
print("Could not open video file")
exit()
# إعداد متغيرات للون وحجم المربع المستخدم في تحديد الوجوه
color = (255, 0, 0) # اللون الأزرق
thickness = 5 # سمك الخط
# حلقة الفيديو
while True:
# قراءة الإطار التالي من الفيديو
ret, img = cape.read()
# التحقق من نجاح قراءة الإطار
if not ret:
print("Could not read frame")
break
# تحويل الإطار إلى grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# الكشف عن الوجوه في الإطار
faces = face_detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=4)
# رسم مربع حول كل وجه مكتشف
for (x,y,w,h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), color, thickness)
# عرض الإطار الحالي
cv2.imshow('Video', img)
# انتظر حتى يتم الضغط على ESC لإنهاء البرنامج
k = cv2.waitKey(30) & 0xff
if k == 27:
break
# إغلاق ملف الفيديو وإغلاق نافذة العرض
cape.release()
cv2.destroyAllWindows()