Human Crowd Detection with public Webcam|Part 1

Introduction

Human crowd detection is one of the widely used features by governments and organizations these days, to control crimes or to detect if there are cases of fainting or to identify any movement around premises. The proposed system was implemented using OpenCV and YoloV5 with python to detect a person’s movements.

PDF] Enhancing human detection using crowd density measures and an adaptive  correction filter | Semantic Scholar

To implement this project you need to know 5 main parts:

1- OpenCV

2- What’s CNN (Convolution Neural Network)? and what are its types?

3- What are YOLO versions? different between them?

4- Data Annotation

5- Project Implementation

OpenCV Introduction

let’s start Introduction to OpenCV (Open Source Computer Vision).

OpenCV is one of the most popular computer vision libraries. If you want to start your journey in the field of computer vision, then a thorough understanding of the concepts of OpenCV is of paramount importance.

In this article, I will try to introduce the most basic and important concepts of OpenCV in my project.

We begin by installing the OpenCV library:

pip install opencv-python

1- Reading image:

# Importing the OpenCV library
import cv2
# Reading the image using imread() function
image = cv2.imread('image.png')
  
# Extracting the height and width of an image
h, w = image.shape[:2]
# Displaying the height and width
print("Height = {},  Width = {}".format(h, w))

2- extract the RGB values of an individual pixel

The Convolution/Pooling Operation For RGB Images - The Click Reader
# Extracting RGB values. 
(B, G, R) = image
  
# Displaying the pixel values
print("R = {}, G = {}, B = {}".format(R, G, B))
  
# We can also pass the channel to extract 
# the value for a specific channel
B = image[100, 100, 0]
print("B = {}".format(B))

Note: OpenCV arranges the channels in BGR order. So the 0th value will correspond to the Blue pixel and not Red.

3- Resizing the image

# resize() function takes 2 parameters, the image and the dimensions
resize = cv2.resize(image, (800, 800))

4- Capture Video from Camera:

Often, we have to capture a live stream with a camera. OpenCV provides a very simple interface to do this.
Let’s capture a video from the camera (I am using the built-in webcam on my laptop), convert it into grayscale filter video, and display it. Just a simple task to get started.

#if numpy didn't install uncomment the next line
#!pip install numpy

import numpy as np
import cv2 as cv

# Normally one camera will be connected (as in my case) so pass 0 (or -1).
cap = cv.VideoCapture(0)

# If it is True, OK. Otherwise open it using cap.open().
if not cap.isOpened():
    print("Cannot open camera")
    exit()

while True:

    # Capture frame-by-frame
    ret, frame = cap.read()

    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break

    # Our operations on the frame come here
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv.imshow('frame', gray)
    if cv.waitKey(1) == ord('q'):
        break
# When everything done, release the capture
cap.release() 
cv.destroyAllWindows()

5- Playing Video from file

Playing video from a file is the same as capturing it from the camera, just change the camera index to a video file name. Also while displaying the frame, use the appropriate time for cv.waitKey(). If it is too less, the video will be very fast, and if it is too high, the video will be slow (Well, that is how you can display videos in slow motion).

25 milliseconds will be OK in normal cases.

import numpy as np
import cv2 as cv
cap = cv.VideoCapture('test.mp4')
while cap.isOpened():
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    cv.imshow('frame', gray)
    if cv.waitKey(1) == ord('q'):
        break
cap.release()
cv.destroyAllWindows()

6- Thresholding

Here, the matter is straightforward. For every pixel, the same threshold value is applied. If the pixel value is smaller than the threshold, it is set to 0, otherwise, it is set to a maximum value. The function cv. the threshold is used to apply the thresholding. The first argument is the source image, which should be a grayscale image.

The second argument is the threshold value which is used to classify the pixel values. The third argument is the maximum value which is assigned to pixel values exceeding the threshold.

OpenCV provides different types of thresholding which are given by the fourth parameter of the function. Basic thresholding as
described above is done by using the type cv.THRESH_BINARY

All simple thresholding types are:

  1. cv.THRESH_BINARY
  2. cv.THRESH_BINARY_INV
  3. cv.THRESH_TRUNC
  4. cv.THRESH_TOZERO
  5. cv.THRESH_TOZERO_INV

This code compares the different simple thresholding types:

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('gradient.png',0)

ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
ret,thresh2 = cv.threshold(img,127,255,cv.THRESH_BINARY_INV)
ret,thresh3 = cv.threshold(img,127,255,cv.THRESH_TRUNC)
ret,thresh4 = cv.threshold(img,127,255,cv.THRESH_TOZERO)
ret,thresh5 = cv.threshold(img,127,255,cv.THRESH_TOZERO_INV)

titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
    plt.subplot(2,3,i+1),plt.imshow(images[i],'gray',vmin=0,vmax=255)
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

Conclusion

In this part, we learned about the concept of reading images, extracting the RGB values of an individual pixel, opening video streaming, extracting images from it, playing with existing video, and applying Thresholding to detect interesting parts using OpenCV in Python which can be found in the library.

In the next part, we will learn about CNN and its types

References

  1. https://docs.opencv.org/3.4/

Facebook
Twitter

Unlimited access to educational materials for subscribers

Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.
Hi, Welcome back!
Forgot?
Don't have an account?  Register Now