AttributeError:'_thread._local'对象没有属性'value'

时间:2020-04-10 20:13:45

标签: python django tensorflow keras

深度学习的新手无法弄清正在发生的事情。我正在实施django应用程序以实时识别人脸。在两个不同的环境中使用pip和conda安装了软件包,但是没有成功。在这里,我使用MTCNN来检测人脸,然后进行建模以识别人脸。当没有django时一切运行正常,但它们无法与django一起使用。 更新-发现了另一件事-该脚本甚至在单独测试(没有Django)并且使用Keras == 2.2.5进行测试时甚至没有运行。我相信MTCNN会有一些问题。 enter image description here

import cv2

进口泡菜 从PIL导入图片 从numpy导入负载 从numpy import expand_dims 从numpy导入asarray 从mtcnn.mtcnn导入MTCNN 从tensorflow.keras.models导入load_model

从sklearn.preprocessing导入LabelEncoder

RealTimeFaceDetection类:

def __init__(self):
    self.stroke = 1
    self.detector = MTCNN()
    self.video_cap = cv2.VideoCapture(0)
    self.color = (255, 0, 0)
    print("Loading pre-trained Keras model for face recognition")
    self.keras_model = load_model('facenet_keras.h5', compile=False)
    print("Face recognition model loaded successfully...")
    print("Loading pre-trained SVC model")
    self.svc_model = pickle.load(open('FACENET_MODEL.sav', 'rb'))
    print("Loading successful...")
    self.emb_data = load('5-celebrity-faces-embeddings.npz')

def img_to_array(self, face_img_pixels, required_size=(160, 160)):
    image = Image.fromarray(face_img_pixels)
    image = image.resize(required_size)
    return asarray(image)

# Get the face embedding for one face
def get_embedding(self, model, face_pixels):
    face_pixels = face_pixels.astype('float32')
    mean, std = face_pixels.mean(), face_pixels.std()
    face_pixels = (face_pixels - mean) / std
    samples = expand_dims(face_pixels, axis=0)
    yhat = model.predict(samples)
    return yhat[0]

def get_encoder(self):
    trainy = self.emb_data['arr_1']
    # Label encode targets
    out_encoder = LabelEncoder()
    out_encoder.fit(trainy)
    return out_encoder

def find_faces(self):
    out_encoder = self.get_encoder()
    while(self.video_cap.isOpened()):
        ret, frame = self.video_cap.read()
        rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        pixels = asarray(rgb_image)
        all_faces = self.detector.detect_faces(pixels)
        for face in all_faces:
            x1, y1, width, height = face['box']
            x1, y1 = abs(x1), abs(y1)
            x2, y2 = x1 + width, y1 + height
            face_arr = self.img_to_array(pixels[y1:y2, x1:x2])
            face_emb = self.get_embedding(self.keras_model, face_arr)
            samples = expand_dims(face_emb, axis=0)
            yhat_class = self.svc_model.predict(samples)
            yhat_prob = self.svc_model.predict_proba(samples)
            class_index = yhat_class[0]
            class_probability = yhat_prob[0, class_index] * 100
            class_probability = round(class_probability)
            if class_probability > 95.0:
                predict_names = out_encoder.inverse_transform(yhat_class)
                print('Predicted: %s (%f)' % (predict_names[0], class_probability))
            else:
                print("Low accuracy, probability = %f" % (class_probability))

0 个答案:

没有答案