יסודות בינה מלאכותית : 05-RB29 – יסודות בינה מלאכותית TEXT TO VOICE
פייתון לימוד : המשך הכנה להרצה והבנה קוד בינה מלאכותית
הרץ את הדומאות הבאות בסבבית קולאס – המצרה יעבור שורה שורה
1 2 3 4 5 6 |
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] even_numbers = [] for num in numbers: if num % 2 == 0: even_numbers.append(num) print("Even numbers:", even_numbers) |
1 2 3 |
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] even_numbers = [num for num in numbers if num % 2 == 0] print("Even numbers (comprehension):", even_numbers) |
1 2 3 4 |
names = ["Alice", "Bob", "Charlie", "David"] for name in names: if "a" in name.lower(): print(name, "contains the letter 'a'") |
1 2 3 4 5 6 |
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] count = 0 for num in numbers: if num > 5: count += 1 print("Count of numbers greater than 5:", count) |
1 2 3 4 5 6 7 8 |
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] modified_numbers = [] for num in numbers: if num % 2 == 0: modified_numbers.append(num * 10) # להכפיל מספר זוגי ב-10 else: modified_numbers.append(num) # להשאיר מספר אי-זוגי ללא שינוי print("Modified numbers:", modified_numbers) |
יותר מתקדמים
1 2 3 4 5 6 7 8 9 |
# Create an array of detected objects (simulated YOLOv8 output) detected_objects = ["person", "chair", "table", "computer", "bottle", "chair", "laptop"] # Loop over the array and check if the object is either a computer or a chair for obj in detected_objects: if obj == "computer" or obj == "chair": print(f"The object '{obj}' fits the criteria (computer or chair).") else: print(f"The object '{obj}' does not fit the criteria (not a computer or chair).") |
הורדת קובץ תמונה מהאינטרנט והצגה במחשב המקומי
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 |
# Import required libraries import requests # להורדת התמונה מהאינטרנט import numpy as np # לעבודה עם מערכים import cv2 # לעיבוד תמונה import matplotlib.pyplot as plt # להצגת התמונה # הגדרת כתובת התמונה img_url = "https://officebanao.com/wp-content/uploads/2024/02/3d-rendering-business-meeting-room-1024x682.webp" # הורדת התמונה מהאינטרנט print("Downloading image...") response = requests.get(img_url) if response.status_code == 200: # המרת התוכן למערך של מספרים img_bytes = np.asarray(bytearray(response.content), dtype=np.uint8) # דיקוד התמונה באמצעות OpenCV img = cv2.imdecode(img_bytes, cv2.IMREAD_COLOR) # המרת צבעים מ-BGR ל-RGB לצורך הצגה נכונה img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) print("Image downloaded successfully!") else: print("Error downloading the image.") raise Exception("Image download failed.") # הצגת התמונה באמצעות matplotlib plt.figure(figsize=(10, 6)) plt.imshow(img_rgb) plt.axis('off') plt.title("Image from URL") plt.show() |
תרגיל כיתה 1: שימוש ב YOLO8 זיהוי עצים על ידי בינה מלאכותית
המטרה
1.הרץ את הקוד הבא בקולאב
התקנת ספריות לקוד
1 2 |
# שלב 1: התקנת חבילת ultralytics (כוללת את YOLOv8) !pip install ultralytics |
1.1 העתק והדבק
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 76 77 78 79 80 81 82 83 84 |
# Step 1: Install the YOLOv8 package from ultralytics !pip install ultralytics # Step 2: Import required libraries import requests # To download the image from the internet import cv2 # To work with images import numpy as np # For numerical operations import pandas as pd # For creating tables from ultralytics import YOLO # To use the YOLOv8 model import matplotlib.pyplot as plt # To show images # Step 3: Define the image URL img_url = "https://officebanao.com/wp-content/uploads/2024/02/3d-rendering-business-meeting-room-1024x682.webp" # Step 4: Download the image from the internet print("Downloading image...") response = requests.get(img_url) if response.status_code == 200: # Convert downloaded bytes into a NumPy array img_bytes = np.asarray(bytearray(response.content), dtype=np.uint8) # Decode the image using OpenCV img = cv2.imdecode(img_bytes, cv2.IMREAD_COLOR) # Convert the image from BGR to RGB (for correct colors when displaying) img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) print("Image downloaded successfully!") else: print("Error downloading the image.") raise Exception("Image download failed.") # Step 5: Load the YOLOv8 large model print("\nLoading YOLOv8 model...") model = YOLO('yolov8l.pt') print("Model loaded!") # Step 6: Print all objects that the model knows (before detection) print("\nList of all objects (classes) known by the model:") all_objects = model.model.names # A dictionary mapping class index to object name for index, name in all_objects.items(): print(index, ":", name) # Print the total number of objects (classes) the model knows total_classes = len(all_objects) print("\nTotal number of object classes the model knows:", total_classes) # Step 7: Run object detection on the image print("\nRunning detection on the image...") results = model.predict(img_rgb) print("Detection complete!") # Step 8: Print all detected objects and count them # Get list of detected class indices from the results if results[0].boxes.cls.is_cuda: detected_classes = results[0].boxes.cls.cpu().numpy() else: detected_classes = results[0].boxes.cls.numpy() # Convert class indices to names using our mapping (all_objects) detected_objects = [all_objects[int(cls)] for cls in detected_classes] print("\nDetected objects:") for obj in detected_objects: print(obj) # Count the number of times each object was detected object_counts = {} for obj in detected_objects: object_counts[obj] = object_counts.get(obj, 0) + 1 # Create a summary table using a pandas DataFrame df_summary = pd.DataFrame(list(object_counts.items()), columns=["Object", "Count"]) print("\nSummary Table of Detected Objects:") print(df_summary) # Print the total number of detected objects total_detected = len(detected_objects) print("\nTotal number of objects detected:", total_detected) # Step 9: Display the image with detection results (bounding boxes) annotated_img = results[0].plot() # This returns the image with drawn bounding boxes in RGB format plt.figure(figsize=(10, 6)) plt.imshow(annotated_img) plt.axis('off') plt.title("YOLOv8 Detection Results") plt.show() |
1.2 צורך את אותה תוכנה בעצמך ב CHATGPT זכור לבקש את סביבית COLAB YOLO8 וסיכום כמות האובייקטים
התשמש בקישור הבא
https://colab.research.google.com/drive/1X4us-VCzs-FBeqRPB2MHEmqUHoaGlC_k?usp=sharing
תרגיל כיתה 2 (ללא עזרה מהמרצה ב 7 דקות הראשונות )
היכנס עם GMAIL קרא את התרגיל עד הסוף ורק אז השב עליו .
- צור בעזרת בינה מלאוכתית קוד בשפת פייתון המקבלת קלט בטקסט בשפה העברית ויוצרת קול בסביבת COLAB כאשר לוחצים על כפתור המר , התוכנית שומרת את ההמרה לתוך קוב. MP3 ומורידה אותו למחשב המקומי
- איזה סיפריות נידרשות להתקנה ב colab בקש הספר מהבנה המלאוכתי להתקנה ומה תפיקד כל ספריה
הקלד את הטסקס הבא "
3 בקש הסבר מפורט על כל שורת קוד
3.1 יצירית סיפור קצר ודיבוב הסריט בעזרת בינה מלאכותית
צוק קבצי קול לכל אחד מהמקטעים הבאים :
קטע 1: ליאורה והפחד
ליאורה הייתה ילדה קטנה, מתוקה וסקרנית, אך כל לילה, כשהחושך ירד, היא חשה פחד גדול. החדר שלה הפך לממלכה מסתורית וחשוכה, והיא חששה מכל צל ורשרוש. היא לא הבינה שבלילה מסתתר גם קסם, אך כל מה שראתה היה רק חושך.
קטע 2: הלילה שבו נפתח הסוד
יום אחד, אמה שמה לה לבשה שמיכה צבעונית והדליקה נורה קטנה שהפכה את החדר לאור עדין. ליאורה התיישבה מול החלון, שם היא הבחינה בכוכבים מנצנצים בשמיים. האור הקטן והכוכבים סיפרו לה סיפור של אור בתוך החושך, והבינו שלפעמים, כדי למצוא את הקסם, צריך רק להסתכל בעיניים אחרות.
קטע 3: ההתמודדות והניצחון
לאט לאט, ליאורה למדה שהחושך אינו אויב, אלא רק חלק מהיום. היא החלה לראות את החשיכה כקרקע להופעת כוכבים ואפשרות לחלום. כך, בכל לילה, היא הדליקה את הנורה הקטנה שלה והרגישה בטוחה ונינוחה, כשהיא יודעת שהאור שבפנים שלה תמיד ינצח את החשיכה.
פתרון
https://colab.research.google.com/drive/1oP0CjvFmk6FJDLPXnh8w8-vXsTiJfG8n?usp=sharing
1 2 3 4 |
# התקנת הספריות הדרושות של הבינה מלאכותית (אם לא מותקנות) !pip install gTTS !pip install ipywidgets # מתקינים פעם אחד בלבד ! |
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 |
import ipywidgets as widgets from IPython.display import display, clear_output from gtts import gTTS from google.colab import files def generate_tts(text, lang='iw', slow=False, tld='com', filename='output.mp3'): """יוצר קובץ קול מהטקסט הנתון""" tts = gTTS(text, lang=lang, slow=slow, tld=tld) tts.save(filename) return filename def on_button_clicked(b): with output: clear_output() user_text = text_area.value if not user_text.strip(): print("נא להזין טקסט") return filename = generate_tts(user_text) print("הקובץ נוצר בהצלחה. מתחילים בהורדה...") files.download(filename) # יצירת תיבת טקסט להדבקת הטקסט text_area = widgets.Textarea( value='', placeholder='הדבק את הטקסט כאן...', description='טקסט:', disabled=False, layout=widgets.Layout(width='100%', height='150px') ) # יצירת כפתור ליצירת הקול button = widgets.Button( description='צור טקסט', disabled=False, button_style='info', tooltip='לחץ כאן ליצירת קול', icon='play' ) # יצירת אזור פלט להצגת הודעות output = widgets.Output() button.on_click(on_button_clicked) display(text_area, button, output) |
שימוש ב CANVA
תרגיל כיתה 3 : יצירת סרטון וידאו בתוכנת CANVA ושימו בבינה מלאוכתית
יצירת אנמיציה ב CANVA