יסודות בינה מלאכותית : 09-RB27
(לשיעור הבא נא לפתוח חשבון ב DISCORD על מנת ללמוד תוכנות ל A.I)
- עבדוה עם אפליקציה לעריכות וידאו – תרגיל כיתה
1.1 תרגיל כיתה
1.2.הצטלם על רקע לבן והחלקף את הדמות שלך בסרטון של ריקוד
1.3 העזר בתוכנות https://www.photopea.com/
2, עולה כסף אבל נראה בכל אופן
3. זיהוי פנים על ידי בינה מלאכותית
הרצת סקריפט בקוד פייתון התקנת סביבית עבדוה
3.1 התקנה : (תרגול בכיתה )
.1 3.1 פתח את אנקודמה כמו בשיעורים הקודמים
3.1.2 התקן הרץ בתוך הטרמינל את ההפקדות הבאות
3.1.3 הרץ את הפקודות הבאות אם יש שגיעות העזר ב CHATGPT לתקן אותם
1 2 3 4 5 6 7 8 9 10 11 |
python -m pip install --upgrade pip setuptools wheel pip install cmake pip install dlib pip install face_recognition pip install opencv-python pip install numpy |
3.2 בדיקה שהכל הותקן תקין
1 2 3 4 5 6 7 |
import face_recognition import cv2 import numpy as np print("face_recognition version:", face_recognition.__version__) print("OpenCV version:", cv2.__version__) print("NumPy version:", np.__version__) |
3.3 אימון הבינה המלאוכתית לזיהוי פנים הורדת תמונות והרצת קוד ניסיון :
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import face_recognition import cv2 import os import numpy as np # Step 1: Load known faces known_face_encodings = [] known_face_names = [] # Define the full path to the known people directory known_people_dir = r"D:\temp\faces" for person_name in os.listdir(known_people_dir): person_folder = os.path.join(known_people_dir, person_name) if not os.path.isdir(person_folder): continue # Skip non-folder files for image_name in os.listdir(person_folder): image_path = os.path.join(person_folder, image_name) if not image_name.lower().endswith(('.jpg', '.jpeg', '.png')): continue # Skip non-image files image = face_recognition.load_image_file(image_path) face_encodings = face_recognition.face_encodings(image) # Debug: Check if encoding exists if face_encodings: print(f"Face detected in {image_path}") known_face_encodings.append(face_encodings[0]) known_face_names.append(person_name) else: print(f"No face detected in {image_path}") # Step 2: Capture or load an image to recognize unknown_image_path = r"D:\temp\test\2.jpeg" # Replace with the path to your test image unknown_image = face_recognition.load_image_file(unknown_image_path) unknown_face_locations = face_recognition.face_locations(unknown_image) unknown_face_encodings = face_recognition.face_encodings(unknown_image, unknown_face_locations) # Convert the image to BGR color (for OpenCV) unknown_image_bgr = cv2.cvtColor(unknown_image, cv2.COLOR_RGB2BGR) # Debug: Check if faces are detected in the unknown image if not unknown_face_encodings: print("No faces detected in the unknown image.") cv2.imshow("Result", unknown_image_bgr) cv2.waitKey(0) cv2.destroyAllWindows() exit() # Step 3: Compare and label faces for (top, right, bottom, left), unknown_face in zip(unknown_face_locations, unknown_face_encodings): results = face_recognition.compare_faces(known_face_encodings, unknown_face) distances = face_recognition.face_distance(known_face_encodings, unknown_face) if any(results): best_match_index = np.argmin(distances) name = known_face_names[best_match_index] label = f"{name} ({distances[best_match_index]:.2f})" print(f"This is: {name}") else: name = "Unknown" label = "Unknown" print("This person is unknown.") # Draw a rectangle around the face cv2.rectangle(unknown_image_bgr, (left, top), (right, bottom), (0, 255, 0), 2) # Add a label below the face cv2.putText(unknown_image_bgr, label, (left, bottom + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) # Step 4: Display the result cv2.imshow("Face Recognition Result", unknown_image_bgr) cv2.waitKey(0) # Wait for a key press to close the window cv2.destroyAllWindows() |
3.4 תרגיל כיתה
3.5 תצלם את עצמך או דמות נוספת אחרת לפי בחירתך כ 15 תמנות
3.6 צור ספריה נוספת בספריית היעד
3.7 בדוק בתוך ספריית TEST עם תמונה חדשה שלך או של דמות היעד שהבינה מלאכותית מזהה
3.8 הורד סירטון וידאו או צורך סירטון וידאו משלך עם דמות יעד ובדוק את הקוד הבא שמזהה בסרטון וידאו
https://y2meta.tube/convert/?videoId=xiyYN2_JKPc
3.8.1 שמור את הוידאו כ D:\temp\test\v1.mp4
3.9 הרץ קוד בסרטון וידאו ליזהוי תמונה
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import face_recognition import cv2 import os import numpy as np # Step 1: Load known faces known_face_encodings = [] known_face_names = [] # Define the full path to the known people directory known_people_dir = r"D:\temp\faces" for person_name in os.listdir(known_people_dir): person_folder = os.path.join(known_people_dir, person_name) if not os.path.isdir(person_folder): continue # Skip non-folder files for image_name in os.listdir(person_folder): image_path = os.path.join(person_folder, image_name) if not image_name.lower().endswith(('.jpg', '.jpeg', '.png')): continue # Skip non-image files image = face_recognition.load_image_file(image_path) face_encodings = face_recognition.face_encodings(image) if face_encodings: known_face_encodings.append(face_encodings[0]) known_face_names.append(person_name) # Step 2: Process the video file video_path = r"D:\temp\test\v1.mp4" video_capture = cv2.VideoCapture(video_path) while video_capture.isOpened(): ret, frame = video_capture.read() if not ret: break # Exit the loop if no more frames are available # Convert the frame to RGB (face_recognition requires RGB format) rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Find all face locations and encodings in the current frame face_locations = face_recognition.face_locations(rgb_frame) face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) # Compare and label each face for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): matches = face_recognition.compare_faces(known_face_encodings, face_encoding) distances = face_recognition.face_distance(known_face_encodings, face_encoding) if any(matches): best_match_index = np.argmin(distances) name = known_face_names[best_match_index] label = f"{name} ({distances[best_match_index]:.2f})" else: name = "Unknown" label = "Unknown" # Draw a rectangle around the face cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) # Add a label below the face cv2.putText(frame, label, (left, bottom + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) # Display the frame with the labeled faces cv2.imshow("Video Face Recognition", frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the video capture and close all windows video_capture.release() cv2.destroyAllWindows() |