faldet

A simple fall-detection algorithm using opencv
Log | Files | Refs | LICENSE

faldet.py (1138B)


      1 import numpy as np
      2 import cv2
      3 import imutils
      4 
      5 # cap = cv2.VideoCapture('/home/zerous/src/video-fall-detection/queda.mp4')
      6 cap = cv2.VideoCapture(0)
      7 bgsub = cv2.createBackgroundSubtractorMOG2()
      8 
      9 while(True):
     10     ret, frame = cap.read()
     11 
     12     fgmask = bgsub.apply(frame)
     13 
     14     # apply some filter to smoothen things out
     15     blur = cv2.GaussianBlur(fgmask, (5, 5), 0)
     16     avg = cv2.threshold(blur, 60, 255, cv2.THRESH_BINARY)[1]
     17 
     18     contours, hierarchy = cv2.findContours(avg, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
     19     for c in contours:
     20             M = cv2.moments(c)
     21             image = cv2.drawContours(frame, contours, -1, (0, 255, 0), 2)
     22             if M["m00"] != 0:
     23                 cX = int(M["m10"] / M["m00"])
     24                 cY = int(M["m01"] / M["m00"])
     25                 image = cv2.circle(image, (cX, cY), 7, (255, 255, 255), -1)
     26                 image = cv2.putText(image, "center", (cX - 20, cY - 20),
     27                                     cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
     28             cv2.imshow('win1', image)
     29 
     30     k = cv2.waitKey(30) & 0xff
     31     if k == 27:
     32         break
     33 
     34 cap.release()
     35 cv2.destroyAllWindows()