{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":15768,"databundleVersionId":700263,"sourceType":"competition"}],"dockerImageVersionId":30528,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# YOLOv8 is part of ultralytics package\n!pip install ultralytics  #The ultralytics package has the YOLO class","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-20T14:35:26.328598Z","iopub.execute_input":"2024-08-20T14:35:26.329495Z","iopub.status.idle":"2024-08-20T14:35:40.192252Z","shell.execute_reply.started":"2024-08-20T14:35:26.329456Z","shell.execute_reply":"2024-08-20T14:35:40.191066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import libraries\nimport numpy as np\nimport pandas as pd\nfrom ultralytics import YOLO\nimport cv2\nimport PIL \nfrom PIL import Image\nfrom IPython.display import display\nimport matplotlib.pyplot as plt\nimport os \nimport pathlib ","metadata":{"execution":{"iopub.status.busy":"2024-08-20T14:37:45.866331Z","iopub.execute_input":"2024-08-20T14:37:45.866712Z","iopub.status.idle":"2024-08-20T14:37:49.142350Z","shell.execute_reply.started":"2024-08-20T14:37:45.866678Z","shell.execute_reply":"2024-08-20T14:37:49.141515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create model using pretrained yolov8\nmodel = YOLO(\"yolov8m.pt\") ","metadata":{"execution":{"iopub.status.busy":"2024-08-20T14:37:49.144159Z","iopub.execute_input":"2024-08-20T14:37:49.144779Z","iopub.status.idle":"2024-08-20T14:37:50.115615Z","shell.execute_reply.started":"2024-08-20T14:37:49.144743Z","shell.execute_reply":"2024-08-20T14:37:50.114799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results=model.predict(source=\"/kaggle/input/3d-object-detection-for-autonomous-vehicles/test_images/host-a004_cam0_1231810077351067006.jpeg\",save=True, conf=0.2,iou=0.5)\n# conf: object confidence threshold for detection\n#Iou: intersection over union threshold for Non Max Supression","metadata":{"execution":{"iopub.status.busy":"2024-08-20T14:37:50.116751Z","iopub.execute_input":"2024-08-20T14:37:50.117060Z","iopub.status.idle":"2024-08-20T14:37:52.061761Z","shell.execute_reply.started":"2024-08-20T14:37:50.117033Z","shell.execute_reply":"2024-08-20T14:37:52.060903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results\n#as you can see below, Results object contains 5 components:\n# boxes : they are object with properties for manipulating bounding boxes.\n# masks : masks object indexing masks or getting segment coordinates\n# keypoints : keypoint object for with properties and methods for manipulating predicted keypoints\n# probs : pobs object for containing class probabilities\n# orig_img : original image loaded in memory\n# path :  path to the input image ","metadata":{"execution":{"iopub.status.busy":"2024-08-20T14:37:52.064230Z","iopub.execute_input":"2024-08-20T14:37:52.064875Z","iopub.status.idle":"2024-08-20T14:37:52.073259Z","shell.execute_reply.started":"2024-08-20T14:37:52.064838Z","shell.execute_reply":"2024-08-20T14:37:52.072274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = results[0]\n\n# finding the detailed result(Class, Coordinates, Prob)\n\nfor box in result.boxes:\n    class_id = result.names[box.cls[0].item()]\n    cords = box.xyxy[0].tolist()\n    cords = [round(x) for x in cords]\n    conf = round(box.conf[0].item(), 2)\n    print(\"Object type:\", class_id)\n    print(\"Coordinates:\", cords)\n    print(\"Probability:\", conf)\n    print(\"---\")","metadata":{"execution":{"iopub.status.busy":"2024-08-20T14:37:52.074578Z","iopub.execute_input":"2024-08-20T14:37:52.074925Z","iopub.status.idle":"2024-08-20T14:37:52.089417Z","shell.execute_reply.started":"2024-08-20T14:37:52.074892Z","shell.execute_reply":"2024-08-20T14:37:52.088405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting results\nres_plotted = results[0].plot()\nres_plotted = cv2.cvtColor(res_plotted, cv2.COLOR_BGR2RGB)   #opencv uses BGR color format, while other image libraries (e.g. Image) uses RGB. So we need a conversion from BGR to RGB\ndisplay(Image.fromarray(res_plotted)) # .fromarray is used to create image from numpy array","metadata":{"execution":{"iopub.status.busy":"2024-08-20T14:37:52.090615Z","iopub.execute_input":"2024-08-20T14:37:52.090909Z","iopub.status.idle":"2024-08-20T14:37:52.582268Z","shell.execute_reply.started":"2024-08-20T14:37:52.090883Z","shell.execute_reply":"2024-08-20T14:37:52.581097Z"},"trusted":true},"execution_count":null,"outputs":[]}]}