OpenCV+Python face detector change rectangle size (25% bigger) - python

I want to change the rectangle size of this face detector i need it about 25% bigger:
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Read the input image
img = cv2.imread('test.jpg')
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Display the output
cv2.imshow('img', img)
cv2.waitKey()
xml: https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml
but I don't know how to do it

Related

image processing (face detection): how to detect whole face up to hair?

I want to detect the whole head from chin to the top of the hair to calculate its size.
I have a simple face detection code with python/openCV :
import cv2
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
faceCascade = cv2.CascadeClassifier(os.path.join(dir_path, 'haarcascade_frontalface_default.xml'))
image_name = input()
image = cv2.imread(os.path.join(dir_path, image_name))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
flags=cv2.CASCADE_SCALE_IMAGE
)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 255, 255), 3)
print('{} faces detected.'.format(len(faces)))
cv2.imshow('image', image)
cv2.waitKey()
cv2.imwrite(os.path.join(dir_path, image_name[:image_name.find('.') + 1] + '_resault.jpg'), image)
this detect the face from the chin to the top of the forehead like this:
the problem is I want to calculate the height of the face to the top of the hair like this:
how can I detect the whole head from the chin to the top of the hair ?

Opencv Region of Interest

I have a program that detects a face when the the web cam i recording. I've created a region of interest and i want to only detect faces only within the roi. Im trying to instruct my program to operate only in that region. Have no clue how to
cap = cv2.VideoCapture(0)
Cascade_face = cv2.CascadeClassifier('C:\\Users\moham\PycharmProjects\Face\cascade\cascade.xml')
roi = cap[40:520,340:550]
while True:
success, img = cap.read()
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = Cascade_face.detectMultiScale(imgGray, 1.3, 5)
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
cv2.imshow('face_detect', img)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyWindow('face_detect')
Try this for ROI. I do not have cascade.xml. Actually, I cannot test it.
for (x,y,w,h) in faces:
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.imshow('face_detect',img)
k = cv2.waitKey(30) & 0xff
if k == 27: # press 'ESC' to quit
break
cap.release()
cv2.destroyAllWindows()
You need to create a mask using the coordinates of the ROI.
Sample image taken from this link:
Code:
img = cv2.imread('crowd.jpg')
# create background to draw the mask
black = np.zeros((img.shape[0], img.shape[1]), np.uint8)
#ROI for this image: img[40:180, 130:300]
# create the mask using ROI coordinates
black = cv2.rectangle(black, (180, 40), (300, 130), 255, -1)
# masking the image
roi = cv2.bitwise_and(img, img, mask = black)
Now you can perform face detection on roi. You need to incorporate the above snippet in your code accordingly.
Note: To draw rectangle, cv2.rectangle() follows (x1, y1), (x2, y2) order. But to crop an image, the order is reversed: crop_img = img[y1:x1, y2:x2]

Auto zooming while face detected

hi guys I want to implement (zoom in and zoom out) like digital camera to the detected faces while real-time capturing using opencv, is there is any way I can do it without just cropping the frame then display it.
here is my code ... ,,,
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# To capture video from webcam.
cap = cv2.VideoCapture(0)
# To use a video file as input
# cap = cv2.VideoCapture('filename.mp4')
while True:
# Read the frame
_, img = cap.read()
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw the rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Display
cv2.imshow('img', img)
# Stop if escape key is pressed
k = cv2.waitKey(30) & 0xff
if k==27:
break
# Release the VideoCapture object
cap.release()

How to get confidence value in Face Detection using Haar Cascade

I use open cv and haar cascade features to detect faces from an image. And after I load the cascade.xml library it will test the face read and draw a green rectangle in the face. My question is how to get confidence value from this library, like percentage or accusation value?
import cv2 as cv
img = cv.imread('Bryan/2.PNG')
cv.imshow('Bryan', img)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)
haar_cascade = cv.CascadeClassifier('cascade.xml')
faces_rect = haar_cascade.detectMultiScale(
gray, scaleFactor=1.1, minNeighbors=6)
print(f'Number of faces of found = {len(faces_rect)}')
for (x, y, w, h) in faces_rect:
cv.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), thickness=2)
cv.imshow('Detected faces', img)
cv.waitKey(0)

How to get each value of x,y,w,h in this crop image?

i want to ask something. In opencv there is cv2.rectangle to build a rectangle of object interest. After I got the object interest which is represent with rectangle, I want to get the region of interest box. To do that I used crop = frame[y:y+h, x:x+w].
Now I want to get the value of x,y,w,h of that cropped box. How to do that?
Here is my code
while bol:
capture = cv2.VideoCapture(0)
ret, frame = capture.read()
#load object detector
path = os.getcwd()+'\haarcascade_frontalface_default.xml'
face_cascade = cv2.CascadeClassifier(path)
#convert image to grayscale
imgGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
for (x, y, w, h) in face_cascade.CascadeClassifier(imgGray):
cv2.rectangle(frame, (x,y), (x+w, y+h), (255,255,255), 3)
_croppedImage = frame[y:y+h, x:x+w]
ROI = _croppedImage
how to get value of x,y,w,h of _croppedImage ?
The value of x & y for _croppedImage is of no importance and significance. This is because the _croppedImage is simply an image cropped out of another image.
To get the w & h for _croppedImage, you can use .shape() method. w & h of an image is simply the width and height of the image respectively.
h, w = _croppedImage[:2]

Categories