This was my first python programming experience so apols [đ]
I am just hacking the original as⌠is the same and its all Ninja this side.
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=3,
minSize=(20, 20),
flags=cv2.cv.CV_HAAR_DO_CANNY_PRUNING | cv2.cv.CV_HAAR_FIND_BIGGEST_OBJECT | cv2.cv.CV_HAAR_DO_ROUGH_SEARCH
)
I modded the original slightly and noticed that your code is a bit of a fudge in terms on pan-tilt
I had a go myself at detection -> move to absolute position an get round to it.
#!/usr/bin/env python
# USAGE
# python motion_detector.py
# python motion_detector.py --video videos/example_01.mp4
# import the necessary packages
import argparse
import datetime
import imutils
import time
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the video file")
ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size")
args = vars(ap.parse_args())
# if the video argument is None, then we are reading from webcam
if args.get("video", None) is None:
camera = cv2.VideoCapture(0)
time.sleep(0.25)
# otherwise, we are reading from a video file
else:
camera = cv2.VideoCapture(args["video"])
# initialize the first frame in the video stream
firstFrame = None
# loop over the frames of the video
while True:
# grab the current frame and initialize the occupied/unoccupied
# text
(grabbed, frame) = camera.read()
text = "Unoccupied"
# if the frame could not be grabbed, then we have reached the end
# of the video
if not grabbed:
break
# resize the frame, convert it to grayscale, and blur it
frame = imutils.resize(frame, width=500)
frame = cv2.flip(frame, -1)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
# if the first frame is None, initialize it
if firstFrame is None:
firstFrame = gray
continue
# compute the absolute difference between the current frame and
# first frame
frameDelta = cv2.absdiff(firstFrame, gray)
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
# dilate the thresholded image to fill in holes, then find contours
# on thresholded image
thresh = cv2.dilate(thresh, None, iterations=2)
(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# loop over the contours
for c in cnts:
# if the contour is too small, ignore it
if cv2.contourArea(c) < args["min_area"]:
continue
# compute the bounding box for the contour, draw it on the frame,
# and update the text
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
text = "Occupied"
# draw the text and timestamp on the frame
cv2.putText(frame, "Room Status: {}".format(text), (10, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
(10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
# show the frame and record if the user presses a key
cv2.imshow("Security Feed", frame)
cv2.imshow("Thresh", thresh)
cv2.imshow("Frame Delta", frameDelta)
key = cv2.waitKey(1) & 0xFF
# if the `q` key is pressed, break from the lop
if key == ord("q"):
break
# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()
Then the alternative C api rather than the C++ one
[code]
#!/usr/bin/env python
#You might want to remark the tickcount calls and print statements after bug checking and analysis
import numpy as np
import cv2.cv as cv
cascade = cv.Load('/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml')
min_size = (20, 20)
image_scale = 4
haar_scale = 1.2
min_neighbors = 2
haar_flags = cv.CV_HAAR_DO_CANNY_PRUNING | cv.CV_HAAR_FIND_BIGGEST_OBJECT | cv.CV_HAAR_DO_ROUGH_SEARCH
t = 0
cap = cv.CreateCameraCapture(0)
cv.NamedWindow("Tracker", 1)
if cap:
frame_copy = None
while(True):
# Capture frame-by-frame
t = cv.GetTickCount()
frame = cv.QueryFrame(cap)
if not frame:
cv.WaitKey(0)
break
if not frame_copy:
frame_copy = cv.CreateImage((frame.width,frame.height),
cv.IPL_DEPTH_8U, frame.nChannels)
if frame.origin == cv.IPL_ORIGIN_TL:
cv.Flip(frame, frame, -1)
t = cv.GetTickCount() - t
print(t), 'Tickcount get image'
# Our operations on the frame come here
t = cv.GetTickCount()
gray = cv.CreateImage((frame.width,frame.height), 8, 1)
small_img = cv.CreateImage((cv.Round(frame.width / image_scale),
cv.Round (frame.height / image_scale)), 8, 1)
# convert color input image to grayscale
cv.CvtColor(frame, gray, cv.CV_BGR2GRAY)
# scale input image for faster processing
cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)
cv.EqualizeHist(small_img, small_img)
midFace = None
t = cv.GetTickCount() - t
print(t), 'Tickcount create detect image'
if(cascade):
t = cv.GetTickCount()
# HaarDetectObjects takes 0.02s
faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),
haar_scale, min_neighbors, haar_flags, min_size)
t = cv.GetTickCount() - t
t = cv.GetTickCount()
print(t), 'Tickcount Detect objects'
if faces:
for ((x, y, w, h), n) in faces:
# the input to cv.HaarDetectObjects was resized, so scale the
# bounding box of each face and convert it to two CvPoints
pt1 = (int(x * image_scale), int(y * image_scale))
pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
cv.Rectangle(frame, pt1, pt2, cv.RGB(100, 220, 255), 1, 8, 0)
# get the xy corner co-ords, calc the midFace location
x1 = pt1[0]
x2 = pt2[0]
y1 = pt1[1]
y2 = pt2[1]
midFaceX = x1+((x2-x1)/2)
midFaceY = y1+((y2-y1)/2)
midFace = (midFaceX, midFaceY)
print midFace, 'Face center'
t = cv.GetTickCount() - t
print(t), 'Tickcount faces'
# Display the resulting frame
t = cv.GetTickCount()
cv.ShowImage('Tracker',frame)
t = cv.GetTickCount() - t
print(t), 'Tickcount Show image'
if cv.WaitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cv.DestroyWindow("Tracker")
[/code]