Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
172
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import cv2
  2. from fastai import *
  3. from fastai.vision import *
  4. learn = load_learner('.', 'export.pkl')
  5.  
  6. # Load the cascade
  7. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  8.  
  9. # To capture video from webcam.
  10. cap = cv2.VideoCapture(0)
  11. # To use a video file as input
  12. # cap = cv2.VideoCapture('filename.mp4')
  13. i=0
  14. while True:
  15.     # Read the frame
  16.     _, img = cap.read()
  17.     # Convert to grayscale
  18.     gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  19.     #print(gray.shape)
  20.     # Detect the faces
  21.     faces = face_cascade.detectMultiScale(gray, 1.1, 5, minSize=(48, 48))
  22.     # Draw the rectangle around each face
  23.     for (x, y, w, h) in faces:
  24.         cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
  25.     # Display
  26.     if faces is ():
  27.         cv2.putText(img, "No Face Found", (20, 60) , cv2.FONT_HERSHEY_SIMPLEX,2, (0,255,0), 3)
  28.     else:
  29.         roi = gray[y:y+h, x:x+w]
  30.         roi_resize = cv2.resize(roi, (64, 64))  
  31.         # cv2.imwrite('x'+str(i)+'.jpg',roi)
  32.         i+=1
  33.         img_fastai = Image(pil2tensor(roi_resize, dtype=np.float32).div_(255))
  34.         print(learn.predict(img_fastai))
  35.        
  36.  
  37.     cv2.imshow('img', img)
  38.     # Stop if escape key is pressed
  39.    
  40.     k = cv2.waitKey(30) & 0xff
  41.     if k==27:
  42.         break
  43. # Release the VideoCapture object
  44. cap.release()
Advertisement
RAW Paste Data Copied
Advertisement