{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":"2023-08-12T21:36:20.08988Z","iopub.execute_input":"2023-08-12T21:36:20.090323Z","iopub.status.idle":"2023-08-12T21:36:34.215935Z","shell.execute_reply.started":"2023-08-12T21:36:20.090282Z","shell.execute_reply":"2023-08-12T21:36:34.214767Z"},"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":"2023-08-12T21:37:34.997858Z","iopub.execute_input":"2023-08-12T21:37:34.998564Z","iopub.status.idle":"2023-08-12T21:37:35.004181Z","shell.execute_reply.started":"2023-08-12T21:37:34.998525Z","shell.execute_reply":"2023-08-12T21:37:35.003109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create model using pretrained yolov8\nmodel = YOLO(\"yolov8m.pt\") ","metadata":{"execution":{"iopub.status.busy":"2023-08-12T21:38:41.192181Z","iopub.execute_input":"2023-08-12T21:38:41.192582Z","iopub.status.idle":"2023-08-12T21:38:41.338266Z","shell.execute_reply.started":"2023-08-12T21:38:41.192549Z","shell.execute_reply":"2023-08-12T21:38:41.337309Z"},"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":"2023-08-12T21:41:33.684119Z","iopub.execute_input":"2023-08-12T21:41:33.68454Z","iopub.status.idle":"2023-08-12T21:41:58.140407Z","shell.execute_reply.started":"2023-08-12T21:41:33.684502Z","shell.execute_reply":"2023-08-12T21:41:58.139362Z"},"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":"2023-08-12T21:42:34.752428Z","iopub.execute_input":"2023-08-12T21:42:34.753496Z","iopub.status.idle":"2023-08-12T21:42:34.763887Z","shell.execute_reply.started":"2023-08-12T21:42:34.753448Z","shell.execute_reply":"2023-08-12T21:42:34.762614Z"},"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":"2023-08-12T21:43:38.874419Z","iopub.execute_input":"2023-08-12T21:43:38.874837Z","iopub.status.idle":"2023-08-12T21:43:38.89411Z","shell.execute_reply.started":"2023-08-12T21:43:38.874804Z","shell.execute_reply":"2023-08-12T21:43:38.89287Z"},"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":"2023-08-12T21:44:02.528753Z","iopub.execute_input":"2023-08-12T21:44:02.529318Z","iopub.status.idle":"2023-08-12T21:44:03.214615Z","shell.execute_reply.started":"2023-08-12T21:44:02.529279Z","shell.execute_reply":"2023-08-12T21:44:03.213502Z"},"trusted":true},"execution_count":null,"outputs":[]}]}