1

Greetings. My name is Agil Calfarera and currently, I'm working on a project to detect and calculating human's walking speed but I have an issue that bothering my code. I have successfully detecting every human that appears on the camera and marked them with a bounding box. But, the bounding box is unstable and bouncing as the human walks. I have tried to blur and changed it into gray but still, the boxes are still unstable. Then I've tried dilation and erosion but it's getting a little better but still unstable. How am I suppose to do to make the bounding box of detected human stable and not bouncing? Thanks in advance.

I have used haar cascade for fullbody and upperbody. Here's the code that I've made by using blur, erosion and dilation.

# Import necessary library
import cv2
import time
import numpy as np

cascade_src = 'haarcascade_upperbody.xml'
video_src = 'test.mpg'

# Draw line a
ax1=15
ay=125
ax2=300

# Draw line b
bx1=15
by=175
bx2=300

# Assign Human ID number
i = 1
start_time = time.time()

# Initiate video streaming
cap = cv2.VideoCapture(video_src)
human_cascade = cv2.CascadeClassifier(cascade_src)   

videoWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
videoHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# Set video name to write
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output-55.mp4', fourcc, 30.0, (videoWidth,videoHeight))

def Speed_Cal(time):
    #Here i converted m to Km and second to hour then divison to reach Speed in this form (KM/H) 
    #the 9.144 is distance of free space between two lines # found in https://news.osu.edu/
    try:
        Speed = (9.144*3600)/(time*1000)
        return Speed
    except ZeroDivisionError:
        print (5)

while True:
    ret, img = cap.read()
    if (type(img) == type(None)):
        break
    
    # Bluring to have exacter detection
    #blurred = cv2.blur(img, ksize=(10,10))
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Taking a matrix of size 5 as the kernel
    kernel = np.ones((3, 3), np.uint8)

    # Erosion and dilation image to smooth the edge of video
    erosion = cv2.erode(gray, kernel, iterations=1)
    dilation = cv2.dilate(erosion, kernel, iterations=1)
    #opening = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel)
    human = human_cascade.detectMultiScale(dilation, 1.1, 2)
    
    #line a #i know road has got 
    cv2.line(img,(ax1, ay),(ax2, ay),(255,0,0),1)
    #line b
    cv2.line(img,(bx1, by),(bx2, by),(255,0,0),1)
    
    for (x,y,w,h) in human:
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)   
        cv2.circle(img,(int((x+x+w)/2), int((y+y+h)/2)), 1, (0,255,0), -1)
        
        while int(ay) == int((y+y+h)/2):
            start_time = time.time()
            break
            
        while int(ay) <= int((y+y+h)/2):
            if int(by) <= int((y+y+h)/2)&int(by+10) >= int((y+y+h)/2):
                cv2.line(img,(bx1,by),(bx2,by), (0,255,0), 2)
                Speed = Speed_Cal(time.time() - start_time)
                print("ID Number "+str(i)+" Speed: "+str(int(Speed)))
                i = i + 1
                cv2.putText(img, "Speed: "+str(int(Speed))+"km/j", (x, y-15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 1);
                break
            else :
                cv2.putText(img, "Calculating", (100,200), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 1)
                break
                
                
    out.write(img)
    cv2.imshow('video', img)
    #cv2.imshow('erotion', blurred)
    #cv2.imshow('gray', gray)
    #cv2.imshow('dilation', dilation)
    cv2.imshow('erosion', erosion)
    #cv2.imshow('Opening', opening)
    
    if cv2.waitKey(33) == 27:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

Please, I hope someone can help me and I will very happy.

0 Answers0