If you are using the Happy Faces Activity Lesson Plan, and you need the code for it, you can find it below:
You can review the code below, and pull out parts you need, or download the entire program here. (be sure to rename the extension .txt to .py if you plan to run it)
Here is the code below (be sure to keep these indents and always check your indentation when you run Python):
# Webcam based OpenCV face detector sample code import cv2 face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml") # font font = cv2.FONT_HERSHEY_SIMPLEX # webcam - if you have two webcams, you may need to change this 0 to a 1 or 2 cap = cv2.VideoCapture(0) while 1: ret, img = cap.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.2, 5) for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,255),2) con_gray = gray[y:y+h, x:x+w] con_color = img[y:y+h, x:x+w] img = cv2.putText(img,'Face', (x,y), font, 1, (255,0,0), 2, cv2.LINE_AA) # Eye detector eyes = eye_cascade.detectMultiScale(con_gray) for (ex,ey,ew,eh) in eyes: cv2.rectangle(con_color,(ex,ey),(ex+ew,ey+eh),(0,255,255),2) cv2.imshow('img',img) # Press Escape key to stop the window k = cv2.waitKey(30) & 0xff if k == 27: break # Close cap.release() # End the program cv2.destroyAllWindows() |