Try   HackMD

長榮中學 AICourse Build Your Own AI

tags: 長榮中學

HackMD:https://hackmd.io/@Titi/HktLJC840

YOLO

You Only Look Once

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

下載 Miniconda

Miniconda: https://www.anaconda.com/download/success

虛擬環境

Create

$ conda create --name ultralytics-env python=3.8 -y

Enter

$ conda activate ultralytics-env

安裝套件

$ conda install jupyterlab
$ conda install -c conda-forge ultralytics
$ conda install pytorch torchvision torchaudio cpuonly -c pytorch

執行 jupyter lab

$ jupyter lab

Project

  • 創資料夾 (workspace)
  • 創notebook

物件偵測

測試 YOLO 物件偵測

from ultralytics import YOLO # Load a model model = YOLO('yolov8n.pt') # pretrained YOLOv8n model # Predict with the model results = model("https://ultralytics.com/images/bus.jpg") # predict on an image results[0].show()

測試 Webcam

import cv2 cap = cv2.VideoCapture(0) cap.set(3, 640) cap.set(4, 480) while True: ret, img= cap.read() cv2.imshow('Webcam', img) if cv2.waitKey(1) == ord('q'): break cap.release() cv2.destroyAllWindows()

Webcam 進行物件偵測

from ultralytics import YOLO import cv2 import math # start webcam cap = cv2.VideoCapture(0) cap.set(3, 640) cap.set(4, 480) # model model = YOLO("yolov8n.pt") # object classes names = model.model.names while True: success, img = cap.read() results = model(img, stream=True) # coordinates for r in results: boxes = r.boxes for box in boxes: # bounding box x1, y1, x2, y2 = box.xyxy[0] x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values # put box in cam cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3) # confidence confidence = math.ceil((box.conf[0]*100))/100 print("Confidence --->",confidence) # class name cls = int(box.cls[0]) print("Class name -->", names[cls]) # object details org = [x1, y1] font = cv2.FONT_HERSHEY_SIMPLEX fontScale = 1 color = (255, 0, 0) thickness = 2 cv2.putText(img, names[cls], org, font, fontScale, color, thickness) cv2.imshow('Webcam', img) if cv2.waitKey(1) == ord('q'): break cap.release() cv2.destroyAllWindows()

實例分割

測試 YOLO 實例分割

from ultralytics import YOLO # Load a model model = YOLO("yolov8n-seg.pt") # load an official model # Predict with the model results = model("https://ultralytics.com/images/bus.jpg") # predict on an image results[0].show()

Webcam 進行實例分割

from ultralytics import YOLO from ultralytics.utils.plotting import Annotator, colors import cv2 import math # start webcam cap = cv2.VideoCapture(0) cap.set(3, 640) cap.set(4, 480) # Load a model print("Loading model") model = YOLO("yolov8n-seg.pt") # load an official model names = model.model.names while True: ret, im0 = cap.read() if not ret: print("Video frame is empty or video processing has been successfully completed.") break results = model.predict(im0) annotator = Annotator(im0, line_width=2) if results[0].masks is not None: clss = results[0].boxes.cls.cpu().tolist() masks = results[0].masks.xy for mask, cls in zip(masks, clss): annotator.seg_bbox(mask=mask, mask_color=colors(int(cls), True), det_label=names[int(cls)]) cv2.imshow("instance-segmentation", im0) if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows()

骨架辨識

測試 YOLO 骨架辨識

from ultralytics import YOLO # Load a model model = YOLO("yolov8n-pose.pt") # load an official model # Predict with the model results = model("https://ultralytics.com/images/bus.jpg") # predict on an image results[0].show()

Webcam 進行骨架辨識

from ultralytics import YOLO from ultralytics.utils.plotting import Annotator, colors import cv2 import math # start webcam cap = cv2.VideoCapture(0) cap.set(3, 640) cap.set(4, 480) # Load a model print("Loading model") model = YOLO("yolov8n-pose.pt") # load an official model names = model.model.names while True: ret, im0 = cap.read() if not ret: print("Video frame is empty or video processing has been successfully completed.") break results = model.predict(im0) annotator = Annotator(im0) if results[0].keypoints is not None: for data in results[0].keypoints.data: annotator.kpts(kpts=data) cv2.imshow("pose", im0) if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows()