In this video lesson we find faces and eyes, and then track the face using the Pan Tilt camera on the AI Fusion Lab Kit. We use Haar Cascades to find the faces. Then we have a small green ‘On’ button on the frame, and a small red ‘Off’ button. We are able to do this at 15 FPS. The code we developed in the video is available below.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
import cv2 import time from picamera2 import Picamera2 from fusion_hat.servo import Servo panPin = 2 tiltPin = 3 panServo = Servo(panPin) tiltServo = Servo(tiltPin) panAngle = -10 tiltAngle =-20 panServo.angle(panAngle) tiltServo.angle(tiltAngle) piCam = Picamera2() W=1280 H=720 tStart = time.time() fps = 0 RES = (W,H) piCam.preview_configuration.main.size = RES piCam.preview_configuration.main.format = "RGB888" piCam.preview_configuration.controls.FrameRate=60 piCam.preview_configuration.align() piCam.configure("preview") piCam.start() textLowerLeft = (int(W*.01),int(H*.05)) fontFace = cv2.FONT_HERSHEY_SIMPLEX fontThickness = int(W/425) fontScale = H*.0015 fontColor = (0,0,255) faceFinder = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') eyeFinder = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml") track = False xFrameC = int((W-1)/2) yFrameC = int((H-1)/2) xBoxC = xFrameC yBoxC = yFrameC def mouseAction(event, x, y, flags, param): global frame, track if event == 0: xPos = x yPos = y if frame is not None: frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) Hue, Sat, Val = frameHSV[y,x] if Hue == 60: track = True if Hue == 0: track = False cv2.namedWindow('Camera',cv2.WINDOW_GUI_NORMAL) cv2.moveWindow('Camera',0,65) cv2.resizeWindow('Camera',W,H) cv2.setMouseCallback('Camera',mouseAction) while True: deltaT = time.time() - tStart tStart=time.time() fps = fps*.95 + (1/deltaT)*.05 frame= piCam.capture_array() frame=cv2.flip(frame,-1) cv2.circle(frame,(int(.9*W),int(.1*H)),20,(0,255,0),-1) cv2.circle(frame, (int(.95*W),int(.1*H)),20,(0,0,255),-1) gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) faces = faceFinder.detectMultiScale(gray,scaleFactor=1.1, minNeighbors =5, minSize=(100,100)) for (x,y,w,h) in faces: cv2.rectangle(frame, (x,y),(x+w,y+h),(0,255,255),3) xBoxC = x + int(w/2) yBoxC = y + int(h/2) xError = xBoxC - xFrameC yError = yBoxC - yFrameC eyes =eyeFinder.detectMultiScale(gray[y:y+h,x:x+w],scaleFactor=1.1,minNeighbors=5,minSize=(15,15)) for (i,j,w,h) in eyes: cv2.rectangle(frame,(x+i,y+j),(x+i+w,y+j+h),(255,0,0),3) if track == True and len(faces)>0: panAngle = panAngle - xError/50/2 panServo.angle(int(panAngle)) #time.sleep(.02) tiltAngle = tiltAngle + yError/50/2 tiltServo.angle(int(tiltAngle)) #time.sleep(.02) myText = "FPS: "+str(round(fps,1)) cv2.putText(frame,myText,textLowerLeft,fontFace,fontScale,fontColor,fontThickness) cv2.imshow("Camera", frame) cv2.moveWindow("Camera",0,60) if cv2.waitKey(1)==ord('q'): break cv2.destroyAllWindows() print('Program Terminated') |