Skip to main content

Helping small businesses better deal with Covid-19 capacity restrictions with Computer Vision

 Helping small businesses deal with Covid-19 capacity restrictions with Computer Vision

Plug it, point the camera towards the inside of the bar or shop, and you'll know whether the next customer can come in or not.



(before starting just letting you know that you can find all the files, scripts, pictures, schematics and so on in this link to my Google Drive https://drive.google.com/drive/folders/17I19sAAR66CAnyozrhP9EZvPkjmSdxDz?usp=sharing)

Continuing on what I did last week, I wanted to come up with some other ideas to help people fight against the struggles Covid-19 pandemic and the restrictions that had come with it. So I thought that bars and shops indoors might be having trouble trying to constantly monitor the number of people inside and that it would be cool to design something to help alleviate that extra workload.

This is what I came up with:

This Raspberry-Pi-based device has its main advantage that it doesn't require any prior knowledge on the matter. It's completely "plug and play" and only has 3 "major" components:

  • The box with the hardware and software and the screen (a more advanced one could be used or one with more digits but I didn't have around) 
  • A webcam, anyone will do, it doesn't need to have a very good resolution.
  • The power supply, a smartphone one will do the job too, although it would be a good idea to have a dedicated one for Raspberry Pi.
It really is that simple, you can see a test in a bar in my hometown. Unfortunately, I cannot go carrying around a screen, keyboard, and mouse so I couldn't record the screen of the Raspberry Pi but you can see how it detects the number of people that webcam captures and how then it displays the number of the little screen of the box.


But here below you can see some tests I did at home capturing the screen, when it detects my face it shows "1" on the counter and when it detects nothing because I cover the webcam it goes to "0".



Regarding the prices of the components that I used, the main ones are almost the same as the ones in this entry: https://achefethings.blogspot.com/2021/05/computer-vision-to-fight-against.html, this is somehwere below 100 euros for the Raspi, the camera and the power supply, and really the cables, resistances and screen display all together would be below 5 euros, so yes I would say all in all with a tight budget a lot of nice things can be done.

The software

The Raspberry Pi runs a Debian port OS, I out this little tiny script on its desktop you can see below, and then in the cmd, I had to type the following command lines to install OpenCV for Python to order the Raspberry Pi to run a script on boot.


There is one more thing that you need to have this running on your device, the haarcascades .xml files that you can find in this GitHub repository https://github.com/opencv/opencv/tree/master/data/haarcascades and by the way, you have haar_cascades classifiers for a bunch of things ranging from different features of people to cats, hand or Russian license plates. 

Run script on the boot

And btw these lines of commands ser good for any Linux thing,

sudo crontab -e

@reboot python "path to your file" &

Then Control+X, then Y, and finally Enter and you are done.

OpenCV

sudo apt-get install libhdf5-dev libhdf5-serial-dev libatlas-base-dev libjasper-dev libqtgui4 libqt4-test

pip3 install opencv-contrib-python

pip3 install opencv-contrib-python==4.1.0.25

Then you can check out whether the package was correctly installed or not by doing:

import cv2

cv2.__version__

# este es el bueno
import cv2 as cv
import RPi.GPIO as GPIO

display_list = [17,27,22,10,9,11,5]
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
for pin in display_list:
GPIO.setup(pin,GPIO.OUT) # setting pins
GPIO.setup(6,GPIO.OUT) # setting dot pin
GPIO.setwarnings(True)

arrSeg = [[1,1,1,1,1,1,0],\
[0,1,1,0,0,0,0],\
[1,1,0,1,1,0,1],\
[1,1,1,1,0,0,1],\
[0,1,1,0,0,1,1],\
[1,0,1,1,0,1,1],\
[1,0,1,1,1,1,1],\
[1,1,1,0,0,0,0],\
[1,1,1,1,1,1,1],\
[1,1,1,1,0,1,1]]

vid = cv.VideoCapture(0)

while True:
ret, frame = vid.read()
gray_frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
haar_cascade = cv.CascadeClassifier("/home/pi/Desktop/haarcascade.xml")
faces_rect = haar_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=4)

for (x, y, w, h) in faces_rect:
cv.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), thickness=2)

