Skip to main content

A Serious Example: Retail Analytics

Tracking utilization of a customer service counter

This example demonstrates the application of Groundlight to a retail analytics solution, which monitors the usage of a service counter by customers throughout the day. The application creates a detector to identify when the service desk is being utilized by a customer. It checks the detector every minute, and once an hour, it prints out a summary of the percentage of time that the service counter is in use. At the end of the day, it emails the daily log.

This retail analytics application can be beneficial in various ways:

  1. Staff allocation and scheduling: By analyzing the usage patterns of the service counter, store managers can optimize staff allocation and scheduling, ensuring that enough employees are available during peak hours and reducing wait times for customers.

  2. Identifying trends: The application can help identify trends in customer behavior, such as busier times of the day or specific days of the week with higher traffic. This information can be used to plan targeted marketing campaigns or promotions to increase sales and customer engagement.

  3. Improving store layout: Understanding when and how often customers use the service counter can provide insights into the effectiveness of the store's layout. Retailers can use this information to make data-driven decisions about rearranging the store layout to encourage customers to visit the service counter or explore other areas of the store.

  4. Customer satisfaction: By monitoring the usage of the service counter and proactively addressing long wait times or crowded areas, retailers can improve customer satisfaction and loyalty. A positive customer experience can lead to increased sales and return visits.

To implement this retail analytics solution, a store would need to install a supported camera near the service counter, ensuring a clear view of the area. The camera would then be connected to a computer running the Groundlight-based application. Store managers would receive hourly summaries of the service counter usage and a daily log via email, enabling them to make informed decisions to improve store operations and customer experience.


  • Groundlight SDK with Python 3.7 or higher
  • A supported USB or network-connected camera
  • An email account with SMTP access to send the daily log


Ensure you have Python 3.7 or higher installed, and then install the Groundlight SDK, OpenCV library, and other required libraries:

pip install groundlight opencv-python pillow

Creating the Application

  1. First, log in to the Groundlight application and get an API Token.

  2. Next, we'll write the Python script for the application. Import the required libraries:

import time
import cv2
import smtplib
from groundlight import Groundlight
from PIL import Image
from datetime import datetime, timedelta
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
  1. Define a function to capture an image from the camera using OpenCV:
def capture_image():
cap = cv2.VideoCapture(0)

ret, frame =

if ret:
# Convert to PIL image
return Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
return None
  1. Define a function to send the daily log via email. You will need to customize this for your particular network environment.
def send_email(sender, receiver, subject, body):
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('', 587)
server.login(sender, "your-password")
text = msg.as_string()
server.sendmail(sender, receiver, text)
  1. Define when your business's operating hours are:
START_OF_BUSINESS = 9  # e.g. 9am
END_OF_BUSINESS = 17 # e.g. 5pm

def is_within_business_hours():
current_hour =
return START_OF_BUSINESS <= current_hour < END_OF_BUSINESS

  1. Write the main application loop:
gl = Groundlight()

detector = gl.get_or_create_detector(
query="Is there a customer at the service counter?",
# We can get away with relatively low confidence since we're aggregating

DELAY = 60

log = []
daily_log = []
next_hourly_start =, second=0, microsecond=0) + timedelta(hours=1)

while True:
if not is_within_business_hours():

image = capture_image()
if not image:
print("Failed to capture image")

iq = gl.submit_image_query(image=image, detector=detector, wait=60)
except Exception as e:
print(f"Error submitting image query: {e}")

answer = iq.result.label

if >= next_hourly_start:
next_hourly_start += timedelta(hours=1)

percent_in_use = (log.count("YES") / len(log)) * 100
current_time =, minute=0, second=0)
formatted_time = current_time.strftime("%I%p") # like 3pm
msg = f"Hourly summary for {formatted_time}: {percent_in_use:.0f}% counter in use"
log = []

current_hour =
if current_hour == END_OF_BUSINESS and not daily_log == []:
daily_summary = "Daily summary:\n"
for msg in daily_log:
daily_summary += f"{msg}\n"

subject="Daily Service Counter Usage Log",
daily_log = []


This application captures an image using the capture_image function, then submits it to the Groundlight API for analysis. If a customer is detected at the counter, it logs the event. Every hour, it prints a summary of the counter's usage percentage, and at the end of the day, it emails the daily log using the send_email function.

Save the script as and run it: