How to Control Crowds with Python, OpenCV and InfluxDB

Navigate to:

As I mentioned a while ago, I am learning to develop in Python; it seems that it is a super easy language to interpret, and so far, I have not found a “no” when trying to do things with it. I am delighted, and I keep learning.

In this learning process, I started to play with OpenCV (Open Source Computer Vision Library) to detect faces. OpenCV is an open-source, BSD-licensed library that includes several hundreds of computer vision algorithms. It did not take me long to understand how everything worked, and I began to think about InfluxDB time series use cases and how to integrate other technologies.

From that search came the idea of integrating OpenCV with InfluxDB to count the times it detects a face. Time series and biometrics are a perfect fit, I think, opening up numerous applications such as, but not limited to, the following:

  • Register the number of people coming through a specific entry point in a store at a particular moment of the day
  • Register the number of cars or other vehicles that pass for an avenue.
  • Count how many people stop and watch a specific type of product to understand if it's attractive to them
  • Measure customer conversion in retail and hospitality establishments, such as comparing restaurant guests walking in vs. plates sold
  • Overwatch crowds within a defined space and time (resources allocation, traffic light management, infrastructure improvements, etc.)

The last is very important considering the global context we live in. Controlling crowds to ensure public safety in a pandemic era has been challenging for law enforcement agencies worldwide.

Let's see some of the code...

The code of this project can be found in this GitHub repository. The relevant files are:

  • live.py
  • influxdb_config.ini
  • haarcascade_frontalface_default.xml

The file live.py looks as shown below. The only thing to modify, if desired, is the name of the bucket to match the bucket that we created in InfluxDB.

# This script will detect faces via your webcam and push the data to an InfluxDB instance.
# By Ignacio Van Droogenbroeck @hectorivand
 
import cv2
 
from datetime import datetime
 
from influxdb_client import Point, InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
 
# Specify the InfluxDB bucket
 
bucket = "crowd-counter"
 
# In this file, you must set the parameters to connect to InfluxDB.
 
client = InfluxDBClient.from_config_file('influxdb_config.ini')
 
write_api = client.write_api(write_options=SYNCHRONOUS)
query_api = client.query_api()
 
cap = cv2.VideoCapture(0)
 
# Create the haar cascade
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
 
while(True):
	# Capture frame-by-frame
	ret, frame = cap.read()
 
	# Our operations on the frame come here
	gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 
	# Detect faces in the image
	faces = faceCascade.detectMultiScale(
		gray,
		scaleFactor=1.2,
		minNeighbors=5,
		minSize=(30, 100)
	)
 
	# Write the quantity of faces detected on InfluxDB
 
	p = Point("public-count").tag("cameras", "entry").field("people", '{0}'.format(len(faces)))
	write_api.write(bucket=bucket, record=p)
 
	# Draw a rectangle around the faces
	for (x, y, w, h) in faces:
		cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
 
	# Display the resulting frame
	cv2.imshow('frame', frame)
	if cv2.waitKey(1) & 0xFF == ord('q'):
		break
 
# When everything is done, release the capture
cap.release()
cv2.destroyAllWindows()

The other file to modify is the influxdb_config.ini. This file has the data to connect to InfluxDB. As you can see, you need to define the values to connect to InfluxDB.

[influx2]
url=$INFLUX_URL
org=$INFLUX_ORG
token=$INFLUX_TOKEN
timeout=6000
verify_ssl=True

If you don’t have an instance of InfluxDB OSS running, I recommend you create an InfluxDB Cloud account (InfluxDB Cloud is a managed, elastic, serverless edition of InfluxDB). The free tier is more than enough to perform some tests. If you’ve decided to use this solution in a production environment, you can keep your Cloud account.

The last important file is the haarcascade_frontalface_default.xml. This file provides the models to detect faces. In the OpenCV: Cascade Classifier tutorial, I recommend reading about haarcascade and why it is an excellent option to detect objects without the need to train a TensorFlow model.

Now we have everything set up, so let’s run the code.

Let's count some faces...

Once this script is running, if you run the live.py script, it will activate your webcam to start counting faces and send that data to InfluxDB.

facial detection using python opencv influxdb

As you can see, this face detection approach is not so complicated. It can be used to control traffic flow on a highway or avenue.

vehicle detection using influxdb and python

Well, you might be thinking — all is beautiful, you have a pretty face, the cars look good too, but show me how to control crowds using this technology. Fair enough! Let me reply to your comment with this screenshot.

crowd detection solution opencv, influxdb, python

In this case, the script detected 110 faces and sent that data to InfluxDB. Imagine this example running through several devices in parks, restaurants or fairs.

As you may notice, I used a video because, well, I don’t have a crowd in my home! But the output will be the same; the only difference in this case is the input of the data (the video vs. a live feed).

Conclusion

This software solution isn’t perfect; it is part of my learning path. If you find some issues, I encourage you to fork and propose improvements in the GitHub Repository.

I hope this article has just given you a whole new use case for deploying InfluxDB.