בינה מלאכותית : אימון yolo8 וטיפים
נתייחס לעבודה שאנחנו עובדים בוירטואלי דרך אנקודה נשלב בין CMD לבין NOTBOOK בכתיבת קוד פייטון
התקנו סביבה של YOLO8 בוירטואל
זיהוי אובייקט דוגמאות
yolo task=detect mode=predict model=yolov8n.pt source=D:/temp5/images/1.jpg
תוצאה תיהיה
מעל 60 אחוז
yolo task=detect mode=predict model=yolov8n.pt source=D:/temp5/images/1.jpg conf=0.6
הרצת קוד מקביל לפקודה ב CMD
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 |
from ultralytics import YOLO import cv2 # Load the YOLOv8 model model = YOLO('yolov8m.pt') # Path to the image image_path = 'D:/temp5/images/1.jpg' # Perform inference on the image results = model(image_path) # Extract information from the results detections = results[0].boxes.xyxy.cpu().numpy() # bounding box coordinates confidences = results[0].boxes.conf.cpu().numpy() # confidence scores class_ids = results[0].boxes.cls.cpu().numpy() # class ids # Load the image image = cv2.imread(image_path) # Iterate over detections and draw bounding boxes for 'person' class for detection, confidence, class_id in zip(detections, confidences, class_ids): x1, y1, x2, y2 = detection if class_id == 0: # class_id 0 corresponds to 'person' in COCO dataset label = f'Person: {confidence:.2f}' color = (0, 0, 255) # Red color in BGR (since OpenCV uses BGR format) 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, color, 2) # Display the image with bounding boxes in a window cv2.imshow('YOLOv8 Detection', image) # Wait for the user to press 'ESC' or 'Q' to close the window while True: key = cv2.waitKey(1) & 0xFF if key == 27 or key == ord('q'): # 27 is the ESC key break # Close the window cv2.destroyAllWindows() |
הרצת על סרטון
(yolov8_env) C:\Users\dev66>yolo task=detect mode=predict model=yolov8m-pose.pt source=D:/temp5/mov_1.mp4
פלט ריצה
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 |
from ultralytics import YOLO import cv2 # Load the YOLOv8 model model = YOLO('yolov8m.pt') # Path to the video video_path = 'D:/temp5/mov_1s.mp4' # Open the video file cap = cv2.VideoCapture(video_path) # Check if the video was opened successfully if not cap.isOpened(): print("Error: Could not open video.") exit() # Process the video frame by frame while cap.isOpened(): ret, frame = cap.read() if not ret: break # Perform inference on the frame results = model(frame) # Extract information from the results detections = results[0].boxes.xyxy.cpu().numpy() # bounding box coordinates confidences = results[0].boxes.conf.cpu().numpy() # confidence scores class_ids = results[0].boxes.cls.cpu().numpy() # class ids # Iterate over detections and draw bounding boxes for 'person' class for detection, confidence, class_id in zip(detections, confidences, class_ids): x1, y1, x2, y2 = detection if class_id == 0: # class_id 0 corresponds to 'person' in COCO dataset label = f'Person: {confidence:.2f}' color = (0, 0, 255) # Red color in BGR (since OpenCV uses BGR format) cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), color, 2) cv2.putText(frame, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) # Display the frame with bounding boxes in a window cv2.imshow('YOLOv8 Detection', frame) # Wait for the user to press 'ESC' or 'Q' to close the window key = cv2.waitKey(1) & 0xFF if key == 27 or key == ord('q'): # 27 is the ESC key break # Release the video capture object cap.release() # Close all OpenCV windows cv2.destroyAllWindows() |
Yolo-pose
train
קובץ YMAL זה המקום – אשר שומרים את הקבצים
yolo task=detect mode=train model=yolov8n.pt data=\C:\Users\dev66\datasets5\setup.yaml epochs=10 imgsz=640
1 2 3 4 5 6 |
train: C:\Users\dev66\datasets5\train val: C:\Users\dev66\datasets5\val names: 0: pigeon 1: cat |
לאחר האימון נקבל מודל שבעזרתו ניתן לזהוות את האובייקט
>yolo task=detect mode=predict model=runs/detect/train25/weights/best.pt source=d:/temp3/1.jpg