1. You have an image named "landscape.jpg" representing a scenic view.
Write
Python code using OpenCV to accomplish the following tasks:
2. Read the image file.
3. Display the image.
4. Convert the image to grayscale.
5. Display the grayscale image.
Answer::
import cv2
from matplotlib import pyplot as plt
# Read the image
image = cv2.imread('landscape.jpg')
# Display the original image
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title('Original Image')
plt.axis('off')
plt.show()
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Display the grayscale image
plt.imshow(gray_image, cmap='gray')
plt.title('Grayscale Image')
plt.axis('off')
plt.show()
2. You're tasked with implementing a basic face recognition system using OpenCV in
Python. Write code to accomplish the following steps:
1. Load a pre-trained face detection model.
2. Capture video from the webcam.
3. Detect faces in each frame of the video stream.
4. Draw rectangles around the detected faces.
5. Display the video stream with the detected faces outlined in real-time.
Answer::
import cv2
# Load pre-trained face detection model
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Capture video from the webcam
cap = cv2.VideoCapture(0)
while True:
# Read a frame from the video stream
ret, frame = cap.read()
# Convert the frame to grayscale for face detection
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale frame
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
# Draw rectangles around the detected faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the frame with detected faces
cv2.imshow('Face Recognition', frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture object and close all windows
cap.release()
cv2.destroyAllWindows()
3. You're tasked with building a face recognition system to detect and recognize
faces in an image using OpenCV in Python. Write code to accomplish the following
steps:
1. Load pre-trained face detection and recognition models.
2. Load an image containing one or more faces.
3. Detect faces in the image.
4. Recognize each detected face and draw bounding boxes around them.
5. Display the image with the detected and recognized faces.
Answer ::.
import cv2
# Load pre-trained face detection and recognition models
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trained_model.yml') # Load the trained face recognition model
# Load an image containing faces
image = cv2.imread('faces.jpg')
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale image
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
# Recognize and draw bounding boxes around the detected faces
for (x, y, w, h) in faces:
# Recognize the face
face_id, confidence = recognizer.predict(gray[y:y+h, x:x+w])
# Draw a bounding box around the face
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Write the recognized face ID and confidence level on the image
text = f'Face ID: {face_id}, Confidence: {round(confidence, 2)}'
cv2.putText(image, text, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Display the image with detected and recognized faces
cv2.imshow('Face Recognition', image)
cv2.waitKey(0)
cv2.destroyAllWindows()