#!/usr/bin/env python # coding: utf-8 # # Object Detection from Videos with YOLOv5 # # 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. # # > This is an advanced functionality of fastdup. Sign up for free to be an beta tester and get early access at info@visual-layer.com . # ## Installation & Setting Up # In[ ]: get_ipython().system('pip install pip -U') get_ipython().system('pip install fastdup') # In[4]: import fastdup fastdup.__version__ # ## Download & Extract Dataset # In[1]: get_ipython().system('gdown --fuzzy https://drive.google.com/file/d/1fzmOgmRu557aU4lEbzL7XCf78KntFCeQ/view') # In[2]: get_ipython().system('unzip data.zip') # ## 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: # In[5]: 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[6]: fd = fastdup.create(input_dir='frames', work_dir='yolov5_detection_work_dir') # As this is an advance functionality of fastdup, you'd need a license key to use this function, sign up and get your license key for free at info@visual-layer.com . # In[7]: fd.run(bounding_box='yolov5s', license='your_license_key', overwrite=True) # ## 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[8]: fd.vis.component_gallery(draw_bbox=True) # If you'd like to view just the cropped bounding box images, specify `draw_bbox=False` # In[9]: 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[10]: 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[11]: fd.vis.outliers_gallery() # ## Duplicate Detections # # With the `duplicates_gallery` view, visualize duplicate image pairs across videos. # In[12]: fd.vis.duplicates_gallery() # In[ ]: # ## Dark Detections # # Using the `stats_gallery` view, we can sort the detections following a desired `metric` such as 'dark', 'bright' and 'blur'. # In[13]: fd.vis.stats_gallery(metric='dark') # ## Bright Detections # In[14]: fd.vis.stats_gallery(metric='bright') # ## Blurry Detections # In[15]: fd.vis.stats_gallery(metric='blur') # In[ ]: