Advertisement
Botlarakla

Azadov Nadirbek 15 praktika

Dec 3rd, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | Source Code | 0 0
  1. #eye
  2.  
  3. import cv2
  4.  
  5. # Load Haar cascade for eye detection
  6. eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
  7.  
  8. # Open the webcam
  9. cap = cv2.VideoCapture(0)
  10.  
  11. while True:
  12.     # Read a frame from the webcam
  13.     ret, frame = cap.read()
  14.    
  15.     # Convert to grayscale
  16.     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  17.    
  18.     # Detect eyes with updated parameters
  19.     eyes = eye_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(20, 20))
  20.    
  21.     # Draw yellow rectangles around detected eyes
  22.     for (ex, ey, ew, eh) in eyes:
  23.         cv2.rectangle(frame, (ex, ey), (ex+ew, ey+eh), (0, 255, 255), 2)  # Yellow rectangle
  24.    
  25.     # Display the frame
  26.     cv2.imshow('Eye Detection', frame)
  27.    
  28.     # Break the loop if 'q' is pressed
  29.     if cv2.waitKey(1) & 0xFF == ord('q'):
  30.         break
  31.  
  32. # Release the webcam and close windows
  33. cap.release()
  34. cv2.destroyAllWindows()
  35.  
  36.  
  37. #face
  38.  
  39.  
  40. import cv2
  41.  
  42. # Load Haar cascade for face detection
  43. face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
  44.  
  45. # Open the webcam
  46. cap = cv2.VideoCapture(0)
  47.  
  48. while True:
  49.     # Read a frame from the webcam
  50.     ret, frame = cap.read()
  51.    
  52.     # Convert to grayscale (Haar cascades work on grayscale images)
  53.     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  54.    
  55.     # Detect faces with updated parameters
  56.     faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(30, 30))
  57.    
  58.     # Draw rectangles around detected faces and add labels
  59.     for (x, y, w, h) in faces:
  60.         cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)  # Red rectangle
  61.         cv2.putText(frame, 'Human', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
  62.    
  63.     # Display the frame
  64.     cv2.imshow('Face Detection', frame)
  65.    
  66.     # Break the loop if 'q' is pressed
  67.     if cv2.waitKey(1) & 0xFF == ord('q'):
  68.         break
  69.  
  70. # Release the webcam and close windows
  71. cap.release()
  72. cv2.destroyAllWindows()
  73.  
  74.  
  75. #image
  76.  
  77. import cv2
  78.  
  79. # Load Haar cascade for face detection
  80. face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
  81.  
  82. # Load an image
  83. image = cv2.imread('/Users/texnik/Downloads/Project 2/diverse.jpg')
  84.  
  85. if image is None:
  86.     print("Error: Could not read image file. Check the file path.")
  87.     exit()
  88.  
  89. # Convert to grayscale
  90. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  91.  
  92. # Detect faces
  93. faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(30, 30))
  94.  
  95. # Count and print the number of faces
  96. print(f"Number of faces detected: {len(faces)}")
  97.  
  98. # Draw ellipses around detected faces
  99. for (x, y, w, h) in faces:
  100.     center = (x + w // 2, y + h // 2)
  101.     axes = (w // 2, h // 2)
  102.     cv2.ellipse(image, center, axes, 0, 0, 360, (0, 0, 255), 2)
  103.     cv2.putText(image, 'Human', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
  104.  
  105. # Display the result
  106. cv2.imshow('Face Detection', image)
  107. cv2.waitKey(0)
  108. cv2.destroyAllWindows()
  109.  
Tags: 15 praktika
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement