def process(self, image): im = np.array(image) img_size = np.asarray(im.shape)[0:2] _, _, bbs, point = mtcnn.detect_face(im[:, :, ::-1], self.MIN_SIZE, self.pnet_fun, self.rnet_fun, self.onet_fun, self.THRES, self.FACTOR) face_boxes = [] if len(bbs) > 0: for jj, bbi in enumerate(bbs): det = np.squeeze(bbs[jj, 0:4]) bb = np.zeros(4, dtype=np.int32) margin = self.margin bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size[0]) #if (bb[1]) < 0 or (bb[3]) < 0 or (bb[0]) < 0 or (bb[2]) < 0: # continue # face = im[int(bb[1]):int(bb[3]), int(bb[0]):int(bb[2]), :] face_boxes.append(bb) return face_boxes
def get_faces(image, threshold=0.5, minsize=30): # img = img_to_np(image) # face detection parameters threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor temp = None temp_area = 0.0 bounding_boxes, points = detect_face(image, minsize, pnet, rnet, onet, threshold, factor) idx = 0 bboxes = [] label_texts = [] labels = [] scores = [] face_imgs = [] for i in range(len(bounding_boxes)): landmark = points[:, i].reshape((2, 5)).T bb = bounding_boxes[i] area = (float(bb[2]) - float(bb[0])) * (float(bb[3] - float(bb[1]))) img = alignment(image, bounding_boxes[i][0:4], landmark, (112, 112)) if face_crop_size != 112: img = cv2.resize(img, (face_crop_size, face_crop_size)) face_imgs.append(img) if area > temp_area: temp_area = area idx = i bboxes.append([ bounding_boxes[idx][0], bounding_boxes[idx][1], bounding_boxes[idx][2], bounding_boxes[idx][3] ]) label_texts.append("face") labels.append(1) scores.append(bounding_boxes[idx][4]) return bboxes, label_texts, labels, scores, face_imgs[idx]
def mtcnnDetect(image): if(image.shape[2]!=3 and image.shape[2]!=4): return [],[],[] if(image.shape[2]==4): image = image[:,:,:-1] img_matlab = image.copy() tmp = img_matlab[:,:,2].copy() img_matlab[:,:,2] = img_matlab[:,:,0] img_matlab[:,:,0] = tmp # boundingboxes: [None, 5] => the last dim is probability. boundingboxes, points = mtcnn.detect_face(img_matlab, minsize, PNet, RNet, ONet, threshold, False, factor) vectors = [] for i in range(boundingboxes.shape[0]): if(boundingboxes[i][1]<0): boundingboxes[i][1] = 0 if(boundingboxes[i][0]<0): boundingboxes[i][0] = 0 warped = img_matlab[int(boundingboxes[i][1]):int(boundingboxes[i][3]), int(boundingboxes[i][0]):int(boundingboxes[i][2])] vector = calcCaffeVector(warped) vectors.append(vector) return boundingboxes, points, vectors
def detect_faces(img, args): with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): pnet, rnet, onet = mtcnn.create_mtcnn(sess, './models/') minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) bounding_boxes, _ = mtcnn.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] if nrof_faces > 0: det = bounding_boxes[:, 0:4] det_arr = [] img_size = np.asarray(img.shape)[0:2] if nrof_faces > 1: if args.detect_multiple_faces: for i in range(nrof_faces): det_arr.append(np.squeeze(det[i])) else: bounding_box_size = (det[:, 2] - det[:, 0]) * (det[:, 3] - det[:, 1]) img_center = img_size / 2 offsets = np.vstack([ (det[:, 0] + det[:, 2]) / 2 - img_center[1], (det[:, 1] + det[:, 3]) / 2 - img_center[0] ]) offset_dist_squared = np.sum(np.power(offsets, 2.0), 0) index = np.argmax(bounding_box_size - offset_dist_squared * 2.0) det_arr.append(det[index, :]) else: det_arr.append(np.squeeze(det)) faces = [] boxes = [] for i, det in enumerate(det_arr): det = np.squeeze(det) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - args.margin / 2, 0) bb[1] = np.maximum(det[1] - args.margin / 2, 0) bb[2] = np.minimum(det[2] + args.margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + args.margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] scaled = cv2.resize(cropped, (args.image_size, args.image_size)) boxes.append(bb) faces.append(scaled) return faces, boxes
def main(): fps = 0 frame_num = 0 # Capture device. Usually 0 will be webcam and 1 will be usb cam. video_capture = cv2.VideoCapture(0) video_capture.set(3, 640) video_capture.set(4, 480) minsize = 25 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.50) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): pnet, rnet, onet = mtcnn.create_mtcnn(sess, None) while (True): start_time = time.time() ret, frame = video_capture.read() frame_num = frame_num + 1 if not ret: break # Display the resulting frame img = frame[:, :, 0:3] boxes, _ = mtcnn.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) print(boxes) for i in range(boxes.shape[0]): pt1 = (int(boxes[i][0]), int(boxes[i][1])) pt2 = (int(boxes[i][2]), int(boxes[i][3])) cv2.rectangle(frame, pt1, pt2, color=(0, 255, 0)) # frame_info = 'Frame: {0}, FPS: {1:.2f}'.format(frame_num, fps) end_time = time.time() fps = fps * 0.9 + 1 / (end_time - start_time) * 0.1 start_time = end_time frame_info = 'Frame: {0}, FPS: {1:.2f}'.format(frame_num, fps) cv2.putText(frame, frame_info, (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) cv2.imshow('Video', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() cv2.destroyAllWindows()
def fac_detection_alignment(img, minsize, PNet, RNet, ONet, threshold, factor): ''' return all the aligned faces with the given image ''' # face detection boundingboxes, points = mtcnn.detect_face(img, minsize, PNet, RNet, ONet, threshold, False, factor) if(boundingboxes.shape[0] == 0): alignfaces = [] else: # face alignment alignfaces = faceAlign(img, points) # original image with facial rects img = mtcnn.drawBoxes(img, boundingboxes) return alignfaces, img
def get_faces(image, threshold=0.5, minsize=20): # img = img_to_np(image) # face detection parameters threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor faces = [] bounding_boxes, points = detect_face(image, minsize, pnet, rnet, onet, threshold, factor) idx = 0 for bb in bounding_boxes: # print(bb[:4]) # img = image.crop(bb[:4]) # bb[2:4] -= bb[:2] # faces.append(Face(*bb, img)) landmark = points[:, idx].reshape((2, 5)).T bbox = bb[0:4] euler = calculate_euler(image, landmark) # print(landmark) # test_img = image[...,::-1] # for i in range(np.shape(landmark)[0]): # x = int(landmark[i][0]) # y = int(landmark[i][1]) # cv2.circle(test_img, (x, y), 2, (255, 0, 0)) # # img_size = np.asarray(image.shape)[0:2] # bb[0] = np.maximum(bb[0] - face_crop_margin / 2, 0) # bb[1] = np.maximum(bb[1] - face_crop_margin / 2, 0) # bb[2] = np.minimum(bb[2] + face_crop_margin / 2, img_size[1]) # bb[3] = np.minimum(bb[3] + face_crop_margin / 2, img_size[0]) # cropped = image[int(bb[1]):int(bb[3]), int(bb[0]):int(bb[2]), :] # img = misc.imresize(cropped, (face_crop_size, face_crop_size), interp='bilinear') img = alignment(image, bbox, landmark, (112, 112)) if face_crop_size != 112: img = misc.imresize(img, (face_crop_size, face_crop_size), interp='bilinear') faces.append(Face(bb[0], bb[1], bb[2], bb[3], bb[4], img.copy(), euler)) idx = idx + 1 # plt.imshow(test_img) # plt.show() return faces
def detect(self,im): img_matlab = im.copy() tmp = img_matlab[:,:,2].copy() img_matlab[:,:,2] = img_matlab[:,:,0] img_matlab[:,:,0] = tmp boundingboxes, tmp_points = detect_face(img_matlab, self.__minsize, self.__pnet, self.__rnet, self.__onet, self.__threshold, False, self.__factor) boundingboxes = boundingboxes.tolist() for box in boundingboxes: for i in range(4): box[i] = int(box[i]) points = [] for point in tmp_points: if len(point)!=10: continue points.append([[point[0],point[5]],[point[1],point[6]],[point[2],point[7]],[point[3],point[8]],[point[4],point[9]]]) return boundingboxes,points
def execute(self, data, batch_size): minsize = 20 threshold = [0.6, 0.7, 0.7] factor = 0.709 PNet = self._PNet RNet = self._RNet ONet = self._ONet ret = [] for i in range(batch_size): img = Image.open(data[i]) img_array = misc.fromimage(img) boundingboxes, points = detect_face(img_array, minsize, PNet, RNet, ONet, threshold, False, factor) boundingboxes = boundingboxes.tolist() ret_val = json.dumps(boundingboxes) ret.append(ret_val) return ret
def mtcnnDetect(img): minsize = 40 caffe_model_path = "./mtcnn" threshold = [0.8, 0.8, 0.6] factor = 0.709 caffe.set_mode_cpu() PNet = caffe.Net(caffe_model_path+"/det1.prototxt", caffe_model_path+"/det1.caffemodel", caffe.TEST) RNet = caffe.Net(caffe_model_path+"/det2.prototxt", caffe_model_path+"/det2.caffemodel", caffe.TEST) ONet = caffe.Net(caffe_model_path+"/det3.prototxt", caffe_model_path+"/det3.caffemodel", caffe.TEST) img_matlab = img.copy() tmp = img_matlab[:,:,2].copy() img_matlab[:,:,2] = img_matlab[:,:,0] img_matlab[:,:,0] = tmp boundingboxes, points = mtcnn.detect_face(img_matlab, minsize, PNet, RNet, ONet, threshold, False, factor) warped = img_matlab[int(boundingboxes[0][1]):int(boundingboxes[0][3]), int(boundingboxes[0][0]):int(boundingboxes[0][2])] print(int(boundingboxes[0][1]), int(boundingboxes[0][3]), int(boundingboxes[0][0]), int(boundingboxes[0][2])) warped = cv2.resize(warped, (160,160)) return warped
def detect(imgpath): with tf.Graph().as_default(): img = plt.imread(imgpath) minsize = 20 # minimum size of face threshold = [0.9, 0.8, 0.8] # three steps's threshold factor = 0.709 # scale factor key_points = [] key_boxs = [] key_confidence = [] with tf.Session() as sess: pnet, rnet, onet = mtcnn.create_mtcnn(sess, None) boxes, points = mtcnn.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) num_people = points.shape[1] for _i in range(num_people): # key point _point = [] for _j in range(5): _point.append((points[_j][_i], points[5 + _j][_i])) key_points.append(_point) # box _box = [(boxes[_i][0], boxes[_i][1]), (boxes[_i][2], boxes[_i][3]), (boxes[_i][2] - boxes[_i][0], boxes[_i][3] - boxes[_i][1])] key_boxs.append(_box) # confidence key_confidence.append(boxes[_i][4]) return key_points, key_boxs, key_confidence
def process(img): boxPtArr = [] ages = [] genders = [] boundingboxes, points = mtcnn.detect_face(img, minsize, PNet, RNet, ONet, threshold, False, factor) for i in range(boundingboxes.shape[0]): boxPtDict = {} box = [] left = int(boundingboxes[i][0]) right = int(boundingboxes[i][2]) top = int(boundingboxes[i][1]) bottom = int(boundingboxes[i][3]) box.append(left) box.append(top) box.append(right) box.append(bottom) boxPtDict["box"] = box old_size = (right - left + bottom - top) / 2.0 centerX = right - (right - left) / 2.0 centerY = bottom - (bottom - top) / 2 + old_size * 0.1 size = int(old_size * 1.3) x1 = int(centerX - size / 2) y1 = int(centerY - size / 2) x2 = int(centerX + size / 2) y2 = int(centerY + size / 2) width = x2 - x1 height = y2 - y1 rectify_x1 = x1 rectify_y1 = y1 warped = img if (x2 > img.shape[1]): warped = cv2.copyMakeBorder(img, 0, 0, 0, x2 - img.shape[1], cv2.BORDER_CONSTANT) if (x1 < 0): warped = cv2.copyMakeBorder(img, 0, 0, -x1, 0, cv2.BORDER_CONSTANT) rectify_x1 = 0 if (y2 > img.shape[0]): warped = cv2.copyMakeBorder(img, 0, y2 - img.shape[0], 0, 0, cv2.BORDER_CONSTANT) if (y1 < 0): warped = cv2.copyMakeBorder(img, -y1, 0, 0, 0, cv2.BORDER_CONSTANT) rectify_y1 = 0 warped = warped[rectify_y1:y2, rectify_x1:x2] age, gender = calcBiometry(warped) boxPtDict["age"] = str(int(age.argmax() + 0.5)) if (gender > 0.5): boxPtDict["gender"] = "Female" else: boxPtDict["gender"] = "Male" boxPtArr.append(boxPtDict) return boxPtArr
def process_find_largest_face(self, filepath): head, tail = os.path.split(filepath) basename, file_extension = os.path.splitext(tail) im = Image.open(filepath) (width, height) = im.size im = np.array(im) # cv_img = im.astype(np.uint8) img_size = np.asarray(im.shape)[0:2] try: _, _, bbs, points = mtcnn.detect_face(im, self.MIN_SIZE, self.pnet_fun, self.rnet_fun, self.onet_fun, self.THRES, self.FACTOR) except: print("detect error!") sys.exit(1) bbc = np.zeros(4, dtype=np.int32) choosedface = False if len(bbs) > 0: maxarea = 0 for jj, bbi in enumerate(bbs): det = np.squeeze(bbs[jj, 0:4]) bb = np.zeros(4, dtype=np.int32) bd = np.zeros(4, dtype=np.int32) W = det[2] - det[0] H = det[3] - det[1] margin1 = W * 0.2 margin2 = H * 0.2 bb[0] = np.maximum(det[0] - margin1 / 2, 0) bb[1] = np.maximum(det[1] - margin2 / 2, 0) bb[2] = np.minimum(det[2] + margin1 / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin2 / 2, img_size[0]) W = bb[2] - bb[0] H = bb[3] - bb[1] # if (bb[1]) < 0 or (bb[3]) > height or (bb[0]) < 0 or (bb[2]) > width: # continue if W * H > maxarea: maxarea = W * H bbc[0] = bb[0] bbc[1] = bb[1] bbc[2] = bb[2] bbc[3] = bb[3] choosedface = True # bbd[0] = bd[0] # bbd[1] = bd[1] # bbd[2] = bd[2] # bbd[3] = bd[3] # cv2.rectangle(im, (bbc[0], bbc[1]), (bbc[2], bbc[3]), (0, 0, 255), 2) # cv2.rectangle(im, (160,120), (480,360), (255, 0, 0), 2) if choosedface: cropped = im[bbc[1]:bbc[3], bbc[0]:bbc[2], :] # if not os.path.isdir(os.path.join(src_path, folder)): # os.makedirs(os.path.join(src_path, folder)) # print(basename) # cv2.imwrite(basename + '_face.png', cropped[:, :, ::-1]) return cropped[:, :, ::-1] else: print("Face is not detected or discarded!!!") return None return None
def mtcnnDetect(image): try: if(image.shape[2]!=3 and image.shape[2]!=4): return [],[] if(image.shape[2]==4): image = image[:,:,:-1] except Exception as e: return [],[] img_matlab = image.copy() tmp = img_matlab[:,:,2].copy() img_matlab[:,:,2] = img_matlab[:,:,0] img_matlab[:,:,0] = tmp # boundingboxes: [None, 5] => the last dim is probability. boundingboxes, points = mtcnn.detect_face(img_matlab, minsize, PNet, RNet, ONet, threshold, False, factor) boundingboxes = boundingboxes.astype(np.int32) warpedFaces = [] for i in range(boundingboxes.shape[0]): left = boundingboxes[i][0] right = boundingboxes[i][2] top = boundingboxes[i][1] bottom = boundingboxes[i][3] old_size = (right-left+bottom-top)/2.0 centerX = right - (right-left)/2.0 centerY = bottom - (bottom-top)/2 + old_size*0.1 size = int(old_size*1.15) x1 = int(centerX-size/2) y1 = int(centerY-size/2) x2 = int(centerX+size/2) y2 = int(centerY+size/2) width = x2 - x1 height = y2 - y1 rectify_x1 = x1 rectify_y1 = y1 warped = img_matlab if(x2>img_matlab.shape[1]): warped = cv2.copyMakeBorder(img_matlab, 0, 0, 0, x2-img_matlab.shape[1], cv2.BORDER_CONSTANT) if(x1<0): warped = cv2.copyMakeBorder(img_matlab, 0, 0, -x1, 0, cv2.BORDER_CONSTANT) rectify_x1 = 0 if(y2>img_matlab.shape[0]): warped = cv2.copyMakeBorder(img_matlab, 0, y2-img_matlab.shape[0], 0, 0, cv2.BORDER_CONSTANT) if(y1<0): warped = cv2.copyMakeBorder(img_matlab, -y1, 0, 0, 0, cv2.BORDER_CONSTANT) rectify_y1 = 0 warped = warped[rectify_y1:y2, rectify_x1:x2] warpedFaces.append(warped) if(left<0): boundingboxes[i][0] = 0 if(top<0): boundingboxes[i][1] = 0 if(right>img_matlab.shape[1]): boundingboxes[i][2] = img_matlab.shape[1] if(bottom>img_matlab.shape[0]): boundingboxes[i][3] = img_matlab.shape[0] return boundingboxes, warpedFaces
def main(args): print('Creating networks and loading parameters') with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): pnet, rnet, onet = mtcnn.create_mtcnn(sess, './models/') minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor nrof_images_total = 0 nrof_successfully_aligned = 0 for cls in os.listdir(args.unaligned): output_class_dir = os.path.join(args.aligned, cls) if not os.path.exists(output_class_dir): os.makedirs(output_class_dir) folder = os.path.join(args.unaligned, cls) for image_name in os.listdir(folder): nrof_images_total += 1 image_path = os.path.join(folder, image_name) filename = image_name.split('.')[0] output_filename = os.path.join(output_class_dir, filename + '.png') print(image_path) if not os.path.exists(output_filename): try: img = misc.imread(image_path) except (IOError, ValueError, IndexError) as e: error_message = '{}: {}'.format(image_path, e) print(error_message) else: bounding_boxes, _ = mtcnn.detect_face( img, minsize, pnet, rnet, onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] if nrof_faces > 0: det = bounding_boxes[:, 0:4] det_arr = [] img_size = np.asarray(img.shape)[0:2] if nrof_faces > 1: if args.detect_multiple_faces: for i in range(nrof_faces): det_arr.append(np.squeeze(det[i])) else: bounding_box_size = (det[:, 2] - det[:, 0]) * ( det[:, 3] - det[:, 1]) img_center = img_size / 2 offsets = np.vstack([ (det[:, 0] + det[:, 2]) / 2 - img_center[1], (det[:, 1] + det[:, 3]) / 2 - img_center[0] ]) offset_dist_squared = np.sum( np.power(offsets, 2.0), 0) index = np.argmax(bounding_box_size - offset_dist_squared * 2.0) det_arr.append(det[index, :]) else: det_arr.append(np.squeeze(det)) for i, det in enumerate(det_arr): det = np.squeeze(det) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - args.margin / 2, 0) bb[1] = np.maximum(det[1] - args.margin / 2, 0) bb[2] = np.minimum(det[2] + args.margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + args.margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] scaled = misc.imresize( cropped, (args.image_size, args.image_size), interp='bilinear') nrof_successfully_aligned += 1 filename_base, file_extension = os.path.splitext( output_filename) if args.detect_multiple_faces: output_filename_n = "{}_{}{}".format( filename_base, i, file_extension) else: output_filename_n = "{}{}".format( filename_base, file_extension) misc.imsave(output_filename_n, scaled) else: print('Unable to align "%s"' % image_path) print('Total number of images: %d' % nrof_images_total) print('Number of successfully aligned images: %d' % nrof_successfully_aligned)
def process_image(): count = 0 stamp = str(current_milli_time()) global model, classes, minsize, threshold, factor if not request.headers.get('Content-type') is None: if (request.headers.get('Content-type').split(';')[0] == 'multipart/form-data'): if 'image' in request.files.keys(): print("Form Data, Multipart upload") file = request.files['image'] # if user does not select file, browser also # submit a empty part without filename if file.filename == '': result = { "error": True, "message": "No Image Recieved", "gender": None, "skin": None } return jsonify(result) filename = secure_filename(file.filename) # file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) file.save( os.path.join(app.config['UPLOAD_FOLDER'], "photo_" + stamp + ".jpg")) else: return jsonify( get_status_code( "Invalid body", "Please provide valid format for Image 2")), 415 elif (request.headers.get('Content-type') == 'application/json'): if (request.data == b''): return jsonify( get_status_code( "Invalid body", "Please provide valid format for Image")), 415 else: print("Application/Json upload as base64") body = request.get_json() if 'image_string' in body.keys(): img_string = body['image_string'] try: # str_image = img_string.split(',')[1] imgdata = base64.b64decode(img_string) with open( os.path.join(UPLOAD_FOLDER, "photo_" + stamp + ".jpg"), 'wb') as f: f.write(imgdata) except IndexError: result = { "error": True, "message": "Invalid base64 string", "gender": None, "skin": None } return jsonify(result) else: result = { "error": True, "message": "Put 'image_string' as key in input payload", "gender": None, "skin": None } return jsonify(result) else: return jsonify( get_status_code( "Invalid header", "Please provide correct header with correct data")), 415 else: return jsonify( get_status_code("Invalid Header", "Please provide valid header")), 401 print("File to be processed : {}".format( os.path.join(UPLOAD_FOLDER, "photo_" + stamp + ".jpg"))) root_image = cv2.imread( os.path.join(UPLOAD_FOLDER, "photo_" + stamp + ".jpg")) if root_image is None: print("Could not read input image") # output = np.copy(image) image = cv2.resize(root_image, (96, 96)) image = image.astype("float") / 255.0 image = img_to_array(image) image = np.expand_dims(image, axis=0) # run inference on input image confidence = model.predict(image)[0] idx = np.argmax(confidence) label = classes[idx] # label = "{}: {:.2f}%".format(label, confidence[idx] * 100) face_count = 0 # print("Label : {}".format(label)) # print("Confidence : {:.2f}".format(confidence[idx] * 100)) sess = tf.Session() with sess.as_default(): pnet, rnet, onet = mtcnn.create_mtcnn(sess, None) # img = cv2.imread(os.path.join(os.getcwd(), 'uploads', 'sample_input1.jpg')) boxes, _ = mtcnn.detect_face(root_image, minsize, pnet, rnet, onet, threshold, factor) for i in range(boxes.shape[0]): # pt1 = (int(boxes[i][0]), int(boxes[i][1])) # pt2 = (int(boxes[i][2]), int(boxes[i][3])) x = int(boxes[i][0]) y = int(boxes[i][1]) w = int(boxes[i][2]) h = int(boxes[i][3]) p1 = int(boxes[0][2]) p2 = int(boxes[0][3]) print(float(boxes[i][4])) if (float(boxes[i][4]) >= 0.95): sub_faces = root_image[y:h, x:w] count = count + 1 cv2.imwrite( os.path.join(os.getcwd(), 'uploads', "photo_" + stamp + ".jpg"), sub_faces) else: print("No faces detected") count = 0 print("Count : ".format(count)) if (count == 0): result = { "error": True, "message": "No faces found", "gender": None, "skin": None } elif (count > 1): result = { "error": True, "message": "Too many faces detected", "gender": None, "skin": None } else: skin_tone = detect_skintone.get_skin_tone(sub_faces) hex_colors = list() for element in skin_tone: print(element['color']) obtained_hexcolor = RGB2HEX(element['color']) hex_colors.append(obtained_hexcolor) # print(hex_colors) result = { "error": False, "message": None, "gender": { "type": label, "confidence": confidence[idx] * 100 }, "skin": hex_colors } return jsonify(result)