SHARE
TWEET

Untitled

a guest Jul 19th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #importing the library
  2. import cv2
  3. import dlib
  4. from scipy.spatial import distance
  5. from imutils import face_utils
  6.  
  7. def smiledetector(coordinates):
  8.     val1 = distance.euclidean(coordinates[3],coordinates[9])
  9.     val2 = distance.euclidean(coordinates[2],coordinates[10])
  10.     val3 = distance.euclidean(coordinates[4],coordinates[8])
  11.     average = (val1+val2+val3)/3
  12.     length = distance.euclidean(coordinates[0],coordinates[6])
  13.     return average/length
  14.  
  15. cap = cv2.VideoCapture(0) #Creating the object
  16.  
  17. detect = dlib.get_frontal_face_detector() # Object Created
  18. predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
  19. while True:
  20.     ret, img = cap.read()  # Reading images
  21.  
  22.     if not ret:           #Checking that image is recieved
  23.         break
  24.  
  25.     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  26.    
  27.     boxes = detect(gray)  # Detection the location of faces
  28.     #print(boxes)
  29.     for i,d in enumerate(boxes):
  30.         shape = predictor(gray, d) #Applying predictor on each face we detect
  31.         shape = face_utils.shape_to_np(shape) # Converting the object to numpy array
  32.         mar = smiledetector(shape[48:68]) # passing coordinates
  33.         #print(mar)
  34.         if(mar > 0.30 and mar< 0.39):   #determining smiling or neutral
  35.             print("neutral")
  36.         elif(mar > 0.39):
  37.             print("smiling")
  38.  
  39.     cv2.imshow("Image", img)    # Showing the video feed
  40.     key = cv2.waitKey(1) & 0xFF  
  41.     # if the `q` key was pressed, break from the loop
  42.     if key == ord("q"):
  43.         break
  44.  
  45. cv2.destroyAllWindows()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top