You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

0 Días
14 Hrs
46 Min
8 Seg

Integración de Segmentación en Tiempo Real y Generación de Heatmaps

6/16
Resources

People traffic analysis in commercial spaces has evolved significantly thanks to the integration of advanced computer vision techniques. The combination of traditional image processing with segmentation models such as YOLO allows for much more accurate results focused exclusively on human movement, eliminating the visual noise that previously hindered these analyses. This technology is transforming the way businesses understand their customers' behavior in physical space.

How to improve people detection with segmentation?

The initial problem with traditional image processing is that it detects any moving element, without discriminating whether they are people or other objects. This generates "noise" in the data, such as when a rope or other element moves and is incorrectly detected as relevant traffic.

The implemented solution combines two powerful techniques:

  1. Motion detection: using background subtraction to identify any changes between frames.
  2. Segmentation with YOLO: Applying a specific model to identify and isolate only people.

The result is a refined hitmap that exclusively shows the movement of people, eliminating false positives and providing much cleaner and more useful data for analysis.

Implementing the refined hitmap with segmentation

To implement this solution we need to combine traditional image processing with segmentation using YOLO. The process can be divided into the following steps:

  1. Installing dependencies (ultralytics)
  2. Definition of the video path
  3. Configuration of the background extractor
  4. Import YOLO model for segmentation
  5. Frame by frame processing

The basic code for this implementation would be:

# Install dependencies!pip install ultralytics
 # Necessary importsimport cv2import numpy as npfrom ultralytics import YOLO
 # Define video pathvideo_path = "data/video.mp4"
 # Configure background extractorbg_subtractor = cv2.createBackgroundSubtractorMOG2()
 # Initialize refined hitmaphitmap_refined hitmap = np.zeros((height, width),  dtype=np.uint8)
 # Load YOLO model for segmentationmodel = YOLO('yolov8n-seg.pt')
 # Open videocap = cv2.VideoCapture(video_path)
while True: ret, frame = cap.read() if not ret: break    
 # Motion detection (image processing) fg_mask = bg_subtractor.apply(frame) thresh = cv2.threshold(fg_mask, 200, 255, cv2.THRESH_BINARY)[1]    
 # Segmentation with YOLO results = model(frame, verbose=False) result = results[0]    
 # Create segmentation mask seg_mask = np.zeros_like(fg_mask)    
 if len(result) > 0: for seg, cls in zip(result.masks.data, result.boxes.cls): # Only consider people (class 0) if cls == 0: # Convert segmentation to binary mask segment = seg.cpu().numpy() # Resize to frame size segment = cv2.resize(segment, (width, height)) seg_mask = np.logical_or(seg_mask, segment).astype(np.uint8) * 255    
 # Combine masks (motion + segmentation) refined_mask = cv2.bitwise_and(thresh, seg_mask)    
 # Update hitmap hitmap_refined = cv2.add(hitmap_refined, refined_mask)
cap.release()

This code combines motion detection with segmentation to create a hitmap that exclusively shows the movement of people.

What are the advantages of segmentation in traffic analysis?

Implementing segmentation with YOLO for traffic analysis offers significant benefits:

  • Elimination of false positives: Moving objects that are not people (such as ropes, doors, etc.) are no longer detected.
  • Increased accuracy: The resulting hitmap shows exclusively human movement.
  • Cleaner data: The visualization is clearer and easier to interpret.
  • More focused analysis: Allows you to concentrate on customer behavior without distractions.

In the example shown, it was possible to solve the specific problem of a moving rope that was previously incorrectly detected as relevant traffic. With segmentation, the system now ignores the rope and focuses exclusively on people.

How to implement this solution in Google Colab?

To implement this solution in Google Colab, it is important to ensure that you have access to a GPU to speed up processing. The process is similar to the local one, but with some additional considerations:

  1. Enable GPU: In "Notebook Settings" make sure the GPU is enabled.
  2. Install dependencies: Install ultralytics as shown above.
  3. Upload video: Upload the video you want to analyze.
  4. Download the YOLO model: In case of problems with the automatic download, you can do it manually:
!wget https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n-seg.pt

The code for Google Colab is essentially the same, but optimized to directly generate the final hitmap without displaying each frame during processing, which significantly speeds up the execution.

Troubleshooting common problems

A common problem is that YOLO may not automatically download the model. If this happens:

  1. Go to the Ultralytics documentation.
  2. Search for the segmentation model you need
  3. Right click on the link and select "Copy link".
  4. Use the wget command to download it manually.

This approach ensures that you have access to the model even when the automatic download fails.

What results can we expect?

The results of implementing segmentation with YOLO for traffic analysis are noticeably superior to traditional methods:

  • Cleaner Hitmap: No isolated points or visual noise.
  • Exclusive focus on people: Only human movement is visualized.
  • Clear movement patterns: It is easy to identify where customers spend the most time.
  • Elimination of false positives: Elements such as ropes or moving objects no longer affect the analysis.

In the examples shown, the processing time was approximately 2 minutes and 15 seconds for a complete video using GPU, demonstrating the efficiency of this solution.

The combination of traditional image processing with YOLO segmentation represents a significant advance in human traffic analysis. This technique not only improves data accuracy, but also provides more valuable information for business decision making. The next natural step would be to implement a people counter to quantify traffic in different areas of the retail space, taking the analysis to an even deeper level.

Contributions 1

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Excelente clase, nunca había pensado entender esto, pero el profe lo explica muy bien 👍