OpenCV-Python version 3.4.1
I am trying to detect multiple objects through a camera. The objects are Face, eyes, spoon, pen. Spoon and Pen are particular i.e. it should only detect the Pen and Spoon that I have trained it with. But it detects all the kind of faces and eyes as I have used the '.xml' file for face and eye detection that comes with OpenCV-Python.
My Question is about the code. There is a line in my code below which says detectMultiScale(gray, 1.3, 10). Now, I used the documentation and still couldn't clearly understand the last two parameters of the bracket.
My code:
# with camera feed
import cv2
import numpy as np
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
spoon_cascade = cv2.CascadeClassifier('SpoonCascade.xml')
pen_cascade = cv2.CascadeClassifier('PenCascade.xml')
cap = cv2.VideoCapture('link')
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
spoons = spoon_cascade.detectMultiScale(gray, 1.3, 10)
pens = pen_cascade.detectMultiScale(gray, 1.3, 10)
for (x, y, w, h) in spoons:
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'Spoon', (x-w, y-h), font, 0.5, (0, 255, 255), 2,
cv2.LINE_AA)
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
for (x, y, w, h) in pens:
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'Pen', (x-w, y-h), font, 0.5, (0, 255, 255), 2,
cv2.LINE_AA)
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
for (x, y, w, h) in faces:
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'Face', (x + w, y + h), font, 0.5, (0, 255, 255), 2,
cv2.LINE_AA)
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_color = img[y:y + h, x:x + w]
roi_gray = gray[y:y + h, x:x + w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 0,
255), 2)
cv2.imshow('Voila', img)
cv2.imwrite('KuchhToDetected.jpg', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
My question:
Is it just a matter of trial and error adjusting these last two parameters or can one know how to change them according to the images?
According to me these two are really significant and make the code very sensitive as it affects false positives. How do I set them properly to reduce false positives ?
It is really important parameter in case of object detection, so it would be beneficial for all if we have the answer once and for all.
Thank you.