-1

Firstly apologies for the poorly worded title but didn't know how else to word it. I am new to python and opencv and am trying to make sense of some basic face detection code. There is one part of code that I am struggling to understand (perhaps lack of experience with python). The code is as follows:

eyes = eye_cascade.detectMultiScale(roi_gray,scaleFactor=1.2, minNeighbors=5, minSize=(10, 10))

for (ex, ey, ew, eh) in eyes:
    cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0,255,0), 2)

The part I'm struggling to understand is how the for loop knows that ex, ey, ew and eh are the 4 corners of the rectangle? It feels to me like you should at least be saying:

for(ex,ey,ew,eh) in eyes.coordinates

or something similar so it at least knows what to loop through. Sorry for my ignorance, any help is appreciated.

Christian T
  • 128
  • 1
  • 12

1 Answers1

2

It all boils down to the question: What exactly is a rectangle?

From https://docs.opencv.org/trunk/d7/d8b/tutorial_py_face_detection.html and Create instances of class Rect from opencv 3 in Python, we get to know that

rectangles are tuples/lists of the four coordinates

and in python you can iterate a list of tuples as done in the loop

eg.

A = [('1', 1), ('2', 2)]
for (a, b) in A:
    print(a, type(a), b, type(b))

>> 1 <class 'str'> 1 <class 'int'>
>> 2 <class 'str'> 2 <class 'int'>
Ishan Srivastava
  • 1,129
  • 1
  • 11
  • 29
  • perfect answer thankyou! so i guess the order in which the coordinates is listed in a rectangle is important to know, which seemed to be difficult for me to find but i guess i was searching the wrong thing... – Christian T Jul 16 '18 at 09:33
  • yes, you would like to refer the docs (for a general case it will depend on whether its a list of a data structure which is ordered or not) but there is no doubt in your specific case that the order is fixed – Ishan Srivastava Jul 16 '18 at 09:37