{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"First we will prepare images for more accurate analysis. With the help of object detection model called Yolov8, we will crop all images within the boxes detected by this model.","metadata":{}},{"cell_type":"code","source":"pip install ultralytics","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-06-01T16:45:49.577229Z","iopub.execute_input":"2023-06-01T16:45:49.577624Z","iopub.status.idle":"2023-06-01T16:46:06.567130Z","shell.execute_reply.started":"2023-06-01T16:45:49.577594Z","shell.execute_reply":"2023-06-01T16:46:06.565798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO\nfrom distutils.dir_util import copy_tree\nimport shutil\nimport os\nfrom zipfile import ZipFile\nfrom PIL import Image\nimport numpy as np\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-06-01T16:46:06.569900Z","iopub.execute_input":"2023-06-01T16:46:06.570328Z","iopub.status.idle":"2023-06-01T16:46:12.764551Z","shell.execute_reply.started":"2023-06-01T16:46:06.570285Z","shell.execute_reply":"2023-06-01T16:46:12.763544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"copy_tree(\"/kaggle/input/config\",\"/kaggle/working/\")\ncopy_tree(\"/kaggle/input/train-data/data\",\"/kaggle/working/\")","metadata":{"execution":{"iopub.status.busy":"2023-06-01T16:46:12.765826Z","iopub.execute_input":"2023-06-01T16:46:12.766500Z","iopub.status.idle":"2023-06-01T16:46:23.803628Z","shell.execute_reply.started":"2023-06-01T16:46:12.766462Z","shell.execute_reply":"2023-06-01T16:46:23.802694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_DIR = \"/kaggle/working/\"","metadata":{"execution":{"iopub.status.busy":"2023-06-01T16:46:23.806435Z","iopub.execute_input":"2023-06-01T16:46:23.806838Z","iopub.status.idle":"2023-06-01T16:46:23.810910Z","shell.execute_reply.started":"2023-06-01T16:46:23.806802Z","shell.execute_reply":"2023-06-01T16:46:23.809908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = YOLO(\"yolov8n.yaml\")\n# result = model.train(data=os.path.join(ROOT_DIR, \"custom_data.yaml\"), epochs=20)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T16:46:23.812206Z","iopub.execute_input":"2023-06-01T16:46:23.813613Z","iopub.status.idle":"2023-06-01T16:46:23.825497Z","shell.execute_reply.started":"2023-06-01T16:46:23.813577Z","shell.execute_reply":"2023-06-01T16:46:23.824489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = YOLO(\"/kaggle/input/trained-model/best.pt\")","metadata":{"execution":{"iopub.status.busy":"2023-06-01T16:46:23.826561Z","iopub.execute_input":"2023-06-01T16:46:23.827209Z","iopub.status.idle":"2023-06-01T16:46:24.156070Z","shell.execute_reply.started":"2023-06-01T16:46:23.827177Z","shell.execute_reply":"2023-06-01T16:46:24.155142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# copy_tree(\"runs/detect/train/weights/\",\"/kaggle/working/\")","metadata":{"execution":{"iopub.status.busy":"2023-06-01T16:46:24.157597Z","iopub.execute_input":"2023-06-01T16:46:24.157956Z","iopub.status.idle":"2023-06-01T16:46:24.162656Z","shell.execute_reply.started":"2023-06-01T16:46:24.157924Z","shell.execute_reply":"2023-06-01T16:46:24.161477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with ZipFile(\"/kaggle/input/noaa-right-whale-recognition/imgs.zip\", 'r') as zip_ref:\n    zip_ref.extractall(\"/kaggle/working/\")\nzip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2023-06-01T16:46:24.164599Z","iopub.execute_input":"2023-06-01T16:46:24.165071Z","iopub.status.idle":"2023-06-01T16:48:43.122107Z","shell.execute_reply.started":"2023-06-01T16:46:24.164942Z","shell.execute_reply":"2023-06-01T16:48:43.121053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"/kaggle/working/cropped_imgs\")","metadata":{"execution":{"iopub.status.busy":"2023-06-01T16:48:43.123589Z","iopub.execute_input":"2023-06-01T16:48:43.124226Z","iopub.status.idle":"2023-06-01T16:48:43.129105Z","shell.execute_reply.started":"2023-06-01T16:48:43.124189Z","shell.execute_reply":"2023-06-01T16:48:43.128043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img_name in tqdm(os.listdir(\"/kaggle/working/imgs\")):\n    try:\n        image = Image.open(f\"/kaggle/working/imgs/{img_name}\")\n        results = model.predict(np.asarray(image), verbose=False)\n        boxes = results[0].boxes.xyxy.data\n        if boxes.numel() != 0:\n            image = image.crop(boxes[0].tolist())\n        image.save(f\"/kaggle/working/cropped_imgs/{img_name}\")\n    except Exception as e:\n        print(\"error --\",e)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T16:49:34.833676Z","iopub.execute_input":"2023-06-01T16:49:34.834250Z","iopub.status.idle":"2023-06-01T17:10:29.469094Z","shell.execute_reply.started":"2023-06-01T16:49:34.834209Z","shell.execute_reply":"2023-06-01T17:10:29.467885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.make_archive(\"cropped_images\", 'zip', \"/kaggle/working/cropped_imgs\")","metadata":{"execution":{"iopub.status.busy":"2023-06-01T17:14:15.296307Z","iopub.execute_input":"2023-06-01T17:14:15.296764Z","iopub.status.idle":"2023-06-01T17:17:58.246261Z","shell.execute_reply.started":"2023-06-01T17:14:15.296719Z","shell.execute_reply":"2023-06-01T17:17:58.245073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# downloading cropped images for using in another notebook\nfrom IPython.display import FileLink\nFileLink('cropped_images.zip')","metadata":{"execution":{"iopub.status.busy":"2023-06-01T17:35:48.953060Z","iopub.execute_input":"2023-06-01T17:35:48.953445Z","iopub.status.idle":"2023-06-01T17:35:48.959946Z","shell.execute_reply.started":"2023-06-01T17:35:48.953415Z","shell.execute_reply":"2023-06-01T17:35:48.958782Z"},"trusted":true},"execution_count":null,"outputs":[]}]}