Keras负载模型导致mtcnn人脸检测停止工作

问题描述

在加载用于遮罩检测的keras模型时,使用mtcnn的面部检测模型会停止工作,并且一旦load_model行被注释,它就会开始工作吗?我已经检查了功能本身,那里没有问题。这是代码:

  CUDA_VISIBLE_DEVICES = 0
    parser = argparse.ArgumentParser()
    parser.add_argument('--check',help='if its video or folder.',type=str,default=0)
    args = parser.parse_args()
    print(sys.argv)
    log = pd.DataFrame(columns=["name","date and time"])
    cuda = clear_cuda_memory()
    if args.check == 'video':

        #
        MINSIZE = 20
        THRESHOLD = [0.6,0.7,0.7]
        FACTOR = 0.709
        IMAGE_SIZE = 182
        INPUT_IMAGE_SIZE = 160
        CLASSIFIER_PATH = '/home/ubuntu/FacRecog/attendance_system_face_recognition/facenet-master/face-recognition-master/dataset/Friends/innovo_528_GPU'
        mode_for_names={}
        VIDEO_PATH = 'C:\\Users\\Lenovo\\Downloads\\Data_science\\it.mkv'
        FACENET_MODEL_PATH = '/home/ubuntu/FacRecog/attendance_system_face_recognition/facenet-master/face-recognition-master/dataset/Friends/Models/new/20180408-102900.pb'

        unknown_count = 0
        person_count = 0
        workbook = xlsxwriter.Workbook('log.xlsx')
        worksheet = workbook.add_worksheet()
        worksheet.write('A1','name')
        worksheet.write('B1','date and time')

        # Load The Custom Classifier
        with open(CLASSIFIER_PATH,'rb') as file:
            model,class_names = pickle.load(file)
        print("Custom Classifier,Successfully loaded")

        with tf.Graph().as_default():
            config = tf.ConfigProto()
            config.gpu_options.allow_growth = True
            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.2)
            sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,log_device_placement=False))

            with sess.as_default():



                # Load the model
                print('Loading feature extraction model')
                # facenet.load_model(FACENET_MODEL_PATH)
                facenet.load_model(FACENET_MODEL_PATH)# load model for face recognition
                # Get input and output tensors
                images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
                embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
                phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
                embedding_size = embeddings.get_shape()[1]

                pnet,rnet,onet = align.detect_face.create_mtcnn(sess,"./src/align")# used to find the onet pent rnet for mtcnn

                people_detected = set()
                person_detected = collections.Counter()
                phone = 'https://192.168.18.64:8080/video'
                # ip_cam='admin:123@192.168.18.157/media/?action=stream'
                computer = 0
                cap = cv2.VideoCapture(phone)
                # fvs = FileVideoStream(phone).start()
                excel = 1
                check_for_mode=0
                check_for_unknown=0
                max_one = -9999
                # face_clsfr = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
                mask_check = 0

                size = 2
                webcam = cv2.VideoCapture('https://192.168.18.64:8080/video')  # Use camera 0
                label = 5
                mask_count = 0
                # We load the xml file
                classifier = cv2.CascadeClassifier('/home/ubuntu/opencv-3.3.0/data/haarcascades/haarcascade_frontalface_default.xml')
                model_mask = load_model("model8-008.model")#the main issue exits in this line as as soon as it is commented the code works

                labels_dict = {0: 'without mask',1: 'mask'}
                color_dict = {0: (0,255),1: (0,255,0)}

                while True:
                    try:
                        (rval,im) = webcam.read()
                        bounding_boxes,_ = align.detect_face.detect_face(im,MINSIZE,pnet,onet,THRESHOLD,FACTOR)# used for mtcnn face detection
                        faces_found = bounding_boxes.shape[0]
                        # gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
                        # faces = face_clsfr.detectMultiScale(frame,1.3,5)
                        max_check = []

                        try:
                            if faces_found > 0:

                                det = bounding_boxes[:,0:4]
                                bb = np.zeros((faces_found,4),dtype=np.int32)

                                face = 0
                                cv2.waitKey(1)
                                for i in range(faces_found):
                                    bb[i][0] = det[i][0]
                                    bb[i][1] = det[i][1]
                                    bb[i][2] = det[i][2]
                                    bb[i][3] = det[i][3]

                                    x = np.sqrt((bb[i][2] - bb[i][0]) ** 2 + (bb[i][3] - bb[i][1]) ** 2)
                                    max_check.append(x)
                                value_max = np.max(max_check)
                                index_max = max_check.index(value_max)
                                face = index_max
                                cropped = im[bb[face][1]:bb[face][3],bb[face][0]:bb[face][2],:]

                                resized=cv2.resize(cropped,(150,150))
                                normalized=resized/255.0
                                reshaped=np.reshape(normalized,(1,150,3))
                                reshaped = np.vstack([reshaped])
                                resized1=cv2.resize(cropped,150))
                                normalized1=resized1/255.0
                                reshaped1=np.reshape(normalized1,3))
                                reshaped1 = np.vstack([reshaped1])
                                result=model_mask.predict(reshaped1)
                                print(result)

                                label=np.argmax(result,axis=1)[0]
                                #
                                # cv2.rectangle(im,(x,y),(x+w,y+h),color_dict[label],2)
                                # cv2.rectangle(im,y-40),-1)
                                # cv2.putText(im,labels_dict[label],y-10),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,2)

                        except Exception as e:
                            print(e)
                    except Exception as e:
                        print(e)

如果代码的显示或结构存在任何问题,请告知。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...