Object Detection Workshop === ## What Is YOLO and What Can It do? :::info Official Resources - Website: https://docs.ultralytics.com - Github: https://github.com/ultralytics/ultralytics ::: YOLO (You Only Look Once) - a popular object detection model | Sports | Safety | | -------- | -------- | | <img alt="Football Player Detection" src="https://github.com/RizwanMunawar/ultralytics/assets/62513924/7d320e1f-fc57-4d7f-a691-78ee579c3442"> | <img alt="People Fall Detection" src="https://github.com/RizwanMunawar/ultralytics/assets/62513924/86437c4a-3227-4eee-90ef-9efb697bdb43"> | | Football Player Detection| People Fall Detection | ## Detection with Official Pre-trained Model - Reference: - Ultralytics: https://docs.ultralytics.com - Github: https://github.com/ultralytics/ultralytics - Arguements: https://docs.ultralytics.com/modes/predict/#inference-arguments - Workshop Notebook: https://colab.research.google.com/drive/1BVvbLD3z3Z88EVO991RT3zfc3wm4hcwc ### Install Packages Download the package from your terminal ```python= pip install ultralytics ``` If you use colab notebook, add this line into your cell ```shell= !pip install ultralytics ``` ### Load Pre-trained Model ```python= from ultralytics import YOLO model = YOLO('yolov8n.pt') # yolov8n.pt 即是預訓練好的模型 ``` :::info you can choose different pre-trained model - `yolov8n.pt` - `yolov8s.pt` - `yolov8m.pt` - `yolov8l.pt` - `yolov8x.pt` ::: ### Inference on an image you should see the `/runs/detect/predict/bus.jpg` in your working directory ```python= source = 'https://ultralytics.com/images/bus.jpg' results = model(source, save=True, conf=0.5) ``` | Original | Inference| | -------- | -------- | | <img src="https://hackmd.io/_uploads/H1JTj1mLp.jpg)" width=405, height=540/>| <img src="https://hackmd.io/_uploads/r1naiJX86.jpg" width=405, height=540/> | ### Inference on a video ```python= source = 'https://github.com/LittleFish-Coder/object-detection-workshop/raw/master/newjeans.mp4' results = model(source, save=True, conf=0.5) ``` <img src="https://github.com/LittleFish-Coder/object-detection-workshop/raw/master/src/newjeans.gif" alt="newjeans"> ### Select Certain Classes #### COCO Dataset and Classes The [COCO](https://cocodataset.org/#home) (Common Objects in Context) dataset is a large-scale object detection, segmentation, and captioning dataset. It is designed to encourage research on a wide variety of object categories and is commonly used for benchmarking computer vision models. It is an essential dataset for researchers and developers working on object detection, segmentation, and pose estimation tasks. ![image](https://hackmd.io/_uploads/SyXQ96NUT.png) Pre-trained模型是在COCO dataset上預先做訓練的,而coco dataset擁有80種不同的objects(classes) :::spoiler 所有object classes 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 ::: #### Select Certain Classes (person) ```python= classes = [0] # 0: person, 1: bicycle, 2: car, 3: motorcycle, 5: airplane ... source = 'https://ultralytics.com/images/bus.jpg' results = model(source, save=True, conf=0.5, classes=classes) ``` #### Count How Many Objects In The Picture ```python= classes = [0] # 0: person, 1: bicycle, 2: car, 3: motorcycle, 4: airplane, 5: bus ... source = 'https://ultralytics.com/images/bus.jpg' results = model.predict(source, save=True, conf=0.5, classes=classes) for r in results: print(r.boxes.cls) # r.boxes.cls 為預測結果的類別 # tensor([0., 0., 0.) # 3個0代表預測結果為3個person source = 'https://ultralytics.com/images/bus.jpg' results = model.predict(source, save=True, conf=0.5, classes=None) for r in results: print(r.boxes.cls) # r.boxes.cls 為預測結果的類別 # tensor([5., 0., 0., 0.) # 5代表預測結果為airplane, 3個0代表預測結果為3個person # count number of people source = 'https://ultralytics.com/images/bus.jpg' results = model.predict(source, save=True, conf=0.5, classes=None) people = 0 for r in results: for c in r.boxes.cls: print(model.names[int(c)]) if int(c) == 0: people += 1 print(f'There are {people} people') # 3 ``` ### Inference On Your Webcam (會有機率當機) - Install OpenCV ```shell= pip install opencv-python ``` - CV2.VideoCapture() ```pytohn= import cv2 video_path = 0 # 0 代表使用電腦內建的攝影機 cap = cv2.VideoCapture(video_path) # Loop through the video frames while cap.isOpened(): # Read a frame from the video success, frame = cap.read() if success: # Run YOLOv8 inference on the frame results = model(frame) # Visualize the results on the frame annotated_frame = results[0].plot() # Display the annotated frame cv2.imshow("YOLOv8 Inference", annotated_frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord("q"): break else: # Break the loop if the end of the video is reached break # Release the video capture object and close the display window cap.release() cv2.destroyAllWindows() ``` ## Customize Model - Official Tutorial: https://youtu.be/a3SBRtILjPI - Label Platform: https://roboflow.com (Use Chrome Browser!) - Dataset Universe: https://universe.roboflow.com/ ### Prepare Your Own Dataset Label your own dataset ![messageImage_1702322339154](https://hackmd.io/_uploads/H1YneyB8T.jpg) ### Export yaml file ![image](https://hackmd.io/_uploads/B1mrgJS8p.png) The file contains ![image](https://hackmd.io/_uploads/SJOUxJHL6.png) data.yml is the meta data of your dataset ### Transfer Learning ```python= # train the model on custom dataset yml_path = 'data.yaml' results = model.train(data=yml_path, epochs=10) # the result will be saved in runs/detect/train/weights ```