def detect(filename=None, folder=None, num_of_people=people, num_of_pics=pic_per_person): imgs = getImages(filename, folder, num_of_people, num_of_pics) # print(imgs) totalPhotos=len(imgs) photosWithNoFaces = [] photosWithMoreThanOneFace = [] photosWithMoreThan2Eyes = [] index=1 for photo in imgs: if index % 50 == 0: print(index) index +=1 img = cv2.imread(photo.path) if resize: res = uti.resize_img(img, 500) else: res = img gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, scale_factor, num_neighbors) ''' faces = face_cascade.detectMultiScale(gray, 1.9, 3) Total Photos 13233 Photos with no faces: 3333 Photos with more than one face : 341 ----- 54.146432399749756 seconds ---- ************************************************************* faces = face_cascade.detectMultiScale(gray, 1.5, 3) Total Photos 13233 Photos with no faces: 918 Photos with more than one face : 5658 ----- 91.67998313903809 seconds ---- ************************************************************* faces = face_cascade.detectMultiScale(gray, 1.3, 3) Total Photos 13233 Photos with no faces: 418 Photos with more than one face : 356 ----- 128.8101842403412 seconds ---- ************************************************************* faces = face_cascade.detectMultiScale(gray, 1.1, 3) Total Photos 13233 Photos with no faces: 68 Photos with more than one face : 841 ----- 257.9152545928955 seconds ---- ************************************************************* faces = face_cascade.detectMultiScale(gray, 1.1, 5) Total Photos 13233 Photos with no faces: 124 Photos with more than one face : 558 ----- 257.44470167160034 seconds ---- ''' # print(faces) if(len(faces) ==0): photosWithNoFaces += [photo] elif(len(faces)>1): photosWithMoreThanOneFace += [photo] # print(photo.photo_name) if print_photos: for(x,y,w,h) in faces: #Paint face cv2.rectangle(res, (x,y), (x+w, y+h), (255,0,0),2) #Get all facial data roi_gray = gray[y:y+h, x:x+w] roi_color = res[y:y+h, x:x+w] left_eyes = left_eye_cascade.detectMultiScale(roi_gray) right_eyes = right_eye_cascade.detectMultiScale(roi_gray) eyes = eye_cascade.detectMultiScale(roi_gray) smiles = smile_cascade.detectMultiScale(roi_gray) bodies = fullbody_cascade.detectMultiScale(roi_gray) #Paint both eyes for(ex, ey, ew, eh) in eyes: cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0,255,255),2) #Paint left eyes # for(ex, ey, ew, eh) in left_eyes: # cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0,255,0),2) #Paint right eyes # for(ex, ey, ew, eh) in right_eyes: # cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0,0,255),2) #Paint Mouths # for(ex, ey, ew, eh) in smiles: # cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (255,127,200),2) print("Eyes : {0}".format(len(eyes))) print("Left eyes : {0}".format(len(left_eyes))) print("Right eyes : {0}".format(len(right_eyes))) # print("finished") cv2.imshow(photo.photo_name, res) cv2.waitKey(0) cv2.destroyAllWindows() # print(type(["hola"]), type("hola")) if write_log: uti.write_file("photos_with_no_faces.txt", photosWithNoFaces) print("Total Photos " + str(totalPhotos)) print("Photos with no faces: {0}".format((len(photosWithNoFaces)))) print("Photos with more than one face : {0}".format(len(photosWithMoreThanOneFace))) total_time = time.time() - start_time print("----- %s seconds ----" %total_time)
def detect(filename=None, folder=None, num_of_people=people, num_of_pics=pic_per_person): # path = FACE_LIBRARY + 'IMG_20160619_152455(dif).jpg' path = FACE_LIBRARY + 'beard.jpg' img = cv2.imread(path) face_array = [] # print(imgs) index=1 if resize: res = uti.resize_img(img, 600) else: res = img gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY) print(len(gray), len(gray[0])) equalizedImg = cv2.equalizeHist(gray) print('img shape {}'.format(img.shape)) print('gray shape {}'.format(gray.shape)) print('equa shape {}'.format(equalizedImg.shape)) #Test out = det.detectObjectsCustom(img, face_cascade, 600, None, (20, 20), scale_factor, num_neighbors, True, 'Face') #End Test faces = face_cascade.detectMultiScale(equalizedImg, scale_factor, num_neighbors, cv2.CASCADE_FIND_BIGGEST_OBJECT) print('faces\n') print(faces) if print_photos: for i in range(len(out)): print(i) #Paint face (x,y,w,h) = (out[i][0], out[i][1], out[i][2], out[i][3]) # face = (x,y,w,h) cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0),2) #Get all facial data roi_gray = gray[y:y+h, x:x+w] roi_color = res[y:y+h, x:x+w] face_array += [roi_color] left_eyes = left_eye_cascade.detectMultiScale(roi_gray) right_eyes = right_eye_cascade.detectMultiScale(roi_gray) eyes = eye_cascade.detectMultiScale(roi_gray) # smiles = smile_cascade.detectMultiScale(roi_gray) # bodies = fullbody_cascade.detectMultiScale(roi_gray) #Paint both eyes if draw_eyes == 1 : for(ex, ey, ew, eh) in eyes: cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (255,255,255),2) #Paint left eyes elif draw_eyes == 1: for(ex, ey, ew, eh) in left_eyes: cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (255,255,255),2) #Paint right eyes elif draw_eyes == 1: for(ex, ey, ew, eh) in right_eyes: cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (255,255,255),2) print("Eyes : {0}".format(len(eyes))) print("Left eyes : {0}".format(len(left_eyes))) print("Right eyes : {0}".format(len(right_eyes))) # print("finished") total_time = time.time() - start_time print("----- %s seconds ----" %total_time) cv2.imshow('Photo', res) print('face fotos ' + str(len(face_array))) # for n, photo in enumerate(face_array): # print() # cv2.imshow('photo' + str(n), photo) cv2.waitKey(0) cv2.destroyAllWindows()