Skip to content

Instantly share code, notes, and snippets.

@khaidir
Forked from npinto/cv2_detect.py
Last active September 7, 2015 06:45
Show Gist options
  • Select an option

  • Save khaidir/c1caa9839d2e980437aa to your computer and use it in GitHub Desktop.

Select an option

Save khaidir/c1caa9839d2e980437aa to your computer and use it in GitHub Desktop.

Revisions

  1. @npinto npinto revised this gist Sep 5, 2012. 25 changed files with 577403 additions and 0 deletions.
    34 changes: 34 additions & 0 deletions cv2_detect_demo.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    import time
    from cv2_detect import detect
    import cv2
    import cv2.cv as cv


    def draw_rects(img, rects, color):
    for x1, y1, x2, y2 in rects:
    cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)


    def demo(in_fn, out_fn):
    print ">>> Loading image..."
    img_color = cv2.imread(in_fn)
    img_gray = cv2.cvtColor(img_color, cv.CV_RGB2GRAY)
    img_gray = cv2.equalizeHist(img_gray)
    print in_fn, img_gray.shape

    print ">>> Detecting faces..."
    start = time.time()
    rects = detect(img_gray)
    end = time.time()
    print 'time:', end - start
    img_out = img_color.copy()
    draw_rects(img_out, rects, (0, 255, 0))
    cv2.imwrite(out_fn, img_out)


    def main():
    demo('pic.jpg', 'pic.detect.jpg')


    if __name__ == '__main__':
    main()
    4 changes: 4 additions & 0 deletions cv2_detect_demo.py.stdout
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    >>> Loading image...
    pic.jpg (2400, 3600)
    >>> Detecting faces...
    time: 8.11453986168
    15,452 changes: 15,452 additions & 0 deletions haarcascades/haarcascade_eye.xml
    15,452 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    33,158 changes: 33,158 additions & 0 deletions haarcascades/haarcascade_eye_tree_eyeglasses.xml
    33,158 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    26,161 changes: 26,161 additions & 0 deletions haarcascades/haarcascade_frontalface_alt.xml
    26,161 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    23,550 changes: 23,550 additions & 0 deletions haarcascades/haarcascade_frontalface_alt2.xml
    23,550 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    103,493 changes: 103,493 additions & 0 deletions haarcascades/haarcascade_frontalface_alt_tree.xml
    103,493 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    35,712 changes: 35,712 additions & 0 deletions haarcascades/haarcascade_frontalface_default.xml
    35,712 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    18,118 changes: 18,118 additions & 0 deletions haarcascades/haarcascade_fullbody.xml
    18,118 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    9,803 changes: 9,803 additions & 0 deletions haarcascades/haarcascade_lefteye_2splits.xml
    9,803 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    15,085 changes: 15,085 additions & 0 deletions haarcascades/haarcascade_lowerbody.xml
    15,085 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    10,930 changes: 10,930 additions & 0 deletions haarcascades/haarcascade_mcs_eyepair_big.xml
    10,930 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    12,586 changes: 12,586 additions & 0 deletions haarcascades/haarcascade_mcs_eyepair_small.xml
    12,586 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    9,322 changes: 9,322 additions & 0 deletions haarcascades/haarcascade_mcs_leftear.xml
    9,322 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    23,791 changes: 23,791 additions & 0 deletions haarcascades/haarcascade_mcs_lefteye.xml
    23,791 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    21,991 changes: 21,991 additions & 0 deletions haarcascades/haarcascade_mcs_mouth.xml
    21,991 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    48,433 changes: 48,433 additions & 0 deletions haarcascades/haarcascade_mcs_nose.xml
    48,433 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    9,671 changes: 9,671 additions & 0 deletions haarcascades/haarcascade_mcs_rightear.xml
    9,671 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    42,252 changes: 42,252 additions & 0 deletions haarcascades/haarcascade_mcs_righteye.xml
    42,252 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    46,327 changes: 46,327 additions & 0 deletions haarcascades/haarcascade_mcs_upperbody.xml
    46,327 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    31,930 changes: 31,930 additions & 0 deletions haarcascades/haarcascade_profileface.xml
    31,930 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    9,833 changes: 9,833 additions & 0 deletions haarcascades/haarcascade_righteye_2splits.xml
    9,833 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    29,767 changes: 29,767 additions & 0 deletions haarcascades/haarcascade_upperbody.xml
    29,767 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    Binary file added pic.detect.jpg
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
    Binary file added pic.jpg
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  2. @npinto npinto created this gist Sep 5, 2012.
    16 changes: 16 additions & 0 deletions cv2_detect.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    import cv2
    import cv2.cv as cv


    def detect(img, cascade_fn='haarcascades/haarcascade_frontalface_alt.xml',
    scaleFactor=1.3, minNeighbors=4, minSize=(20, 20),
    flags=cv.CV_HAAR_SCALE_IMAGE):

    cascade = cv2.CascadeClassifier(cascade_fn)
    rects = cascade.detectMultiScale(img, scaleFactor=scaleFactor,
    minNeighbors=minNeighbors,
    minSize=minSize, flags=flags)
    if len(rects) == 0:
    return []
    rects[:, 2:] += rects[:, :2]
    return rects