{"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":14315,"databundleVersionId":862230,"sourceType":"competition"},{"sourceId":10658491,"sourceType":"datasetVersion","datasetId":6600281},{"sourceId":10669808,"sourceType":"datasetVersion","datasetId":6608437},{"sourceId":144395,"sourceType":"modelInstanceVersion","modelInstanceId":122379,"modelId":145460},{"sourceId":108462,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":90852,"modelId":115083}],"dockerImageVersionId":30839,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install ultralytics transformers sentencepiece\n!pip install ultralytics\n!pip install cvzone","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-07T14:33:50.012600Z","iopub.execute_input":"2025-02-07T14:33:50.012933Z","iopub.status.idle":"2025-02-07T14:34:04.158897Z","shell.execute_reply.started":"2025-02-07T14:33:50.012908Z","shell.execute_reply":"2025-02-07T14:34:04.158061Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\nimport matplotlib.pyplot as plt\nimport cv2\nimport cvzone\nimport math\nimport time\nimport os\n\nmodel = YOLO(\"/kaggle/input/yolov8l/pytorch/default/1/yolov8l.pt\")\n\ncap = cv2.VideoCapture(\"/kaggle/input/testing-video-1/video_20250205_180325.mp4\")\n\noutput_folder = \"output_frames\"\nos.makedirs(output_folder, exist_ok=True)\n\nclassNames = [\"person\", \"bicycle\", \"car\", \"motorbike\", \"aeroplane\", \"bus\", \"train\", \"truck\", \"boat\",\n              \"traffic light\", \"fire hydrant\", \"stop sign\", \"parking meter\", \"bench\", \"bird\", \"cat\",\n              \"dog\", \"horse\", \"sheep\", \"cow\", \"elephant\", \"bear\", \"zebra\", \"giraffe\", \"backpack\", \"umbrella\",\n              \"handbag\", \"tie\", \"suitcase\", \"frisbee\", \"skis\", \"snowboard\", \"sports ball\", \"kite\", \"baseball bat\",\n              \"baseball glove\", \"skateboard\", \"surfboard\", \"tennis racket\", \"bottle\", \"wine glass\", \"cup\",\n              \"fork\", \"knife\", \"spoon\", \"bowl\", \"banana\", \"apple\", \"sandwich\", \"orange\", \"broccoli\",\n              \"carrot\", \"hot dog\", \"pizza\", \"donut\", \"cake\", \"chair\", \"sofa\", \"pottedplant\", \"bed\",\n              \"diningtable\", \"toilet\", \"tvmonitor\", \"laptop\", \"mouse\", \"remote\", \"keyboard\", \"cell phone\",\n              \"microwave\", \"oven\", \"toaster\", \"sink\", \"refrigerator\", \"book\", \"clock\", \"vase\", \"scissors\",\n              \"teddy bear\", \"hair drier\", \"toothbrush\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-07T14:35:01.429379Z","iopub.execute_input":"2025-02-07T14:35:01.429743Z","iopub.status.idle":"2025-02-07T14:35:06.681731Z","shell.execute_reply.started":"2025-02-07T14:35:01.429704Z","shell.execute_reply":"2025-02-07T14:35:06.681031Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"while True:\n    success, img = cap.read()\n    if not success:\n        break\n    results = model(img, stream=True)\n    for r in results:\n        boxes = r.boxes\n        for box in boxes:\n            # Bounding Box\n            x1, y1, x2, y2 = box.xyxy[0]\n            x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)\n            w, h = x2 - x1, y2 - y1\n            cvzone.cornerRect(img, (x1, y1, w, h))\n            conf = math.ceil((box.conf[0] * 100)) / 100\n            cls = int(box.cls[0])\n            cvzone.putTextRect(img, f'{classNames[cls]} {conf}', (max(0, x1), max(35, y1)), scale=1, thickness=1)\n\n    output_path = os.path.join(output_folder, f\"frame_{int(cap.get(cv2.CAP_PROP_POS_FRAMES)):06d}.jpg\")\n    cv2.imwrite(output_path, img)\ncap.release()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-07T14:35:11.246860Z","iopub.execute_input":"2025-02-07T14:35:11.247282Z","iopub.status.idle":"2025-02-07T14:35:27.568970Z","shell.execute_reply.started":"2025-02-07T14:35:11.247258Z","shell.execute_reply":"2025-02-07T14:35:27.568112Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output_video_path = \"output_video_4.mp4\"\nimg_array = []\nfor filename in sorted(os.listdir(output_folder)):\n    if filename.endswith(\".jpg\"):\n        img_path = os.path.join(output_folder, filename)\n        img = cv2.imread(img_path)\n        height, width, layers = img.shape\n        size = (width, height)\n        img_array.append(img)\n\nout = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*'mp4v'), 30, size)\nfor i in range(len(img_array)):\n    out.write(img_array[i])\nout.release()\n\nprint(\"Video created successfully.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-07T14:35:44.415633Z","iopub.execute_input":"2025-02-07T14:35:44.416005Z","iopub.status.idle":"2025-02-07T14:35:48.868546Z","shell.execute_reply.started":"2025-02-07T14:35:44.415975Z","shell.execute_reply":"2025-02-07T14:35:48.867819Z"}},"outputs":[],"execution_count":null}]}