COMPUTER VISION PROJECT THEFT DETECTION WITH OPENCV LIBRARY

Karan Choudhary
3 min readJun 25, 2020

Computer vision works on enabling computers to see, identify and process images in the same way that human vision does, and then provide appropriate output. It is like imparting human intelligence.

In this blog we are doing theft detection through webcam and generating a text while theft is taking place which would help him to trace the position of the object initially and mask for the object around its movement around the video.

Before coming to implementation of code do visit the artice: INTRODUCTION TO COMPUTER VISION for the basics of computer vision

and go familiar with the it.

  1. Importing the libraries.

import cv2
import numpy as np

2.Capturing video through webcam .

capture = cv2.VideoCapture(0)

3.The algorithm will detect shadows and mark them.

Parameters:

int history = 500 ,

double varThreshold = 16 ,

bool detectShadows =true

fgbg = cv2.createBackgroundSubtractorMOG2(50, 200, True)
fgbg = cv2.createBackgroundSubtractorMOG2(300, 400, True)

4.Capturing the current frame and the pixel count of the video through webcam.

while(1):
ret, frame = capture.read()

# Check if a current frame actually exist
if not ret:
break

frameCount += 1
# Resize the frame
resizedFrame = cv2.resize(frame, (0, 0), fx=1.0, fy=1.0)

5. Assigning frame and count value .Then ,display the original and the mask for it.

if (frameCount > 1 and count > 5000):
print(‘Someones stealing your honey’)
cv2.putText(resizedFrame, ‘Alert!!!! Someones stealing your belongings’, (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)

cv2.imshow(‘Frame’, resizedFrame)
cv2.imshow(‘Mask’, fgmask)

6. Assigning wait key =27 means after 27 seconds it will exit.

k = cv2.waitKey(1)
if k == 27:
break

capture.release()
cv2.destroyAllWindows()

Complete code

import cv2
import numpy as np

# Video Capture through webcam
# capture = cv2.VideoCapture(“Any video.mp4/mov”)
capture = cv2.VideoCapture(0)

# History, Threshold, DetectShadows
fgbg = cv2.createBackgroundSubtractorMOG2(50, 200, True)
fgbg = cv2.createBackgroundSubtractorMOG2(300, 400, True)

# Keeps track of what frame we’re on
frameCount = 0

while(1):
# Return Value and the current frame
ret, frame = capture.read()

# Check if a current frame actually exist
if not ret:
break

frameCount += 1
# Resize the frame
resizedFrame = cv2.resize(frame, (0, 0), fx=1.0, fy=1.0)

# Get the foreground mask
fgmask = fgbg.apply(resizedFrame)

# Count all the non zero pixels within the mask
count = np.count_nonzero(fgmask)

print(‘Frame: %d, Pixel Count: %d’ % (frameCount, count))
# if (frameCount > 1 and count > 5000)
if (frameCount > 1 and count > 5000):
print(‘Someones stealing your honey’)
cv2.putText(resizedFrame, ‘ATTENTION!!! Someone stealing your things’, (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)

cv2.imshow(‘Frame’, resizedFrame)
cv2.imshow(‘Mask’, fgmask)

k = cv2.waitKey(1)
if k == 27:
break

capture.release()
cv2.destroyAllWindows()

Screenshot of the code:

OUTPUT:

frame displaying warning
Mask displaying the movement of object

If u love to read this article and have common interest in similar projects then we can grow our network and can work for more real time projects.

For more details connect with me on my Linkedin account!

THANKS!!!!

--

--