קורס יסודות בינה מלאכותית – RB23-05 – תרגול זיהוי אובייקטים 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 חובה לצפות בסרטון שלב אחר שלב ולהעזר ממנו בהתקנה
2.2 יש ראשית להיכנס לגוגל לחשבון האישי ואז להיכנס ל 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 בדוק את התמונות הבאת
