如何在keras中加载多重重量?

时间:2018-07-13 08:33:48

标签: python tensorflow keras

我正在尝试使用keras实现人脸识别。截至目前,我有四个数据集:person1,person2,person3和unknow [n]。

这四个数据集已经过预训练,所以我有四个权重:

  • person1.json, person1.h5
  • person2.json, person2.h5
  • person3.json, person3.h5
  • unknow.json, unknow.h5

我想问一下如何加载这四个权重,以及如何预测它们

here是我在互联网上找到的源代码。

        # loop over the frames from the video stream
    while True:
        key = cv2.waitKey(1) & 0xFF
         # if the `q` key was pressed, break from the loop
        if key == ord("q"):
            break

        frame = vs.read()
        frame = imutils.resize(frame, width=800)
        height, width = frame.shape[:2]
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # detect faces in the grayayscale frame
        rects = detector(gray_frame, 0)

        # loopop over the face detections
        for rect in rects:
            faceAligned = fa.align(frame, gray_frame, rect)
            faceAligned = cv2.cvtColor(faceAligned, cv2.COLOR_BGR2GRAY)
            faceAligned = np.array(faceAligned)
            faceAligned = faceAligned.astype('float32')
            faceAligned /= 255.0
            faceAligned= np.expand_dims([faceAligned], axis=4)

            Y_pred =  loaded_model.predict(faceAligned)
            print(type(Y_pred))
            print(Y_pred[0])

            for index, value in enumerate(Y_pred[0]):

                result = people[index] + ': ' + str(int(value * 100)) + '%'

                if ( int(value * 100) > 95 ) :
                    # draw rect around face
                    (x,y,w,h) = face_utils.rect_to_bb(rect)
                    cv2.rectangle(frame, (x, y), (x+w, y+h), (0,0,255), 1)
                    # draw person name
                    result = np.argmax(Y_pred, axis=1)
                    cv2.putText(frame, people[result[0]], (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 1)

        # show the frame
        cv2.imshow("Frame", frame)

    # do a bit of cleanup
    cv2.destroyAllWindows()
    vs.stop()      
cv2.putText(frame, result, (10, 15 * (index + 1)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1)

0 个答案:

没有答案