Question
Need to resolve the error (mentioned at the end) facing on running the below Python code based on OpenCV: import cv2 import numpy as np
Need to resolve the error (mentioned at the end) facing on running the below Python code based on OpenCV:
import cv2 import numpy as np
# Load the images img1 = cv2.imread('subject1.jpg') img2 = cv2.imread('subject2.jpg')
# Convert images to grayscale gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# Adjust for illumination clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) gray1 = clahe.apply(gray1) gray2 = clahe.apply(gray2)
# Detect facial features face1_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') face2_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') face1 = face1_cascade.detectMultiScale(gray1, 1.3, 5) face2 = face2_cascade.detectMultiScale(gray2, 1.3, 5)
# Find the eye centers for (x,y,w,h) in face1: roi_gray1 = gray1[y:y+h, x:x+w] eyes1 = cv2.CascadeClassifier('haarcascade_eye.xml') eyes1 = eyes1.detectMultiScale(roi_gray1) if len(eyes1) == 2: ex1,ey1,ew1,eh1 = eyes1[0] ex2,ey2,ew2,eh2 = eyes1[1] eye_center1 = (int(ex1+(ew1/2)), int(ey1+(eh1/2))) eye_center2 = (int(ex2+(ew2/2)), int(ey2+(eh2/2)))
for (x,y,w,h) in face2: roi_gray2 = gray2[y:y+h, x:x+w] eyes2 = cv2.CascadeClassifier('haarcascade_eye.xml') eyes2 = eyes2.detectMultiScale(roi_gray2) if len(eyes2) == 2: ex3,ey3,ew3,eh3 = eyes2[0] ex4,ey4,ew4,eh4 = eyes2[1] eye_center3 = (int(ex3+(ew3/2)), int(ey3+(eh3/2))) eye_center4 = (int(ex4+(ew4/2)), int(ey4+(eh4/2)))
# Calculate the angle between the eye centers eye_angle1 = np.arctan2(eye_center2[1] - eye_center1[1], eye_center2[0] - eye_center1[0]) eye_angle2 = np.arctan2(eye_center4[1] - eye_center3[1], eye_center4[0] - eye_center3[0])
# Rotate the images to align the eyes M1 = cv2.getRotationMatrix2D(eye_center1, eye_angle1*180/np.pi, 1) M2 = cv2.getRotationMatrix2D(eye_center3, eye_angle2*180/np.pi, 1) rotated_img1 = cv2.warpAffine(gray1, M1, (gray1.shape[1], gray1.shape[0])) rotated_img2 = cv2.warpAffine(gray2, M2, (gray2.shape[1], gray2.shape[0]))
# Crop the images for (x,y,w,h) in face1: roi1 = rotated_img1[y:y+h, x:x+w] for (x,y,w,h) in face2: roi2 = rotated_img2[y:y+h, x:x+w]
# Resize the images size = (100,100) resized_img1 = cv2.resize(roi1, size) resized_img2 = cv2.resize(roi2, size)
# Display the processed images cv2.imshow('Subject 1', resized_img1) cv2.imshow('Subject 2', resized_img2) cv2.waitKey(0) cv2.destroyAllWindows()
getting error:
Traceback (most recent call last): File "~/main.py", line 45, in
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started