#!/usr/bin/env python # coding: utf-8 # [![image](https://raw.githubusercontent.com/visual-layer/visuallayer/main/imgs/vl_horizontal_logo.png)](https://www.visual-layer.com) # # Object Detection from Videos with YOLOv5 # # [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/visual-layer/fastdup/blob/main/examples/video-yolov5-detection.ipynb) # [![Open in Kaggle](https://kaggle.com/static/images/open-in-kaggle.svg)](https://kaggle.com/kernels/welcome?src=https://github.com/visual-layer/fastdup/blob/main/examples/video-yolov5-detection.ipynb) # # In this tutorial, we will use fastdup with a pretrained yolov5 object detection model to detect and crop from videos. Following that we analyze the cropped objects for issues such as duplicates, near-duplicates, outliers, bright/dark/blurry objects. # ## Installation & Setting Up # In[ ]: get_ipython().system('pip install fastdup kaggle -Uq') # In[1]: import fastdup fastdup.__version__ # ## Download & Extract Dataset # # # Let's download the [Video Fight Detection Dataset](https://www.kaggle.com/datasets/naveenk903/movies-fight-detection-dataset) from Kaggle. The dataset consists of real life videos of fight and non-fight scenes meant for video classification task. # # You can download the dataset by manually by heading to the dataset [homepage](https://www.kaggle.com/datasets/naveenk903/movies-fight-detection-dataset) or using the [Kaggle API](https://github.com/Kaggle/kaggle-api). # # Let's use the Kaggle API to download the dataset: # In[ ]: get_ipython().system('kaggle datasets download -d naveenk903/movies-fight-detection-dataset') # Next, extract the .zip file into a folder named `data`. # In[ ]: get_ipython().system('unzip -q movies-fight-detection-dataset.zip -d data') # ## Video to Images # # fastdup works on images. We must first turn the videos into frames of images. # # We can use a one-liner fastdup utility function to turn all the videos in a folder into frames. This should create a new folder called `frames` that has all the frames from the videos. # In[ ]: fastdup.extract_video_frames(input_dir="data/", work_dir="frames/") # ## Run fastdup # # Now that we have the frames of images, let's run fastdup and analyze the frames. # In[2]: fd = fastdup.create(input_dir='frames/') # For demonstration we'll just run on `num_images=2000`, feel free to change its value or omitting it altogether to run it on the entire dataset. # In[3]: fd.run(bounding_box='yolov5s') # ## Components Gallery # # We can visualize the cluster of similar detections using the components gallery view. Specify `draw_bbox=True` to see the detection bounding box on the original image. # In[4]: fd.vis.component_gallery(draw_bbox=True) # If you'd like to view just the cropped bounding box images, specify `draw_bbox=False` # In[5]: fd.vis.component_gallery(draw_bbox=False) # ## Find Similar Objects Across Videos # # Using the `similarity_gallery` view, we can find similar looking detections across all the extracted frames. # In[6]: fd.vis.similarity_gallery(draw_bbox=False) # ## Find Outliers # # Useing the `outliers_gallery` we can also visualize detections that looks visually different from others. # In[7]: fd.vis.outliers_gallery() # ## Duplicate Detections # # With the `duplicates_gallery` view, visualize duplicate image pairs across videos. # In[8]: fd.vis.duplicates_gallery() # ## Dark Detections # # Using the `stats_gallery` view, we can sort the detections following a desired `metric` such as 'dark', 'bright' and 'blur'. # In[9]: fd.vis.stats_gallery(metric='dark') # ## Bright Detections # In[10]: fd.vis.stats_gallery(metric='bright') # ## Blurry Detections # In[11]: fd.vis.stats_gallery(metric='blur') # ## Wrap Up # # Next, feel free to check out other tutorials - # # + โšก [**Quickstart**](https://nbviewer.org/github/visual-layer/fastdup/blob/main/examples/quick-dataset-analysis.ipynb): Learn how to install fastdup, load a dataset and analyze it for potential issues such as duplicates/near-duplicates, broken images, outliers, dark/bright/blurry images, and view visually similar image clusters. If you're new, start here! # + ๐Ÿงน [**Clean Image Folder**](https://nbviewer.org/github/visual-layer/fastdup/blob/main/examples/cleaning-image-dataset.ipynb): Learn how to analyze and clean a folder of images from potential issues and export a list of problematic files for further action. If you have an unorganized folder of images, this is a good place to start. # + ๐Ÿ–ผ [**Analyze Image Classification Dataset**](https://nbviewer.org/github/visual-layer/fastdup/blob/main/examples/analyzing-image-classification-dataset.ipynb): Learn how to load a labeled image classification dataset and analyze for potential issues. If you have labeled ImageNet-style folder structure, have a go! # + ๐ŸŽ [**Analyze Object Detection Dataset**](https://nbviewer.org/github/visual-layer/fastdup/blob/main/examples/analyzing-object-detection-dataset.ipynb): Learn how to load bounding box annotations for object detection and analyze for potential issues. If you have a COCO-style labeled object detection dataset, give this example a try. # # ## VL Profiler # If you prefer a no-code platform to inspect and visualize your dataset, [**try our free cloud product VL Profiler**](https://app.visual-layer.com) - VL Profiler is our first no-code commercial product that lets you visualize and inspect your dataset in your browser. # # [Sign up](https://app.visual-layer.com) now, it's free. # # [![image](https://raw.githubusercontent.com/visual-layer/fastdup/main/gallery/vl_profiler_promo.svg)](https://app.visual-layer.com) # # As usual, feedback is welcome! # # Questions? Drop by our [Slack channel](https://visualdatabase.slack.com/join/shared_invite/zt-19jaydbjn-lNDEDkgvSI1QwbTXSY6dlA#/shared-invite/email) or open an issue on [GitHub](https://github.com/visual-layer/fastdup/issues).