Python+OpenCV hybrid Gaussian modeling algorithm human body recognition entrance and exit pedestrian flow statistics and counting

Selected program examples include Python+OpenCV hybrid Gaussian modeling algorithm, human body recognition, entrance and exit pedestrian flow statistics and counting. If you need to install the operating environment or remote debugging, please see your personal QQ business card at the bottom of the article for remote assistance from professional technicians!

Preface

This blog writes code for "Python+OpenCV Mixed Gaussian Modeling Algorithm Human Body Recognition Entrance and Exit People Flow Statistics and Counting". The code is clean, regular and easy to read. Recommended for learning and application.


operation result

Insert image description here


Article directory

1. Required tools and software
2. Usage steps
       1. Main code
       2. Operation results
3. Online assistance

1. Required tools and software

       1. Python
       2. Pycharm

2. Usage steps

The code is as follows (example):

import numpy as np
import math
import cv2
cap = cv2.VideoCapture('video02.mp4')
# fgbg = cv2.createBackgroundSubtractorMOG2(history=5, varThreshold=150)
def line1(x,y):
    return y - (29*x)/96.0 - 300
crossedAbove = 0
crossedBelow = 0
points = set()
pointFromAbove = set()
pointFromBelow = set()

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('pedestrianOutput.avi',fourcc, 25.0, (1920,1080))
font = cv2.FONT_HERSHEY_SIMPLEX
while(1):
    pointInMiddle = set()
    prev = points
    points = set()
    ret, frame = cap.read()
    fgmask = cv2.blur(frame, (4,4))
    fgmask = fgbg.apply(fgmask)
    oldFgmask = fgmask.copy()
    oldFgmask,contours, hierarchy = cv2.findContours(fgmask, cv2.RETR_EXTERNAL,1)
    for contour in contours:
        x,y,w,h = cv2.boundingRect(contour)
            cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),6, lineType=cv2.LINE_AA)
            point = (int(x+w/2.0), int(y+h/2.0))
            points.add(point)
    for point in points:
        (xnew, ynew) = point
        if line1(xnew, ynew) > 0 and line2(xnew, ynew) < 0:
            pointInMiddle.add(point)
        for prevPoint in prev:
            (xold, yold) = prevPoint
            dist = cv2.sqrt((xnew-xold)*(xnew-xold)+(ynew-yold)*(ynew-yold))
                        pointFromAbove.add(point)
                    elif line2(xold, yold) > 0: # Point entered from line below
                        pointFromBelow.add(point)
                    else:   # Point was inside the block
                        if prevPoint in pointFromBelow:
                            pointFromBelow.remove(prevPoint)
                            pointFromBelow.add(point)

                        elif prevPoint in pointFromAbove:
                            pointFromAbove.remove(prevPoint)
                            pointFromAbove.add(point)

                if line1(xnew, ynew) < 0 and prevPoint in pointFromBelow: # Point is above the line
                    print('One Crossed Above')
                    print(point)
                    crossedAbove += 1
                    pointFromBelow.remove(prevPoint)

                if line2(xnew, ynew) > 0 and prevPoint in pointFromAbove: # Point is below the line
                    print('One Crossed Below')
                    print(point)
                    crossedBelow += 1

    for point in points:
        if point in pointFromBelow:
            cv2.circle(frame, point, 3, (255,0,255),6)
        elif point in pointFromAbove:
            cv2.circle(frame, point, 3, (0,0,255),6)
    cv2.line(frame, (0,300), (1920,880), (255, 0, 0), 4)
    cv2.line(frame, (0,500), (1920,1080), (255, 0, 0), 4)
    cv2.putText(frame,'People Going Above = '+str(crossedAbove),(800,50), font, 2,(255,255,255),5,cv2.LINE_AA)
    cv2.putText(frame,'People Going Below = '+str(crossedBelow),(800,100), font, 2,(255,255,255),5,cv2.LINE_AA)
    cv2.putText(frame,'People Going Total  = '+str(crossedBelow+crossedAbove),(800,150), font, 2,(255,255,255),5,cv2.LINE_AA)
    
    cv2.namedWindow("frame", 0);
    cv2.resizeWindow("frame", 540, 380);
    cv2.moveWindow("frame",100,100)
    #cv2.imshow('oldFgmask',oldFgmask)
    cv2.imshow('frame',frame)
    out.write(frame)
    l = cv2.waitKey(1) & 0xff
    if l == 27:
        break
cap.release()
cv2.destroyAllWindows()

operation result

Insert image description here

3. Online assistance:

If you need to install the operating environment or remote debugging, please see your personal QQ business card at the bottom of the article for remote assistance from professional technicians!

1) Remote installation and running environment, code debugging
2) Visual Studio, Qt, C++, Python programming language introductory guide
3) Interface beautification
4) Software production 5
) Cloud server application
6) Website production

Current article link: https://blog.csdn.net/alicema1111/article/details/132666851
Personal blog homepage : https://blog.csdn.net/alicema1111?type=
All articles by blogger click here: https:/ /blog.csdn.net/alicema1111?type=blog

Recommended by bloggers:
Python face recognition attendance punching system:
https://blog.csdn.net/alicema1111/article/details/133434445
Python fruit tree fruit recognition : https://blog.csdn.net/alicema1111/article/details/ 130862842
Python+Yolov8+Deepsort entrance traffic statistics: https://blog.csdn.net/alicema1111/article/details/130454430
Python+Qt face recognition access management system: https://blog.csdn.net/alicema1111/ article/details/130353433
Python+Qt fingerprint entry recognition attendance system: https://blog.csdn.net/alicema1111/article/details/129338432
Python Yolov5 flame smoke recognition source code sharing: https://blog.csdn.net/alicema1111 /article/details/128420453
Python+Yolov8 road bridge wall crack identification: https://blog.csdn.net/alicema1111/article/details/133434445

Guess you like

Origin blog.csdn.net/alicema1111/article/details/135167717