Skip to content

Commit

Permalink
Revert "face detection algo"
Browse files Browse the repository at this point in the history
This reverts commit 9ac134d.
  • Loading branch information
Kunal22shah committed Feb 29, 2024
1 parent 9ac134d commit 2849a94
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 34 deletions.
35 changes: 4 additions & 31 deletions dataset-cleaning.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import os
import shutil
from PIL import Image
import cv2
from torchvision import transforms

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Define the simplified transform pipeline
transform_pipeline = transforms.Compose([
transforms.Resize((256, 256)),
Expand Down Expand Up @@ -54,35 +51,11 @@ def process_expression(expression_path, processed_path):
process_image(img_path, processed_path, img_name)


def detect_and_crop_face(img_cv):
gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE)
# If no faces are detected, return None
if len(faces) == 0:
return None
faces = sorted(faces, key=lambda x: x[2] * x[3], reverse=True) # Sort by area (w * h)
x, y, w, h = faces[0] # Choose the largest face
# Crop the face from the image
face_crop = img_cv[y:y + h, x:x + w]
return face_crop


def process_image(img_path, processed_path, img_name):
img_cv = cv2.imread(img_path)
# Use the detect_and_crop_face function to get the cropped face image
face_crop_cv = detect_and_crop_face(img_cv)
# If a face was detected and cropped
if face_crop_cv is not None:
# Convert the cropped face to PIL Image
face_crop_pil = Image.fromarray(cv2.cvtColor(face_crop_cv, cv2.COLOR_BGR2RGB))

img_transformed = transform_pipeline(face_crop_pil)
img_processed = transforms.ToPILImage()(img_transformed)
img_processed.save(os.path.join(processed_path, img_name))
else:
print(f"No face detected in {img_name}, skipped.")
img = Image.open(img_path).convert('RGB')
img_transformed = transform_pipeline(img)
img_processed = transforms.ToPILImage()(img_transformed)
img_processed.save(os.path.join(processed_path, img_name))


expressions = ['Happy', 'Neutral', 'Suprised', 'Focused']
Expand Down
3 changes: 0 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,3 @@ torch==2.2.0
torchvision==0.17.0
typing_extensions==4.9.0
urllib3==2.2.1

opencv-python~=4.9.0.80
tqdm~=4.66.2

0 comments on commit 2849a94

Please sign in to comment.