cv.imshow("frame", frame)
if cv.waitKey(1) & 0xFF == ord("q"):
break
n_faces = len(faces_rect)
numDisplay = int(n_faces)
if numDisplay == 10:
GPIO.cleanup()
else:
GPIO.output(display_list, arrSeg[numDisplay])
vid.release()
cv.destroyAllWindows()


The hardware

You'll need the following to make your own people counter for your bar:
  • Any camera that has a USB cable
  • Raspberry-Pi
  • Power-supply
  • 8 330 Ohm resistance
  • Breadboard
  • About 25 male-female jumper wires unless you are looking forward to soldering the connections
  • 1 digit 7 segment screen
  • 3D printed enclosure to keep it all tight and safe
Regarding the schematics of the connections, you can see down here a picture of the thing, then the power supply connects to the Power-Supply connection on the left of the Pi, and then the Webcam to any of the USB connections that your Pi has. Note that this is not the Pi I'm using but for the sake of illustrating the problem is enough.


Here you can see some actual pictures of my connections and all the wiring I had to do to have this beast working properly.


And I think I am just missing the enclosure, which was designed in Fusion360, my go-to 3D modelling software as always, and printed in my good ol' reliable Ender 3 with some random PLA I had hanging around (in fact, I had many problems printing it I hope to have some time to service it soon). Down below you can see some snapshots of the 3D models in the slicer software (Ultimaker Cura).




Conclusions

It took me 3 days to build this during my free time, I had many unforeseen events happening like changing completely my design halfway through since I was not being able to establish I2C communications between the Raspberry Pi and the Arduino and so I had to opt for another approach. But after encountering and solving all these issues: oh boy, I am so happy with the result and I learnt so much from it.


I hope you enjoyed reading the entry as much I enjoyed preparing it, it's been by far the most complex thing I've done so far and I am so proud of it.


If you want to try it for yourself and you have any doubt, contact me and I'll be more than happy to respond to your questions.


Hope you like it,


Pablo












f

Comments

Popular posts from this blog

A first approach to IoT, connecting my 3D printer to the internet

My first approach to the IoT, connecting my 3D printer to the internet IoT is one of those fancy words that people like to talk about in conferences and in TedTalks without (apparently) having too much idea of what it is all about. Set up to manage the 3D printer through the internet This one is going to be a short entry where I don't go through code or anything, just wanted to talk about a bit about how I connected my 3D printer to the internet.  I've been in the 3D printing thing for a while now, about a year and I haven't stopped printing ever since I bought my Ender 3. Fortunately enough, I live in a big house where my room/working place is on the fourth floor and my 3D printing is on the first one. You might be thinking as well: "OK Pablo but where do you want to bring us? Go to the point" Well, as you might have noticed there are two floors in betw...

Advent of Code, day 2

We made it to Day 2 of Advent of Code I have kept my promise for 2 days in a row. As weird as it seems for this day and age, I have kept my promise and today is the day 2 of the Advent of Code, this time with elves involved. There you go, the statement of today's first problem down below: And usual, my solution: import pandas as pd import re import numpy as np filepath = "/content/input 1.txt" with open (filepath, 'r' ) as file :     lines = [line.strip() for line in file .readlines()] lines_split = [] for line in lines:   token = re.findall(r '\w+|[^\s\w]' , line)   lines_split.append(token) blue_list = [] green_list = [] red_list = [] for line in lines_split:   blue,green,red = 0 , 0 , 0   for ix, word in enumerate (line):     if word == "blue" :       if blue < int (line[ix -1 ]):         blue = int (line[ix -1 ])     if word == "green" :       if green < int (...

You can know talk to @CastroTiempo and not only that, it will respond you

You can now talk to @CastroTiempo Twitter bot, and not only that, it will respond you! Implementing some simple interaction capabilities as the previous step to Natural Processing Language implementation, this time you can ask it how the weather is like in basically any city around the world. Hola @luisvolumentres , en Kiruna hay cielos poco nubladoss y la temperatura es de 1.0 ºC, para obtener mas información pincha en https://t.co/5dO5q6RkKX — El Tiempo en Castro (@CastroTiempo) April 14, 2021 So I was tinkering about how I could improve the @CastroTiempo Twitter Bot as it had many flaws and it still does. The first ideas that came to my mind were the following ones: Forecasting capabilities Interaction capabilities Update the weather report it provides with the temperature (obviously) and whether it was raining there were clear skies or whatever. To put the data in a public access database so there's a repository of information that every...