👦 Build Your Own Live Body Detection App in 7 Minutes | Computer Vision | OpenCV

In this tutorial, I have touched you, how to create a Live body Detection App in only 7 Minutes. Its computer vision Artificial Intelligence project and I used OpenCV Python Library and Cascade Classifier.

Live Body Detection App

import cv2

# Load the Cascade Classifier
body_cascade = cv2.CascadeClassifier("haarcascade_fullbody.xml")

#startt  web cam
cap = cv2.VideoCapture('videos/Subway - 6398.mp4')

while True:
    
    #read image from webcam
    respose, color_img = cap.read()
    
    if respose == False:
        break
        
    # Convert to grayscale
    gray_img = cv2.cvtColor(color_img, cv2.COLOR_BGR2GRAY)
    
    # Detect the faces
    faces = body_cascade.detectMultiScale(gray_img, 1.1, 1)
    
    #display rectrangle
    for (x, y, w, h) in faces:
        cv2.rectangle(color_img, (x, y), (x+w, y+h), (0, 0, 255), 2)
        
        # display image
        cv2.imshow('img', color_img)
        
        #v_writer.write(color_img) 
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the VideoCapture object
cap.release()
cv2.destroyAllWindows()

Rectangle with 2 colors

import cv2

# Load the Cascade Classifier
body_cascade = cv2.CascadeClassifier("haarcascade_fullbody.xml")

#startt  web cam
cap = cv2.VideoCapture('videos/People - 6387.mp4')

while True:
    
    #read image from webcam
    respose, color_img = cap.read()
    
    if respose == False:
        break
        
    # Convert to grayscale
    gray_img = cv2.cvtColor(color_img, cv2.COLOR_BGR2GRAY)
    
    # Detect the faces
    faces = body_cascade.detectMultiScale(gray_img, 1.1, 1)
    
    #display rectrangle
    i=0
    for (x, y, w, h) in faces:
        if i%2==0:
            cv2.rectangle(color_img, (x, y), (x+w, y+h), (0, 0, 255), 2)
            i +=1
        else:
            cv2.rectangle(color_img, (x, y), (x+w, y+h), (0, 255, 0), 2)
            i +=1  
        
        # display image
        cv2.imshow('img', color_img)
        
        #v_writer.write(color_img) 
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the VideoCapture object
cap.release()
cv2.destroyAllWindows()

Leave a Reply