Skip to content

Instantly share code, notes, and snippets.

@csorgod
Last active September 24, 2023 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csorgod/5a7e7840d9999788ccdb0e221d6f046e to your computer and use it in GitHub Desktop.
Save csorgod/5a7e7840d9999788ccdb0e221d6f046e to your computer and use it in GitHub Desktop.
Realtime OpenCV cat detection and face persistance using a webcam
import os
import cv2
import random
cap = cv2.VideoCapture(0)
cat_cascade = cv2.CascadeClassifier('haarcascade_frontalcatface.xml')
cv2.namedWindow('img')
while True:
ret, image = cap.read()
if not ret:
break
cat_faces = cat_cascade.detectMultiScale(image)
for (x, y, w, h) in cat_faces:
cat_crop = image[y:y + h, x:x + w]
if cat_crop.size == 0:
continue
cv2.imwrite(f'data/treated/cat_name/{random.randint(000000000, 999999999)}.jpg', cat_crop)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('img', image)
k = cv2.waitKey(1)
if k == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment