Skip to content

Instantly share code, notes, and snippets.

@ola0x
Created March 24, 2022 23:24
Show Gist options
  • Save ola0x/4192ca1bddde3e0b4417e7379ec64c94 to your computer and use it in GitHub Desktop.
Save ola0x/4192ca1bddde3e0b4417e7379ec64c94 to your computer and use it in GitHub Desktop.
import cv2
import sys
image = sys.argv[1]
image = cv2.imread(image)
gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
faces = faceCascade.detectMultiScale(
gray_img,
scaleFactor=1.3,
minNeighbors=3,
minSize=(30, 30)
)
print(f"[INFO] Found {len(faces)} Faces!")
for (x,y,w,h) in faces:
cv2.rectangle(image, (x,y), (x+w, y+h), (0,255,0), 2)
roi_obj = image[y:y+h, x:x+w]
print("[INFO] Objects found. Saving locally")
cv2.imwrite(str(w) + str(h) + "_faces.jpg", roi_obj)
status = cv2.imwrite("opencv_face_detected.jpg", image)
print("[INFO] Image face_detected.jpg written to the system: ", status)
# cv2.imshow("image",image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment