OPEN CV Project Detail

 Sure, let's consider a simple OpenCV project to perform face detection in an image. For this project, you'll need Python and the OpenCV library installed.

Project: Face Detection using OpenCV

1. Install OpenCV:
   If you haven't installed OpenCV yet, you can do it using pip:

   ```bash
   pip install opencv-python
   ```

2. Import Libraries:
   Create a new Python script and import the necessary libraries.

   ```python
   import cv2
   import matplotlib.pyplot as plt
   ```

3. Load the Image:
   Load the image on which you want to perform face detection. Place the image in the same directory as your Python script.

   ```python
   image_path = 'sample_image.jpg'
   image = cv2.imread(image_path)
   ```

4. Load the Pre-trained Face Detector:
   OpenCV provides pre-trained Haar cascades for face detection. Load the Haar cascade classifier for face detection.

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

5. Perform Face Detection:
   Use the `detectMultiScale` method to detect faces in the image.

   ```python
   faces = face_cascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
   ```

6. Draw Rectangles Around Detected Faces:
   Draw rectangles around the detected faces on the original image.

   ```python
   for (x, y, w, h) in faces:
       cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
   ```

7. Display the Result:
   Show the original image with rectangles drawn around the detected faces.

   ```python
   plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
   plt.axis('off')
   plt.show()
   ```

8. Save the Result:
   Optionally, you can save the output image with the detected faces.

   ```python
   cv2.imwrite('output_image.jpg', image)
   ```

Complete Python Script:
```python
import cv2
import matplotlib.pyplot as plt

# Load the image
image_path = 'sample_image.jpg'
image = cv2.imread(image_path)

# Load the pre-trained face detector
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Perform face detection
faces = face_cascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

# Draw rectangles around detected faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)

# Display the result
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()

# Save the result (optional)
cv2.imwrite('output_image.jpg', image)
```

Replace `'sample_image.jpg'` with the path to your input image. The script will detect faces in the image, draw rectangles around them, and display the result. If you want to save the output image with the detected faces, uncomment the last line of the script.

Comments

Popular posts from this blog

Learn (Gujarati Typing) Terafont-Varun Keyboard Layout by Sama Soyab

Loops Example in Python

Tally Shortcut key and Video Tutorial Link by Sama Soyab (ACI BHUJ)