Introducción y Visión General de SecureVision AI
Introducción a SecureVision AI y CCTV
Fundamentos de Visión Computarizada en CCTV
Procesamiento de Imágenes y Fundamentos de OpenCV
Introducción a OpenCV para Análisis de CCTV
Creación y Análisis de Heatmaps con OpenCV
Quiz: Procesamiento de Imágenes y Fundamentos de OpenCV
Segmentación de Imágenes con YOLO
Configuración de Modelos Preentrenados para Segmentación con YOLO
Integración de Segmentación en Tiempo Real y Generación de Heatmaps
Quiz: Segmentación de Imágenes con YOLO
Detección de Objetos con YOLO
Introducción a la Detección de Objetos con YOLO
Configuración y Uso de Modelos YOLO Preentrenados
Implementación de un Sistema de Conteo de Personas con YOLO
Quiz: Detección de Objetos con YOLO
Pose Estimation con Mediapipe
Fundamentos de Pose Estimation con Mediapipe
Seguimiento y Análisis de Miradas con Mediapipe
Generación de Heatmap de Miradas con Mediapipe y OpenCV
Quiz: Pose Estimation con Mediapipe
Entrenamiento y Creación de Modelos Personalizados con YOLO
Entrenamiento de un Modelo YOLO para Detectar Defectos en Soldaduras Industriales - Parte 1
Entrenamiento de un Modelo YOLO para Detectar Defectos en Soldaduras Industriales - Parte 2
Etiquetado de Imágenes con Label Studio
Reflexión y Cierre del Curso
You don't have access to this class
Keep learning! Join and start boosting your career
Image processing with OpenCV has become a fundamental tool for visual analysis in retail environments. This technology enables companies to gain valuable insights into customer behavior, optimize product distribution and improve the shopping experience. In the following, we will explore how to implement computer vision solutions for a real case of customer flow analysis in a store.
OpenCV (Open Source Computer Vision Library) is an open source library designed specifically for image and video manipulation and analysis. This powerful tool offers numerous advantages that make it the preferred choice for computer vision projects:
To start working with OpenCV in Python, we need to install the library and perform some basic imports:
# Installation (if not already installed)# pip install opencv-python matplotlib
# Necessary importsimport cv2import matplotlib.pyplot as plt
The first step in working with images is to load them correctly. OpenCV provides simple but powerful methods for this task:
# Define image path path = "data/centro_comercial.jpg"
# Load image image = cv2.imread(path)
# Check if image was loaded correctlyif image is not None: # Convert from BGR to RGB for display with Matplotlib image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the image plt.figure() plt.title("Image captured by CCTV") plt.imshow(image_rgb) plt.axis('off') plt.show()else: print("Could not load image")
It is important to note that OpenCV handles images in BGR (Blue, Green, Red) format, while most other libraries, such as Matplotlib, use RGB (Red, Green, Blue) format. For this reason, it is necessary to perform a color conversion when working with both libraries simultaneously.
To capture video from a webcam, OpenCV offers a simple interface:
# Start video capture (0 for the first camera, 1 for the second, etc.)cap = cv2.VideoCapture(1)
if not cap.isOpened(): print("Could not open camera")else: while True: # Capture frame by frame ret, frame = cap.read()
if not ret: print("Could not read frame") break
# Show resulting frame cv2.imshow('Frame CCTV', frame)
# Exit if 'q' is pressed if cv2.waitKey(1) == ord('q'): break
# Release capture object and close windows cap.release() cv2.destroyAllWindows()
Similarly, we can load and play videos stored on disk:
# Load a video from filevideo_path = "data/video_store.mp4"cap = cv2.VideoCapture(video_path)
if not cap.isOpened(): print("Could not open video")else: while True: # Capture frame by frame ret, frame = cap.read()
if not ret: print("Could not read frame") break
# Show resulting frame cv2.imshow('Video CCTV', frame)
# Exit if 'q' is pressed if cv2.waitKey(25) == ord('q'): break
# Release capture object and close windows cap.release() cv2.destroyAllWindows()
To improve the visual quality of images, especially in variable lighting conditions, we can adjust brightness and contrast:
# Adjust brightness and contrastcontrast_factor = 1.2 # Greater than 1 increases contrastbrightness_value = 30 # Positive value increases brightness
# Apply transformationenhanced_image = cv2.convertScaleAbs(image, alpha=contrast_factor, beta=brightness_value)
# Convert for displayenhanced_image_rgb = cv2.cvtColor(enhanced_image, cv2.COLOR_BGR2RGB)
# Display enhanced imageplt.figure()plt.title("Image with adjusted brightness and contrast")plt.imshow(enhanced_image_rgb)plt.axis('off')plt.show()
# Save processed imagecv2.imwrite("ccv_image_correction.jpg", enhanced_image)
For more advanced color adjustments, we can work in the HSV (Hue, Saturation, Value) color space:
import numpy as np
# Convert to HSVhsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Increase saturationhsv_image[:, :, 1] = hsv_image[:, :, 1] * 1.5 # Multiply saturation channel
# Ensure values are within valid rangehsv_image[:, :, 1] = np.clip(hsv_image[:, :, 1], 0, 255)
# Convert back to BGR and then to RGB for displaysaturated_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)saturated_image_rgb = cv2.cvtColor(saturated_image, cv2.COLOR_BGR2RGB)
# Display image with adjusted saturationplt.figure()plt.title("Image with adjusted saturation")plt.imshow(saturated_image_rgb)plt.axis('off')plt.show()
# Save processed imagecv2.imwrite("adjustment_saturation.jpg", saturated_image)
One of the most useful features of OpenCV is the ability to add annotations to images, such as lines, rectangles and text:
# Create a copy of the original imageannotated_image = image.copy()
# Draw a linecv2.line(annotated_image, (50, 50), (200, 50), (255, 0, 0, 0), 3) # Blue in BGR
# Draw a rectanglecv2.rectangle(annotated_image,(100, 100), (300, 200), (0, 255, 0), 2) # Green in BGR
# Add textfont = cv2.FONT_HERSHEY_SIMPLEXcv2.putText(annotated_image, "High traffic zone", (100, 90), font, 0.7, (0, 0, 0, 255), 2) # Red in BGR
# Convert for displayannotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
# Display annotated imageplt.figure()plt.title("Image with annotations")plt.imshow(annotated_image_rgb)plt.axis('off')plt.show()
To visualize the impact of the different techniques, we can show all processed images together:
plt.figure(figsize=(15, 10))
# Original imageplt.subplot(2, 2, 1)plt.title("Original image")plt.imshow(image_rgb)plt.axis('off')
# Image with adjusted brightness and contrastplt.subplot(2, 2, 2)plt.title("Adjusted brightness and contrast")plt.imshow(enhanced_image_rgb)plt.axis('off')
# Image with adjusted saturationplt.subplot(2, 2, 3)plt.title("Adjusted saturation")plt.imshow(saturated_image_rgb)plt.axis('off'))
# Annotated imageplt.subplot(2, 2, 4)plt.title("Annotated image")plt.imshow(annotated_image_rgb)plt.axis('off')
plt.tight_layout()plt.show()
Image processing with OpenCV offers a world of possibilities for visual analysis in commercial environments. From tracking customer flow to identifying high traffic areas, these techniques can provide valuable information for strategic decision making. In what specific situations would you apply these image processing techniques? Share your ideas and experiences in the comments.
Contributions 4
Questions 0
Want to see more contributions, questions and answers from the community?