Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #eye
- import cv2
- # Load Haar cascade for eye detection
- eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
- # Open the webcam
- cap = cv2.VideoCapture(0)
- while True:
- # Read a frame from the webcam
- ret, frame = cap.read()
- # Convert to grayscale
- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- # Detect eyes with updated parameters
- eyes = eye_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(20, 20))
- # Draw yellow rectangles around detected eyes
- for (ex, ey, ew, eh) in eyes:
- cv2.rectangle(frame, (ex, ey), (ex+ew, ey+eh), (0, 255, 255), 2) # Yellow rectangle
- # Display the frame
- cv2.imshow('Eye Detection', frame)
- # Break the loop if 'q' is pressed
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- # Release the webcam and close windows
- cap.release()
- cv2.destroyAllWindows()
- #face
- import cv2
- # Load Haar cascade for face detection
- face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
- # Open the webcam
- cap = cv2.VideoCapture(0)
- while True:
- # Read a frame from the webcam
- ret, frame = cap.read()
- # Convert to grayscale (Haar cascades work on grayscale images)
- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- # Detect faces with updated parameters
- faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(30, 30))
- # Draw rectangles around detected faces and add labels
- for (x, y, w, h) in faces:
- cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2) # Red rectangle
- cv2.putText(frame, 'Human', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
- # Display the frame
- cv2.imshow('Face Detection', frame)
- # Break the loop if 'q' is pressed
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- # Release the webcam and close windows
- cap.release()
- cv2.destroyAllWindows()
- #image
- import cv2
- # Load Haar cascade for face detection
- face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
- # Load an image
- image = cv2.imread('/Users/texnik/Downloads/Project 2/diverse.jpg')
- if image is None:
- print("Error: Could not read image file. Check the file path.")
- exit()
- # Convert to grayscale
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- # Detect faces
- faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(30, 30))
- # Count and print the number of faces
- print(f"Number of faces detected: {len(faces)}")
- # Draw ellipses around detected faces
- for (x, y, w, h) in faces:
- center = (x + w // 2, y + h // 2)
- axes = (w // 2, h // 2)
- cv2.ellipse(image, center, axes, 0, 0, 360, (0, 0, 255), 2)
- cv2.putText(image, 'Human', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
- # Display the result
- cv2.imshow('Face Detection', image)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement