Skip to content

Instantly share code, notes, and snippets.

@silviogreuel
Created May 2, 2019 23:48
Show Gist options
  • Save silviogreuel/4e073147fd9565669d09ec8bdd81bf27 to your computer and use it in GitHub Desktop.
Save silviogreuel/4e073147fd9565669d09ec8bdd81bf27 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import cv2 as cv
upper = cv.CascadeClassifier("haarcascade_upperbody.xml")
body = cv.CascadeClassifier("haarcascade_fullbody.xml")
face = cv.CascadeClassifier("haarcascade_frontalface_default.xml")
smile = cv.CascadeClassifier("haarcascade_smile.xml")
def detect(cascade, frame):
return cascade.detectMultiScale(gray, 1.3, 5)
def drawn(frame, entities, color, thickness):
(r, g, b) = color
for (x, y, w, h) in entities:
cv.rectangle(frame, (x, y), (x+w, y+h), (r, g, b), thickness)
cam = cv.VideoCapture(0)
while True:
ret, frame = cam.read()
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
bodies = detect(body, gray)
drawn(frame, bodies, (0, 255, 0), 2)
uppers = detect(upper, gray)
drawn(frame, uppers, (255, 0, 0), 2)
faces = detect(face, gray)
drawn(frame, faces, (255, 0, 255), 2)
smiles = detect(smile, gray)
drawn(frame, smiles, (255, 0, 255), 2)
cv.imshow('frame', frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
cam.release()
cv.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment