קורס יסודות בינה מלאוכתית – תרגיל RB23-07t1 – בינה מלאכותית YOLO
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 |
{0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'} |
תרגיל כיתה נתרגל שימוש בבינה מלאוכתית בסביבת פיתוח colab בעזרת בינה מלאכותית YOLO
1. תרגיל כיתה בנה תוכנה עם בינהמלאכותית מסוג YOLO8 שיודעת לזהות רכבים בכביש
חומר לתרגיל – כתובת תמונה
https://robotronix.co.il/wp-content/uploads/2024/06/image-12.png
https://robotronix.co.il/wp-content/uploads/2024/06/image-13.png
2. ספור את כמות הרכבים בכביש
2.1 אם כמות הרכבים גדולה מ 10 כתוב high traffic
2.2 אם קטנה כתוב Low Traffic
פתרון :
https://colab.research.google.com/drive/1MDW3dee8TcuBmZLjbJohHhyUJopvi-HP?usp=sharing
- נכנס ל גימייל ומשם ל colab
- נתקין סביבת עבודה :
-
123456%pip install ultralyticsimport ultralyticsultralytics.checks()!pip install opencv-python-headlessprint (" **** done **** ")
-
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import cv2import requestsimport numpy as npfrom ultralytics import YOLOimport matplotlib.pyplot as plt# Define colorsblue = (255, 0, 0)green = (0, 255, 0)red = (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 modelmodel = YOLO('yolov8n.pt')# Download the image from the URLimage_url = 'https://robotronix.co.il/wp-content/uploads/2024/06/image-12.png'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 locallycv2.imwrite('3-300x199.jpg', image)# Run the model on the downloaded image with a confidence threshold of 0.54 without drawing boxesresults = model.predict(source='3-300x199.jpg', conf=0.14)# Function to draw bounding boxes and text with custom text sizedef 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 = boxlabel = f'{model.names[int(cls)]} {conf:.2f}'# Draw bounding boxcv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), blue, 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 textcv2.putText(image, label, (int(x1), int(y1) - 3), cv2.FONT_HERSHEY_SIMPLEX, 0.4, white, 1)# Display the original imageplt.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 itresult_image = image.copy()# Draw custom boxes and labelsdraw_custom_boxes(result_image, results, model)# Save the result imagecv2.imwrite('result_image.jpg', result_image)# Display the result image inlineplt.subplot(1, 2, 2)plt.title("Detection Results")plt.imshow(cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB))plt.axis('off')plt.show()
- מספר רכבים
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 |
import cv2 import requests import numpy as np import matplotlib.pyplot as plt from ultralytics import YOLO # Ensure you have the Ultralytics YOLO library installed # Define colors for bounding boxes and text colors = { "blue": (255, 0, 0), "green": (0, 255, 0), "red": (0, 0, 255), "white": (255, 255, 255) } # Load the YOLOv8 model model = YOLO('yolov8n.pt') # Ensure the model file is correctly located # Download the image from the URL image_url = 'https://robotronix.co.il/wp-content/uploads/2024/06/image-12.png' response = requests.get(image_url) image_array = np.asarray(bytearray(response.content), dtype=np.uint8) image = cv2.imdecode(image_array, cv2.IMREAD_COLOR) # Run the model on the image results = model.predict(image, conf=0.14) # Function to draw bounding boxes and count cars def draw_custom_boxes(image, results, model): car_count = 0 # Initialize car counter names = model.names # Access the class names from the model # Iterate through detections for result in results: boxes = result.boxes.data.cpu().numpy() for box in boxes: x1, y1, x2, y2, conf, cls_id = box[:6] label = f'{names[int(cls_id)]} {conf:.2f}' if names[int(cls_id)] == 'car': car_count += 1 color = colors['green'] else: color = colors['red'] cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), color, 2) cv2.putText(image, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, colors['white'], 2) return car_count # Copy the original image to draw boxes on it result_image = image.copy() # Draw custom boxes and count cars car_count = draw_custom_boxes(result_image, results, model) # Assess traffic based on the number of cars detected traffic_status = "high traffic" if car_count >= 10 else "low traffic" # Display the images 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') plt.subplot(1, 2, 2) plt.title("Detection Results") plt.imshow(cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)) plt.axis('off') plt.show() # Print the number of cars and traffic status print(f"Total cars found: {car_count}") print(traffic_status) |
https://colab.research.google.com/drive/1MDW3dee8TcuBmZLjbJohHhyUJopvi-HP?usp=sharing
6.נכתוב קוד בעזרת בינה מלאוכתית CHATGPT כמה מכוניות יש בכביש
תרגיל 2 זיהוי דברים בשמים
https://colab.research.google.com/drive/1NSUIreJljQ5Y2ZuBtHPk-Z_jQKPaK-LO?usp=sharing