Skip to content

Instantly share code, notes, and snippets.

@paulsc
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulsc/b91b75a3420f39d0cfa2 to your computer and use it in GitHub Desktop.
Save paulsc/b91b75a3420f39d0cfa2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import numpy as np
import cv2
side_face_fn = "haarcascades/haarcascade_profileface.xml"
cascade_profile = cv2.CascadeClassifier(side_face_fn)
gray = cv2.imread('bug-equed.jpg')
vis = gray.copy()
roi1_coords = (457, 168, 649, 360)
x1, y1, x2, y2 = roi1_coords
cv2.rectangle(vis, (x1, y1), (x2, y2), (255, 255, 255), 1)
roi = gray[y1:y2, x1:x2]
roi_flipped = cv2.flip(roi, 1)
rects = cascade_profile.detectMultiScale(roi_flipped, scaleFactor=1.3,
minNeighbors=4, minSize=(30, 30),
flags = cv2.CASCADE_SCALE_IMAGE)
print rects
roi_vis = vis[y1:y2, x1:x2]
# flip the coordinates to draw the rectangle
roi_w = x2 - x1
x, y, w, h = rects[0]
x1 = int(roi_w - x)
y1 = int(y)
x2 = int(roi_w - (x + w))
y2 = int(y + h)
cv2.rectangle(roi_vis, (x1, y1), (x2, y2), (255, 0, 0), 1)
# re-initializing the cascade fixes the bug!
# cascade_profile = cv2.CascadeClassifier(side_face_fn)
roi3_coords = (993, 199, 1173, 379)
x1, y1, x2, y2 = roi3_coords
cv2.rectangle(vis, (x1, y1), (x2, y2), (255, 255, 255), 1)
roi = gray[y1:y2, x1:x2]
rects = cascade_profile.detectMultiScale(roi, scaleFactor=1.3, minNeighbors=4,
minSize=(30,30),
flags = cv2.CASCADE_SCALE_IMAGE)
print rects
roi_vis = vis[y1:y2, x1:x2]
x1, y1, w, h = rects[0]
cv2.rectangle(roi_vis, (x1, y1), (x1+w, y1+h), (255, 0, 0), 1)
cv2.imshow('main', vis)
ch = cv2.waitKey()
if ch == 27:
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment