def createUserFeature(userId: str, imageId: str, feature):
    userFeature = UserFeature(id=ig.generateId('userFeature'),
                              userId=userId,
                              imageId=imageId,
                              feature=feature,
                              createTime=tg.getNowAsMilli(),
                              updateTime=tg.getNowAsMilli())
    userFeature = userFeatureRep.save(userFeature)
    return userFeature
Beispiel #2
0
def createSubject():
    """
    post endpoint
    ---
    tags:
      - subjectController
    parameters:
      - name: body
        in: body
        required: true
        schema:
          required:
            - subjectName
          properties:
            subjectName:
              type: string
              description: The subject's name.
            subjectId:
              type: string
              description: The subject's id.
              default: ""
    responses:
      200:
        description: The response from subject_controller
        schema:
    """
    subjectName = request.json['subjectName']
    subjectId = request.json['subjectId']
    adminId = request.json['adminId']

    if userRep.findById(adminId) is None:
        return "Not this user", status.HTTP_404_NOT_FOUND
    if subjectId is "":
        subjectId = ig.generateId('subject')
    else:
        subject = subjectRep.findById(subjectId)
        if subject is not None:
            return '', 226

    subject = Subject(id=subjectId,
                      name=subjectName,
                      createTime=tg.getNowAsMilli(),
                      updateTime=tg.getNowAsMilli())
    subject = subjectRep.save(subject)
    subjectUser = SubjectUser(id=ig.generateId('subjectUser'),
                              userId=adminId,
                              subjectId=subjectId,
                              role=1,
                              createTime=tg.getNowAsMilli(),
                              updateTime=tg.getNowAsMilli())
    subjectUserRep.save(subjectUser)
    return json_util.dumps({'subject': subject.__dict__
                            }), status.HTTP_200_OK, ContentType.json
def createAndUpdateSubjectModel(subjectId: str, model):
    currentModel = subjectModelRep.findBySubjectId(subjectId)
    if currentModel is None:
        modelRep.save(model)
        currentModel = SubjectModel(id=ig.generateId('subjectModel'), subjectId=subjectId,
                                    modelId=model.id, createTime=tg.getNowAsMilli(),
                                    updateTime=tg.getNowAsMilli())
        currentModel = subjectModelRep.save(currentModel)
    else:
        previousModelId = currentModel['modelId']
        modelRep.removeById(previousModelId)
        currentModel['modelId'] = model.id
        modelRep.save(model)
        currentModel = subjectModelRep.update(currentModel)
    return currentModel
Beispiel #4
0
def postUserImage():
    """
    post endpoint
    ---
    tags:
      - userImageController
    parameters:
      - name: userId
        in: form
        required: true
        type: string
      - name: image
        in: file
        required: true
        type: file
    responses:
      200:
        description: The userImage inserted in the database
        schema:
    """
    userId = request.form['userId']
    image = request.files['image']
    imageType = image.content_type
    imageId = ig.generateId('faceImage') + '.' + imageType.split('/')[1]
    image.save(os.path.join(app.config['UPLOAD_FOLDER_IMAGE'], imageId))
    image = cv2.imread(app.config['UPLOAD_FOLDER_IMAGE'] + imageId)
    if userId is "" or image is None:
        return json_util.dumps({
            "message": "empty parameters"
        }), status.HTTP_400_BAD_REQUEST, ContentType.json
    if userRep.findById(userId) is None:
        return '', status.HTTP_404_NOT_FOUND
    if detectFace(image) is False:
        return 'This picture has multiple/no face', status.HTTP_400_BAD_REQUEST

    with open(app.config['UPLOAD_FOLDER_IMAGE'] + imageId, "rb") as imageFile:
        imageRep.save(imageId, imageFile)
    userImage = UserImage(id=ig.generateId('userImage'),
                          userId=userId,
                          imageId=imageId,
                          createTime=tg.getNowAsMilli(),
                          updateTime=tg.getNowAsMilli())
    userImage = userImageRep.save(userImage)
    _thread.start_new_thread(encodingFaceAndUploadDatabase,
                             (userId, imageId, image))
    return json_util.dumps({'userImage': userImage.__dict__
                            }), status.HTTP_200_OK, ContentType.json
def createSubjectUser():
    """
    post endpoint
    ---
    tags:
      - subjectUserController
    parameters:
      - name: body
        in: body
        required: true
        schema:
          required:
            - userId
            - subjectId
          properties:
            subjectId:
              type: string
              description: The subject's id.
            userId:
              type: string
              description: The user's id.
    responses:
      200:
        description: The response from subject_user_controller
        schema:
    """
    adminId = request.json['adminId']
    subjectId = request.json['subjectId']
    if not subjectUserRep.isAuthorizedBySubjectIdAndUserId(subjectId=subjectId,
                                                           userId=adminId):
        return "Is not Authorized!", status.HTTP_401_UNAUTHORIZED
    userId = request.json['userId']
    subjectUser = subjectUserRep.findBySubjectIdAndUserId(subjectId, userId)
    if subjectUser is not None:
        return json_util.dumps({'subjectUser': subjectUser
                                }), status.HTTP_201_CREATED, ContentType.json
    else:
        subjectUser = SubjectUser(id=ig.generateId('subjectUser'),
                                  userId=userId,
                                  subjectId=subjectId,
                                  role=2,
                                  createTime=tg.getNowAsMilli(),
                                  updateTime=tg.getNowAsMilli())
        subjectUser = subjectUserRep.save(subjectUser)
        return json_util.dumps({'subjectUser': subjectUser.__dict__
                                }), status.HTTP_200_OK, ContentType.json
