0

I tried this face detection program that uses haarcascades in opencv.I was able to get the required output (Finding the number of faces in the provided image), but there is a slight problem with the resultant image which draws rectangles around the faces.Instead of the original image the output image is a zoomed version of the original which doesn't show the entirety of it.

Sample

Input:enter image description here

Output: enter image description here

So this is what the output looks like after the program is run.

The code:

import cv2
import sys

# Get user supplied values
imagePath = sys.argv[1]
cascPath = "haarcascade_frontalface_default.xml"

# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)

# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.2,
    minNeighbors=5,
    minSize=(30,30)

)

print("Found {0} faces!".format(len(faces)))

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

cv2.imshow("Faces found", image)
cv2.waitKey(0)

In the prompt:

C:\Myproject>python main.py NASA.jpg
Found 20 faces!

Program gives more or less the correct answer.The scale factor can be modified to get accurate results. So my question is what can be done to get a complete image in the output?Please add any other suggestions too,I'll be thankful. Thanks for reading!

EDIT:

After a suggestion i used imwrite and saved the output image which seems very fine,but still the displayed image after running the program remains same.

Saved Image- enter image description here

Fiestyhokage
  • 25
  • 2
  • 9
  • Does it make any difference if you do: `image=cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)` rather than just `cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)`? – fmw42 Sep 18 '19 at 16:38
  • @fmw42 no it doesn't – Fiestyhokage Sep 18 '19 at 16:49
  • Very strange to me. I do not see much different between what you did and from https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_objdetect/py_face_detection/py_face_detection.html. However, I am relatively new to these tools. Sorry, I cannot help. I will look forward to seeing how this is solved. – fmw42 Sep 18 '19 at 17:23
  • Have you tried to save image to file (`cv2.imwrite('tmp.jpg', image)`)? Maybe that's just `imshow` problems. – py_ml Sep 19 '19 at 10:48
  • @py_ml save to which file?I placed all the images i test,in the root folder that is "Myproject" here – Fiestyhokage Sep 19 '19 at 11:57
  • The processed image with added bounding boxes – py_ml Sep 19 '19 at 12:00
  • @py_ml Still doesn't do anything,though the saved image in my folder seems perfect with all faces recognized.I opened it from my folder.But still the problem persists – Fiestyhokage Sep 19 '19 at 12:20
  • Could you explain what is your expected output? – py_ml Sep 19 '19 at 12:33
  • @py_ml I'm expecting the image from 'imshow' to be the same like the one that is saved,but don't know why im getting a zoomed in one. – Fiestyhokage Sep 19 '19 at 12:36
  • Your code does not show how the image window is created. – Mark Ransom Sep 19 '19 at 13:04
  • Maybe the image is too big to be displayed on your screen? Try to add cv2.namedWindow('Faces found', cv2.WINDOW_NORMAL) before cv2.imshow("Faces found", image) That line will create resizable window – Piotr Siekański Sep 19 '19 at 13:09
  • @PiotrSiekański Thank you! The default window is set to WINDOW_AUTOSIZE.I didn't knew that.Thanks for the help.You can write an answer? – Fiestyhokage Sep 19 '19 at 13:39

1 Answers1

4

Your image is too big to be displayed on your screen. Add: cv2.namedWindow('Faces found', cv2.WINDOW_NORMAL) before cv2.imshow("Faces found", image) That line will create resizeable window.

Piotr Siekański
  • 1,665
  • 8
  • 14