@@ -116,6 +116,7 @@ def predict_gender(input_path: str):
116116 frame = image_resize (frame , width = frame_width )
117117 # predict the faces
118118 faces = get_faces (frame )
119+ print (faces )
119120 # Loop over the faces detected
120121 # for idx, face in enumerate(faces):
121122 for i , (start_x , start_y , end_x , end_y ) in enumerate (faces ):
@@ -154,9 +155,48 @@ def predict_gender(input_path: str):
154155 # Cleanup
155156 cv2 .destroyAllWindows ()
156157
158+ def predict_gender_simple (input_path : str ):
159+ """Predict the gender of the faces showing in the image"""
160+ # Read Input Image
161+ img = cv2 .imread (input_path )
162+ # resize the image, uncomment if you want to resize the image
163+ # img = cv2.resize(img, (frame_width, frame_height))
164+ # Take a copy of the initial image and resize it
165+ frame = img .copy ()
166+ if frame .shape [1 ] > frame_width :
167+ frame = image_resize (frame , width = frame_width )
168+ # predict the faces
169+ faces = get_faces (frame )
170+ print (faces )
171+ # Loop over the faces detected
172+ # for idx, face in enumerate(faces):
173+ for i , (start_x , start_y , end_x , end_y ) in enumerate (faces ):
174+ face_img = frame [start_y : end_y , start_x : end_x ]
175+ # image --> Input image to preprocess before passing it through our dnn for classification.
176+ # scale factor = After performing mean substraction we can optionally scale the image by some factor. (if 1 -> no scaling)
177+ # size = The spatial size that the CNN expects. Options are = (224*224, 227*227 or 299*299)
178+ # mean = mean substraction values to be substracted from every channel of the image.
179+ # swapRB=OpenCV assumes images in BGR whereas the mean is supplied in RGB. To resolve this we set swapRB to True.
180+ blob = cv2 .dnn .blobFromImage (image = face_img , scalefactor = 1.0 , size = (
181+ 227 , 227 ), mean = MODEL_MEAN_VALUES , swapRB = False , crop = False )
182+ # Predict Gender
183+ gender_net .setInput (blob )
184+ gender_preds = gender_net .forward ()
185+ i = gender_preds [0 ].argmax ()
186+ gender = GENDER_LIST [i ]
187+ gender_confidence_score = gender_preds [0 ][i ]
188+
189+ print (f'gender => { gender } ' )
190+ print (f'gender_confidence_score => { gender_confidence_score } ' )
157191
192+ # Display processed image
193+ #display_img("Gender Estimator", frame)
194+ # uncomment if you want to save the image
195+ #cv2.imwrite("output.jpg", frame)
196+ # Cleanup
197+ cv2 .destroyAllWindows ()
158198
159199if __name__ == '__main__' :
160200 # Parsing command line arguments entered by user
161201 import sys
162- predict_gender (sys .argv [1 ])
202+ predict_gender_simple (sys .argv [1 ])
0 commit comments