ZinedinZidan

Automatic Selfie

Jul 27th, 2021
1,005
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import cv2
  2. import datetime
  3. cap = cv2.VideoCapture(0)
  4. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  5. smile_cascade = cv2.CascadeClassifier('haarcascade_smile.xml')
  6. while True:
  7.     _, frame = cap.read()
  8.     original_frame = frame.copy()
  9.     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  10.     face = face_cascade.detectMultiScale(frame, 1.3, 5)
  11.     for x, y, w, h in face:
  12.         cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 255), 2)
  13.         face_roi = frame[y:y+h, x:x+w]
  14.         gray_roi = gray[y:y+h, x:x+w]
  15.         smile = smile_cascade.detectMultiScale(gray_roi, 1.3, 25)
  16.         for x1, y1, w1, h1 in smile:
  17.             cv2.rectangle(face_roi, (x1, y1), (x1+w1, y1+h1), (0, 3, 255), 2)
  18.             time_stamp = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
  19.             file_name = f'automatic_selfie{time_stamp}.png'
  20.             cv2.imwrite(file_name, original_frame)
  21.     cv2.imshow('web camera', frame)
  22.     if cv2.waitKey(10) == ord('q'):
  23.         break
RAW Paste Data