HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Motion detection program for snapping of images

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
snappingprogramfordetectionmotionimages

Problem

I'm doing a motion detection program where it snaps an image when it detects movement and snaps an image of the person's face if in view while this is all recorded and sends it all to Dropbox.

It's moving very slowly and lagging like crazy, showing 1 frame in like a minute. Is there a way to optimize it?

I'm using a Raspberry Pi to code all this, and a webcam.

```
import sys
sys.path.append('/usr/local/lib/python3.4/site-packages')
import numpy as np
import cv2
import imutils
from imutils import contours
import datetime
import time
import dropbox

#Function fo Drawing rect and changing text to REC
def draw_rect_movement(c):
#Draw Rectangle around found contour object
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 2)
text = "REC"
return c

def saveNupload(roi_color):
#writing image of face as png in the file
timestring = time.strftime("%Y_%m_%d_%H_%M_%S")
face_timestr = 'face_' + timestring + '.png'
cv2.imwrite(face_timestr, roi_color)

#Opening for [r]eading as [b]inary
FaceFile = open(face_timestr, mode = "rb")
#Reads the number of bytes of the video
data = FaceFile.read()

#Setting the save location with file name
SavetoLocation = '/FYP_Face_Save/'+ face_timestr
SaveToLocation = str(SavetoLocation)

dbx.files_upload(data, SaveToLocation)
#Close for reading and binary
FaceFile.close()

dbx = dropbox.Dropbox('Access Token')
dbx.users_get_current_account()

#cap = cv2.VideoCapture("/home/pi/Desktop/Proj/VideoTestSample.mp4")
cap = cv2.VideoCapture(1)

#Creating froeground and removing Background
fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows=False)

#Set format
fourcc = cv2.VideoWriter_fourcc(*'XVID')
#Get Datetime
timestr = time.strftime("%Y_%m_%d_%H_%M_%S")
#Creating name of folder
timestr = timestr + '.avi'
#Setting Name, Format, FPS, FrameSize
out = cv2.VideoWriter(timestr,fourcc, 10.0, (640, 480))

#setting casacade for use
face_c

Solution

sys.path.append('/usr/local/lib/python3.4/site-packages')


Recommend you use a proper package manager to install numpy and friends,
such as conda, or pip virtualenv.

(x, y, w, h) = cv2.boundingRect(c)


No need for ( extra parens ) on the tuple unpack.
Recommend you run $ flake8, and heed its advice,
preferring identifiers like e.g. save_and_upload or face_file.

SaveToLocation = str(SavetoLocation)


You already had a str, so the function call does nothing.

#Creating froeground


Typo.

while (cap.isOpened()):


No need for ( extra parens ).
Same remark for the grabbed, frame tuple unpack.

detect= None 
            if detect != (_,cnts,hierarchy):
                continue


An unconditional continue would suffice.

The while loop in __main__ is far too long, and should be packaged up
in one or more helper functions.

You didn't post any profiling / timing data, but
I assume you spend the bulk of elapsed time here:

faces = face_cascade.detectMultiScale(gray, 1.2)


Following the advice of BKSpurgeon and Aleksandar,
it would make sense to guard this with some cheap check for changed pixels,
perhaps using cv2.absdiff(),
before requesting the full-blown face finder.
Histograms certainly are a good way of summarizing images and noticing gross differences.

Code Snippets

sys.path.append('/usr/local/lib/python3.4/site-packages')
(x, y, w, h) = cv2.boundingRect(c)
SaveToLocation = str(SavetoLocation)
#Creating froeground
while (cap.isOpened()):

Context

StackExchange Code Review Q#163369, answer score: 2

Revisions (0)

No revisions yet.