Reputation: 827
I'm trying to get the minimum possible size of a face extracted from an image, I mean, I would like to minimize the size of the drawn rectangle that detect the face. For example:
The input image:
The desired output:
Source code:
import numpy as np
import cv2
face_cascade =cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
img=cv2.imread('swaa.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3,5,maxSize=(150,150))
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
cv2.imwrite("imgg1.png",roi_color)
cv2.imwrite("imggg1.png",roi_gray)
cv2.imshow('img',img)
cv2.imwrite("img1.png",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Update: using the solution posted, the results are shown below:
Upvotes: 0
Views: 773
Reputation: 11218
Just use cv2.resize function. https://www.tutorialkart.com/opencv/python/opencv-python-resize-image/
import numpy as np
import cv2
H = 64
W = 64
face_cascade =cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
img=cv2.imread('swaa.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3,5,maxSize=(150,150))
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = cv2.resize(gray[y:y+h, x:x+w], (H, W))
roi_color = cv2.resize(img[y:y+h, x:x+w], (H,W))
cv2.imwrite("imgg1.png",roi_color)
cv2.imwrite("imggg1.png",roi_gray)
cv2.imshow('img',img)
cv2.imwrite("img1.png",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Upvotes: 0