Beispiel #6
0
def update(subject: Subject):
    message = mongo.db.subject.update(
        {'_id': subject['_id']},
        {'$set': {
            'name': subject['name'],
            'updateTime': tg.getNowAsMilli()
        }},
        upsert=False)
    return message
def trackingByFeature():
    """
    post endpoint
    ---
    tags:
      - trackingController
    parameters:
      - name: body
        in: body
        required: true
        schema:
          required:
            - subjectId
            - feature
          properties:
            subjectId:
              type: string
              description: The subject's id.
            feature:
              type: array
              default: []
              items:
                type: number
              description: face features
    responses:
      200:
        description: The response from tracking_controller
        schema:
    """
    feature = request.json['feature']
    subjectId = request.json['subjectId']
    deviceMacAddress = request.json['deviceMacAddress']
    # 辨識拿到userId
    userId = faceService._recognizeByFeatureAndSubjectId(subjectId=subjectId,
                                                         feature=feature)
    if userId is "":
        return 'not found this people in the subject', status.HTTP_404_NOT_FOUND
    tracking = Tracking(id=ig.generateId('tracking'),
                        userId=userId,
                        subjectId=subjectId,
                        createTime=tg.getNowAsMilli(),
                        deviceMacAddress=deviceMacAddress)
    trackingRep.save(tracking)
    return json_util.dumps({'tracking': tracking.__dict__
                            }), status.HTTP_200_OK, ContentType.json
def trackingByFaceImage():
    """
    post endpoint
    ---
    tags:
      - trackingController
    parameters:
      - name: subjectId
        in: form
        require: true
        type: text
      - name: deviceMacAddress
        in: form
        require: true
        type: text
      - name: faceImage
        in: form
        require: true
        type: file
    responses:
      200:
        description: The response from tracking_controller
        schema:
    """
    faceImage = request.files['faceImage']
    subjectId = request.form['subjectId']
    deviceMacAddress = request.form['deviceMacAddress']
    # 先抓出faceFeature...
    feature = faceService._encodingFaceFeature(faceImage)
    if feature is "":
        return 'This picture has multiple/no face', status.HTTP_404_NOT_FOUND
    # 辨識拿到userId...
    userId = faceService._recognizeByFeatureAndSubjectId(subjectId=subjectId,
                                                         feature=feature)
    if userId is "":
        return 'not found this people in the subject', status.HTTP_404_NOT_FOUND
    tracking = Tracking(id=ig.generateId('tracking'),
                        userId=userId,
                        subjectId=subjectId,
                        createTime=tg.getNowAsMilli(),
                        deviceMacAddress=deviceMacAddress)
    trackingRep.save(tracking)
    return json_util.dumps({'tracking': tracking.__dict__
                            }), status.HTTP_200_OK, ContentType.json
def updateSubjectModel():
    """
    post endpoint
    ---
    tags:
      - subjectModelController
    parameters:
      - name: body
        in: body
        required: true
        schema:
          required:
            - subjectId
          properties:
            subjectId:
              type: string
              description: The subject's id.
    responses:
      200:
        description: The response from subject_model_controller
        schema:
    """
    adminId = request.json['adminId']
    subjectId = request.json['subjectId']
    if not subjectUserRep.isAuthorizedBySubjectIdAndUserId(subjectId=subjectId, userId=adminId):
        return "Is not Authorized!", status.HTTP_401_UNAUTHORIZED
    users = subjectUserRep.findBySubjectId(subjectId)
    userIds = []
    modelUsers = []
    modelFeature = []
    for user in users:
        userIds.append(user['userId'])
    for userId in userIds:
        userFeatures = userFeatureRep.findByUserId(userId)
        for userFeature in userFeatures:
            modelUsers.append(userId)
            modelFeature.append(userFeature['feature'])
    model = Model(id=ig.generateId('model'), userIds=modelUsers, encodings=modelFeature, createTime=tg.getNowAsMilli(),
                  updateTime=tg.getNowAsMilli())
    createAndUpdateSubjectModel(subjectId=subjectId, model=model)
    FaceService._updateModelBySubjectId(subjectId)
    return subjectId, status.HTTP_200_OK
Beispiel #10
0
def update(subjectModel: SubjectModel):
    message = mongo.db.subjectModel.update({'_id': subjectModel['_id']},
                                           {'$set': {'modelId': subjectModel['modelId'],
                                                     'updateTime': tg.getNowAsMilli()}},
                                           upsert=False, multi=False)
    return message