Skip to content

Instantly share code, notes, and snippets.

@tomnomnom
Last active August 27, 2015 15:58
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 tomnomnom/bbca4cd2a9558986c0f3 to your computer and use it in GitHub Desktop.
Save tomnomnom/bbca4cd2a9558986c0f3 to your computer and use it in GitHub Desktop.
Playing with OpenCV; making myself into a robot
import cv2
# See https://www.youtube.com/watch?v=FH5oDFgLSs4 for example output
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier("/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier("/usr/share/opencv/haarcascades/haarcascade_eye.xml")
fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 15, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret != True:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(frame, (x,y), (x+w,y+h), (120,120,120), -1)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color, (ex,ey), (ex+ew,ey+eh), (32,32,32), -1)
out.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(50) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment