יסודות בינה מלאכותית : RB27 – מבוא ל YOLO – זיהוי אובייקטים
אז מה נילמד בקורס …..
מטוס קרב עם בינה מלאכותית
מטוס קרב בינה מלאכותית מול אמיתי מי מנצח
1. מבוא לשפת פייתון – שפה פופלארית לפיתוח – בינה מלאוכתית
פייתון (באנגלית: Python) היא שפת תכנות עילית דינמית למטרות כלליות מהנפוצות ביותר, ומדורגת באופן עקבי כאחת משפות התכנות הפופולריות ביותר[2][3]. פייתון תוכננה תוך שימת דגש על קריאוּת הקוד, וכוללת מבנים המיועדים לאפשר ביטוי של תוכניות מורכבות בדרך קצרה וברורה. אחד המאפיינים הבולטים בתחביר השפה הוא השימוש בהזחה להגדרת בלוקים של קוד (ללא שימוש בסוגריים או במילים שמורות לצורך כך, כמו ברוב השפות הנפוצות).
https://he.wikipedia.org/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F
1.1 סביבת הפיתוח
https://colab.research.google.com/
1.1.1 ניתן לפתח גם עם – https://chatgpt.com/
- טבלאות אמת :
הבנה כללית של קוד פיטון עבור הרצה של בינה מלאכותית .
משפטי תנאי :
1 2 3 |
age = 8 if age > 7: print("You are older than 7!") |
1 2 3 |
height = 100 if height < 120: print("You are shorter than 120 cm!") |
1 2 3 4 5 |
day = "Monday" if day == "Sunday": print("It's a weekend!") else: print("It's a weekday!") |
1 2 3 |
color = "blue" if color == "red" or color == "blue": print("You chose a primary color!") |
1 2 3 4 |
age = 10 height = 130 if age > 5 and height > 120: print("You can go on the ride!") |
1 2 3 |
fruit = "apple" if fruit != "banana": print("This is not a banana!") |
1 2 3 4 5 6 7 |
number = 3 if number > 5: print("Number is greater than 5!") elif number == 5: print("Number is exactly 5!") else: print("Number is less than 5!") |
1 2 3 4 |
x = 5 y = 10 if x < 10 and y > 5: print("x is less than 10 and y is greater than 5!") |
1 2 3 |
score = 85 if score > 90 or (score > 80 and not score == 100): print("Great job on your score!") |
string
Python, strings are zero-indexed, which means the first character of a string is at index 0
, the second character is at index 1
, and so on
1 2 3 |
text = "Python" print(text[0]) # Output: P (the first character) print(text[1]) # Output: y (the second character) |
הדפסה של על התווים ניראה בהמשלך לולאות
1 2 3 4 5 6 7 |
text = "Python" length = len(text) # Get the length of the string print(f"The length of the string is: {length}\n") for i in range(length): print(f"Character at position {i}: {text[i]}") |
כתיבה יותר מקוצרת אבל פחות " קלה "
1 2 3 4 |
text = "Python" for i in range(len(text)): print(text[i]) |
1 2 3 4 5 6 |
text = "Hello, welcome to Python programming!" if "Python" in text: print("The word 'Python' is in the text!") else: print("The word 'Python' is not in the text.") |
Output: The word 'Python' is in the text!
1 2 3 4 5 6 |
text = "Learning Python is fun!" if text.find("Python") != -1: print("Found the word 'Python' in the text!") else: print("Couldn't find the word 'Python' in the text.") |
תרגיל כיתה 1
- לחץ על הקישור הבא והרץ את התוכנית :
https://colab.research.google.com/drive/1U9o4Ucnq3kesvtMz4rxVgS0XXhXCoh_L?usp=sharing
2. העתק והרץ את התוכנית הבאה מה היא עושה
1 2 3 4 5 6 |
import random # Import the module that allows random number generation # Generate 5 random numbers between 1 and 10 and print them for i in range(5): random_number = random.randint(1, 10) # Generate a random number between 1 and 10 print(f"Random number {i+1}: {random_number}") |
4. קלט : Time Check
1 2 3 4 5 6 7 |
hour = int(input("Enter the hour (0-23): ")) if 6 <= hour < 12: print("Good morning!") elif 12 <= hour < 18: print("Good afternoon!") else: print("Good evening!") |
4. תרגול לולאות : בדיקת כלי טיס
1 2 3 4 5 6 |
vehicles = ["bird", "kite", "uav", "drone", "plane", "car"] for vehicle in vehicles: if vehicle in ["bird", "kite", "uav", "drone", "plane"]: print(f"{vehicle} is an aerial vehicle") else: print(f"{vehicle} is not an aerial vehicle") |
4.1. בדיקת פירות
1 2 3 4 5 6 |
items = ["apple", "banana", "carrot", "orange", "potato"] for item in items: if item in ["apple", "banana", "orange"]: print(f"{item} is a fruit") else: print(f"{item} is not a fruit") |
4.2. בדיקת ציפורים
1 2 3 4 |
items = ["bird", "kite", "uav", "drone", "plane"] for item in items: if item == "bird": print(f"{item} is a bird") |
4.3 בדיקת ציפורים בתוך רשימה מעורבת
1 2 3 4 5 6 7 8 |
results = ["bird", "kite", "uav", "drone", "plane", "moon", "car"] filtered_results = [] for result in results: if result == "uav": print("UAV found in sky") filtered_results.append(result) print("Filtered results:", filtered_results) |
4.4 בדיקת ציפורים בתוך רשימה מעורבת
1 2 3 4 5 6 7 |
results = ["bird", "kite", "uav", "drone", "plane", "moon", "car"] filtered_results = [] for result in results: if result != "bird" and result == "moon": filtered_results.append(result) print("Items that are not birds and are moons:", filtered_results) |
4.5 בדיקת ציפורים בתוך רשימה מעורבת
1 2 3 4 5 6 7 |
results = ["bird", "kite", "uav", "drone", "plane", "moon", "car"] filtered_results = [] for result in results: if result not in ["bird", "kite", "plane", "moon"]: filtered_results.append(result) print("Items different from bird, kite, plane, and moon:", filtered_results) |
5. תרגול ספירה
1 2 3 4 5 6 7 |
results = ["bird", "kite", "uav", "drone", "plane", "moon", "car", "uav"] count_uav = 0 for result in results: if result == "uav": count_uav += 1 print(f"Number of UAVs found: {count_uav}") |
5.1 הוספה למערך
1 2 3 4 5 6 7 8 9 10 |
results = ["bird", "kite", "uav", "drone", "plane", "moon", "car", "uav"] count_uav = 0 uav_list = [] for result in results: if result == "uav": count_uav += 1 uav_list.append(result) print(f"Number of UAVs found: {count_uav}") print("List of UAVs:", uav_list) |
https://colab.research.google.com/gist/RoboWild/19deff31306d95d2e315a9e1d331f156/yolo-train.ipynb
4 . זיהוי אובייקטים YOLO
YOLO (ראשי תיבות של You Only Look Once) הוא מודל בינה מלאכותית שיודע לזהות ולהבין מה יש בתמונה במהירות גבוהה מאוד.
דמיינו שיש לכם רובוט שמסתכל על תמונה ורואה בה אוטו, חתול ואיש – הוא לא רק יודע שהם שם, אלא גם איפה בדיוק הם נמצאים בתוך התמונה.
YOLO עובד בצורה חכמה – הוא מסתכל על כל התמונה בפעם אחת, מזהה את כל האובייקטים החשובים ומקיף אותם במסגרת. בגלל זה הוא מהיר מאוד ומתאים למכוניות חכמות, מצלמות אבטחה ומשחקים.
שימושים
תעופה: YOLO
- עוזר למטוסים ורחפנים לזהות מכשולים באוויר ולנווט בבטחה.
- דוגמה: רחפן מזהה ציפורים ומתאים את מסלול הטיסה כדי למנוע התנגשות.
רפואה:
- מזהה מחלות בתמונות רנטגן או MRI במהירות.
- דוגמה: רופא משתמש ב-YOLO כדי למצוא גידול בריאות מתוך אלפי סריקות.
צבא:
- מזהה כלי רכב חשודים, חיילים או רחפנים בשדה הקרב.
- דוגמה: מערכת הגנה מזהה בזמן אמת טיל מתקרב ומפעילה יירוט אוטומטי.
תנועה:
- מזהה כלי רכב, רמזורים והולכי רגל לשיפור הבטיחות בדרכים.
- דוגמה: מצלמה חכמה מזהה רכב שחוצה צומת באור אדום ושולחת דו"ח.
יש כמה מודלים מתחרים ל-YOLO בתחום זיהוי אובייקטים בזמן אמת:
- SSD (Single Shot Detector) – כמו YOLO, מסתכל על כל התמונה בפעם אחת, אבל מחלק אותה לרשת קטנה יותר. מהיר אך פחות מדויק בקטנים.
- 📌 דוגמה: משמש במצלמות חכמות לזיהוי פנים.
- Faster R-CNN – מזהה אובייקטים בדיוק גבוה מאוד, אבל איטי יותר כי הוא מחפש אזורים חשובים לפני הזיהוי.
- 📌 דוגמה: משמש ברפואה כדי לזהות תאים סרטניים בתמונות מיקרוסקופיות.
- EfficientDet – יותר יעיל ופחות כבד, מספק איזון בין דיוק למהירות.
- 📌 דוגמה: בשימוש ברחפנים לזיהוי מטרות צבאיות.
- DETR (DEtection TRansformer) – משתמש במודלי Transformer (כמו ב-ChatGPT) כדי לזהות אובייקטים בדיוק רב, אך לא מהיר כמו YOLO.
- 📌 דוגמה: בשימוש במערכות חכמות לנהיגה אוטונומית.
YOLO עדיין מוביל במהירות, אך במקרים שדורשים דיוק גבוה במיוחד, משתמשים גם במודלים אחרים
1.נזכיר YOLO 8 יש מודל דוגמא – שיוע לזהות כ 80 מחלקות כלליות שהם ברורות
1.1 רשימת מחלקות
Person |
Bicycle |
Car |
Motorcycle |
Airplane |
Bus |
Train |
Truck |
Boat |
Traffic light |
Fire hydrant |
Stop sign |
Parking meter |
Bench |
Bird |
Cat |
Dog |
Horse |
Sheep |
Cow |
Elephant |
Bear |
Zebra |
Giraffe |
Backpack |
Umbrella |
Handbag |
Tie |
Suitcase |
Frisbee |
Skis |
Snowboard |
Sports ball |
Kite |
Baseball bat |
Baseball glove |
Skateboard |
Surfboard |
Tennis racket |
Bottle |
Wine glass |
Cup |
Fork |
Knife |
Spoon |
Bowl |
Banana |
Apple |
Sandwich |
Orange |
Broccoli |
Carrot |
Hot dog |
Pizza |
Donut |
Cake |
Chair |
Couch |
Potted plant |
Bed |
Dining table |
Toilet |
TV |
Laptop |
Mouse |
Remote |
Keyboard |
Cell phone |
Microwave |
Oven |
Toaster |
Sink |
Refrigerator |
Book |
Clock |
Vase |
Scissors |
Teddy bear |
Hair drier |
Toothbrush |
2. התקן את הבינה המלאכותית לזיהוי אובייקטים מסוג YOLO 8 לפי ההוראות הבאות
2.1 יש ראשית להיכנס לגוגל לחשבון האישי ואז להיכנס ל COLAB (בסיום ההרצאה יש להתנתק מחשבון GOOGLE )
2.2.2 קישור
https://colab.research.google.com/drive/1wfPhYH4xOMdmW3O730b42bNvPnHxuGwI?usp=sharing
2.3 התקנה של YOLO
1 2 3 4 5 6 |
%pip install ultralytics import ultralytics ultralytics.checks() !pip install opencv-python-headless print (" **** done **** ") |
2.4 יצירת ספריות
1 2 3 4 5 6 7 8 |
import os current_path = os.getcwd() print("Current Path:", current_path) # Create the directories using shell commands !mkdir -p download/images !mkdir -p download/movies print("Directories download/images and download/movies created successfully.") |
2.5
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 |
import cv2 import requests import numpy as np from ultralytics import YOLO import matplotlib.pyplot as plt # Define colors red = (255, 0, 0) green = (0, 255, 0) blue = (0, 0, 255) cyan = (0, 255, 255) magenta = (255, 0, 255) yellow = (255, 255, 0) maroon = (128, 0, 0) dark_green = (0, 128, 0) navy = (0, 0, 128) olive = (128, 128, 0) purple = (128, 0, 128) teal = (0, 128, 128) white = (255, 255, 255) black = (0, 0, 0) # Load the YOLOv8 model model = YOLO('yolov8n.pt') # Download the image from the URL image_url = 'https://robotronix.co.il/wp-content/uploads/2024/05/3-300x199.jpg' response = requests.get(image_url) image_array = np.asarray(bytearray(response.content), dtype=np.uint8) image = cv2.imdecode(image_array, cv2.IMREAD_COLOR) # Save the image locally cv2.imwrite('download/images/3-300x199.jpg', image) # Run the model on the downloaded image with a confidence threshold of 0.54 without drawing boxes results = model.predict(source='download/images/3-300x199.jpg', conf=0.54) # Function to draw bounding boxes and text with custom text size def draw_custom_boxes(image, results, model): for result in results: boxes = result.boxes.data.cpu().numpy() for box in boxes: x1, y1, x2, y2, conf, cls = box label = f'{model.names[int(cls)]} {conf:.2f}' # Draw bounding box cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), red, 2) # Draw label background (w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.4, 1) cv2.rectangle(image, (int(x1), int(y1) - h - 3), (int(x1) + w, int(y1)),red, -1) # Draw label text cv2.putText(image, label, (int(x1), int(y1) - 3), cv2.FONT_HERSHEY_SIMPLEX, 0.4, white, 1) # Display the original image plt.figure(figsize=(14, 7)) plt.subplot(1, 2, 1) plt.title("Original Image") plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) plt.axis('off') # Copy the original image to draw boxes on it result_image = image.copy() # Draw custom boxes and labels draw_custom_boxes(result_image, results, model) # Save the result image cv2.imwrite('result_image.jpg', result_image) # Display the result image inline plt.subplot(1, 2, 2) plt.title("Detection Results") plt.imshow(cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)) plt.axis('off') plt.show() |
3 בדוק את התמונות הבאת

העשרה :