Detect Motion and intimate to the user | python

This IOT Project will allow you to detect the motion and also get intimate to the user via social media using python code.

What is Motion Detection?

Motion detection is the process of detecting a change in the position of an object relative to its surroundings.

Why Motion Detection?

Motion Detector is a device used to detect unauthorized movement in restricted areas. The device is used in commercial and residential properties, as well as in industrial and military.

Requirements

  • Raspberry PI 3
  • A WebCam
  • PIR Sensor

In this case, I will use my laptop so we need not worry about this hardware.

Software

  • Python 3
  • Open CV (libraries)
  • Pywhatkit(libraries)

LIBRARIES

Open CV (Computer Vision)

  • It is a huge open-source library for computer vision, machine learning, and image processing.
  • so it plays a more important role in real-time operations in daily life.
  • It can process an image as well as videos for identity objects.
  • IoT developers can use this OpenCV for more applications like motion detection and people detection.

Link for download Open CV package: https://pypi.org/project/opencv-python/

Pywhatkit

  • Pywhatkit is also an open-source library for sending messages in WhatsApp and Gmail.
  • it also contains many features other than that.
  • This library is mostly used by programmers to send WhatsApp messages.

Link for download pywhatkit package: https://pypi.org/project/pywhatkit/

The motion detection process

  • Grayscale conversion
  • Find the difference between two frames( Frame1, Frame2)
  • calculate the intensity of the object
  • Detection of contours
  • An intimate user via social media

Grayscale Frame

  • Grayscale conversion is like our old black and white tv. It converts our color frame into a monochromic form.
  • grayscale conversion is an easy way to calculate the difference in intensity.
  • It is mostly used in image and video processing to identify objects in a shorter time.

Difference Frame

The difference frame shows the difference of intensities of the frame1 to the frame2

Threshold Frame

If the intensity difference for a particular pixel is more than 30 then that pixel will be white and if the difference is less than 30 that pixel will be black.

Color Frame

In the current frame, if the object is moving we noticed that one green color box is surrounded in that movement place.

Intimate to users

Once the program detects the object is moving, then suddenly inmate to the user via social media. In this case, I use WhatsApp.

Program using Python

#Motion Detection and intimate to user

import cv2
import pywhatkit

count = 0
cam = cv2.VideoCapture(0)
while cam.isOpened():
    ret, frame1 = cam.read()
    ret, frame2 = cam.read()
    diff = cv2.absdiff(frame1, frame2)
    gray = cv2.cvtColor(diff, cv2.COLOR_RGB2GRAY)
    blur = cv2.GaussianBlur(gray, (5, 5), 0)
    _, thresh = cv2.threshold(blur, 30, 255, cv2.THRESH_BINARY)
    dilated = cv2.dilate(thresh, None, iterations=3)
    contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    for c in contours:
        if cv2.contourArea(c) < 5000:
            continue
        count += 1
        x, y, w, h = cv2.boundingRect(c)
        cv2.rectangle(frame1, (x, y), (x + w, y + h), (0, 255, 0), 2)
        
        print(count)

    if count == 0:
        pass
    elif count % 10 == 0: # After 10 movements it will send message
        # Give a whatsapp Number
        pywhatkit.sendwhatmsg_instantly('+91xxxxxxxxxx','Motion was detected')
        

    if cv2.waitKey(10) == ord('q'):
        break

    # Displaying image in gray_scale
    cv2.imshow("Gray Frame", gray)

    # Displaying the difference
    cv2.imshow("Difference Frame", diff)

    # Displaying the intensity
    cv2.imshow("Threshold Frame", thresh)


    # Displaying color frame with contour
    cv2.imshow("Color Frame", frame1)

# Destroying all the windows
cv2.destroyAllWindows()

My Previous Blog

How to Create and Extract Zip file in Python: https://iterathon.tech//how-to-create-and-extract-zip-file-in-python/

LEARN SOMETHING NEW ❣

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *