Skip to content

Instantly share code, notes, and snippets.

@ankushKun
Created April 12, 2023 15:14
Show Gist options
  • Save ankushKun/1d363d49db61668716a2f563c0a8664e to your computer and use it in GitHub Desktop.
Save ankushKun/1d363d49db61668716a2f563c0a8664e to your computer and use it in GitHub Desktop.
Simple Simp Detector 🫠
# DEPENDENCIES:
# pip3 install opencv-python deepface
# TODO:
# Add multithreading support so face detection is done in a different thread independent form video feed and display
# Add good support for multiple face detection (that is not linear)
# ヾ(=`ω´=)ノ” ..nyaa
import cv2
from deepface import DeepFace
# Filename with which currently detected face will be saved
face_file = "face.png"
# File path that will be used to detect simp (required)
target_name = "simp.png"
# Interface using which video will be captured
cap = cv2.VideoCapture(0)
# Set resolution for video feed (lower = faster)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
while True:
ret, frame = cap.read()
if not ret:
break
# Model for isolating face from the whole frame
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Convert image to BW for faster processing
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Isolate faces
faces = face_cascade.detectMultiScale(
gray, scaleFactor=1.3, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
# iterate over faces
for (x, y, w, h) in faces:
face_img = frame[y:y + h, x:x + w, :]
face_img = cv2.resize(face_img, (160, 160))
# Save face to file
cv2.imwrite(face_file, face_img)
# use deepface to match detected face with the simp.png face
preds = DeepFace.verify(face_file, target_name,
enforce_detection=False)
# If there is a match, draw a swuare and send a SIMP ALERT!
if preds["verified"]:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(frame, "SIMP DETECTED", (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
cv2.imshow("Simp Detector", frame)
if cv2.waitKey(1) & 0xFF == 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