Skip to content

Commit

Permalink
refactor: fix bugs and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
NitkarshChourasia committed Jan 16, 2024
1 parent d4ebaae commit 81a131c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 44 deletions.
85 changes: 50 additions & 35 deletions Face and eye Recognition/face_recofnation_first.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
## Name - Soumyajit Chakraborty
## place - kolkata
## date - 10 / 08 / 2020

import cv2 as cv

face_cascade = cv.CascadeClassifier("..\libs\haarcascade_frontalface_default.xml")
face_cascade_eye = cv.CascadeClassifier("..\libs\haarcascade_eye.xml")
# face_glass = cv.CascadeClassifier('..\libs\haarcascade_eye_tree_eyeglasses.xml')

cap = cv.VideoCapture(0)
while cap.isOpened():

falg, img = cap.read() # start reading the camera output i mean frames
# cap.read() returning a bool value and a frame onject type value

gray = cv.cvtColor(
img, cv.COLOR_BGR2GRAY
) # converting to grayscale image to perform smoother
faces = face_cascade.detectMultiScale(
img, 1.1, 7
) # we use detectMultiscale library function to detect the predefined structures of a face
eyes = face_cascade_eye.detectMultiScale(img, 1.1, 7)
# using for loops we are trying to read each and every frame and map
for (x, y, w, h) in faces:
cv.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1)

for (a, b, c, d) in eyes:
cv.rectangle(img, (a, b), (a + c, b + d), (255, 0, 0), 1)

cv.imshow("img", img)
c = cv.waitKey(1)
if c == ord("q"):
break

cv.release()
cv.destroyAllWindows()

def detect_faces_and_eyes():
"""
Detects faces and eyes in real-time using the webcam.
Press 'q' to exit the program.
"""
# Load the pre-trained classifiers for face and eye detection
face_cascade = cv.CascadeClassifier(r"..\libs\haarcascade_frontalface_default.xml")
eye_cascade = cv.CascadeClassifier(r"..\libs\haarcascade_eye.xml")

# Open the webcam
cap = cv.VideoCapture(0)

while cap.isOpened():
# Read a frame from the webcam
flag, img = cap.read()

# Convert the frame to grayscale for better performance
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# Detect faces in the frame
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=7)

# Detect eyes in the frame
eyes = eye_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=7)

# Draw rectangles around faces and eyes
for x, y, w, h in faces:
cv.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1)

for a, b, c, d in eyes:
cv.rectangle(img, (a, b), (a + c, b + d), (255, 0, 0), 1)

# Display the resulting frame
cv.imshow("Face and Eye Detection", img)

# Check for the 'q' key to exit the program
key = cv.waitKey(1)
if key == ord("q"):
break

# Release the webcam and close all windows
cap.release()
cv.destroyAllWindows()


if __name__ == "__main__":
# Call the main function
detect_faces_and_eyes()
27 changes: 18 additions & 9 deletions Face and eye Recognition/gesture_control.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import cv2 as cv

# import numpy as np
# Read the image in grayscale
img = cv.imread(r"..\img\hand1.jpg", cv.IMREAD_GRAYSCALE)

img = cv.imread("..\img\hand1.jpg", 0)
flag, frame = cv.threshold(img, 70, 255, cv.THRESH_BINARY)
# Apply thresholding to create a binary image
_, thresholded = cv.threshold(img, 70, 255, cv.THRESH_BINARY)

contor, _ = cv.findContours(frame.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# Find contours in the binary image
contours, _ = cv.findContours(thresholded.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

hull = [cv.convexHull(c) for c in contor]
# Convex Hull for each contour
convex_hulls = [cv.convexHull(contour) for contour in contours]

final = cv.drawContours(img, hull, -1, (0, 0, 0))
cv.imshow("original_image", img)
cv.imshow("thres", frame)
cv.imshow("final_hsv", final)
# Draw contours and convex hulls on the original image
original_with_contours = cv.drawContours(img.copy(), contours, -1, (0, 0, 0), 2)
original_with_convex_hulls = cv.drawContours(img.copy(), convex_hulls, -1, (0, 0, 0), 2)

# Display the images
cv.imshow("Original Image", img)
cv.imshow("Thresholded Image", thresholded)
cv.imshow("Contours", original_with_contours)
cv.imshow("Convex Hulls", original_with_convex_hulls)

# Wait for a key press and close windows
cv.waitKey(0)
cv.destroyAllWindows()

0 comments on commit 81a131c

Please sign in to comment.