def match_ann(fileName):
    js = json.loads(open(fileName).read())
    for items in js['people']:
        pose = items["pose_keypoints_2d"]
        handRight = items["hand_right_keypoints_2d"]
        handLeft = items["hand_left_keypoints_2d"]

    RightConfPoints = helper.confidencePoints(handRight)
    LeftConfPoints = helper.confidencePoints(handLeft)

    # add all confidence points
    RightConfidence = helper.confidence(RightConfPoints)
    LeftConfidence = helper.confidence(LeftConfPoints)

    # remove file if confidence is less than threshold
    if RightConfidence > 12:
        if LeftConfidence > 12 or LeftConfidence < 2:

            # normalizing bodyKeyPoints
            pose_points = helper.removePoints(pose)
            p1 = [pose_points[0], pose_points[1]]
            p2 = [pose_points[2], pose_points[3]]
            distance = math.sqrt(((p1[0] - p2[0])**2) + ((p1[1] - p2[1])**2))
            scaled_results, scaled_points = norm.scaleBody(
                pose_points, distance)
            poseResults, posePoints = norm.moveBody(scaled_results)

            # normalizing RightHandKeyPoints
            hand_right_points = helper.removePoints(handRight)
            p1 = [hand_right_points[0], hand_right_points[1]]
            p2 = [hand_right_points[18], hand_right_points[19]]
            distance = math.sqrt(((p1[0] - p2[0])**2) + ((p1[1] - p2[1])**2))
            RightResult, Points = scale.scalePoints(hand_right_points,
                                                    distance)
            handRightResults, handRightPoints = norm.move_to_wrist(
                RightResult, poseResults[8], poseResults[9])

            # normalizing LeftHandKeyPoints
            if LeftConfidence > 3:
                hand_left_points = helper.removePoints(handLeft)
                p1 = [hand_left_points[0], hand_left_points[1]]
                p2 = [hand_left_points[18], hand_left_points[19]]
                distance = math.sqrt(((p1[0] - p2[0])**2) +
                                     ((p1[1] - p2[1])**2))
                if distance != 0:
                    LeftResult, Points = scale.scalePoints(
                        hand_left_points, distance)
                    handLeftResults, handLeftPoints = norm.move_to_wrist(
                        LeftResult, poseResults[14], poseResults[15])
                else:
                    handLeftResults, handLeftPoints = norm.move_to_wrist(
                        hand_left_points, poseResults[14], poseResults[15])

            else:
                handLeftResults = [
                    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                    0.0, 0.0, 0.0, 0.0, 0.0, 0.0
                ]

            # combining points
            posePoints = []
            for x in range(18):
                posePoints.append(poseResults[x])
            for x in range(30, 38):
                posePoints.append(poseResults[x])

            results = handRightResults + handLeftResults + posePoints

            # extracting data from db
            connection = sqlite3.connect("data\\db\\main_dataset.db")
            crsr = connection.cursor()

            # extracting x and y points
            sql = 'SELECT Rx1,Ry1'
            for x in range(2, 22):
                sql = sql + ',Rx' + str(x) + ',Ry' + str(x)
            for x in range(1, 22):
                sql = sql + ',Lx' + str(x) + ',Ly' + str(x)
            for x in range(1, 14):
                sql = sql + ',Px' + str(x) + ',Py' + str(x)
            sql = sql + ' FROM poseDataset WHERE 1'
            crsr.execute(sql)
            feature_res = crsr.fetchall()
            feature_res = np.asarray(feature_res)
            features = []
            for x in feature_res:
                features.append(x)

            # extracting labels
            crsr.execute('SELECT label FROM poseDataset WHERE 1')
            label_res = crsr.fetchall()
            labels = []
            for x in label_res:
                labels.append(x)

            # Creating labelEncoder
            le = preprocessing.LabelEncoder()

            # Converting string labels into numbers.
            label_encoded = le.fit_transform(labels)

            label_encoded = to_categorical(label_encoded)

            X_train, X_test, y_train, y_test = train_test_split(features,
                                                                label_encoded,
                                                                test_size=0.2)

            scaler = StandardScaler().fit(X_train)
            X_train = scaler.transform(X_train)
            X_test = scaler.transform(X_test)

            y_pred = model.predict(scaler.transform(np.array([results])))

            C = np.argmax(y_pred)

            result = le.inverse_transform([C])

            return result[0]
        else:
            return "no confidence"
    else:
        return "no confidence"
def populate_words():
    """
    Delete all records from database
    """
    connection = sqlite3.connect("data\\db\\main_dataset.db")
    crsr = connection.cursor()
    sql_command = "DELETE FROM poseDataset;"
    crsr.execute(sql_command)
    connection.commit()
    connection.close()

    print("records deleted, new records will be entered now")

    folders = []
    files = []
    fileNames = []

    for entry in os.scandir('data\\datasets\\words_dataset'):
        if entry.is_dir():
            folders.append(entry.path)
            for entry1 in os.scandir(entry.path):
                if entry1.is_dir():
                    folders.append(entry1.path)
                    for entry2 in os.scandir(entry1.path):
                        if entry2.is_dir():
                            folders.append(entry2.path)
                        elif entry2.is_file():
                            if os.path.splitext(entry2)[1] == ".json":
                                files.append(entry2.path)
                                fileNames.append(entry2.name)
                elif entry1.is_file():
                    if os.path.splitext(entry1)[1] == ".json":
                        files.append(entry1.path)
                        fileNames.append(entry1.name)
        elif entry.is_file():
            if os.path.splitext(entry)[1] == ".json":
                fileNames.append(entry1.name)

    # connecting to the database
    connection = sqlite3.connect("data\\db\\main_dataset.db")
    # cursor
    crsr = connection.cursor()

    count = 0

    for file in files:
        js = json.loads(open(files[count]).read())

        parent = (os.path.dirname(files[count])).split('\\')

        parentName = "'" + parent[len(parent) - 2] + "'"

        count += 1
        for items in js['people']:
            pose = items["pose_keypoints_2d"]
            handRight = items["hand_right_keypoints_2d"]
            handLeft = items["hand_left_keypoints_2d"]

        # extract confidence points
        LeftConfPoints = helper.confidencePoints(handLeft)
        # add all confidence points
        LeftConfidence = helper.confidence(LeftConfPoints)

        pose_points = helper.removePoints(pose)
        #posePoints = helper.join_points(pose_points)
        p1 = [pose_points[0], pose_points[1]]
        p2 = [pose_points[2], pose_points[3]]
        distance = math.sqrt(((p1[0] - p2[0])**2) + ((p1[1] - p2[1])**2))
        scaled_results, scaled_points = norm.scaleBody(pose_points, distance)
        poseResults, posePoints = norm.moveBody(scaled_results)

        hand_right_points = helper.removePoints(handRight)
        p1 = [hand_right_points[0], hand_right_points[1]]
        p2 = [hand_right_points[18], hand_right_points[19]]
        distance = math.sqrt(((p1[0] - p2[0])**2) + ((p1[1] - p2[1])**2))
        RightResult, Points = scale.scalePoints(hand_right_points, distance)
        handRightResults, handRightPoints = norm.move_to_wrist(
            RightResult, poseResults[8], poseResults[9])

        if LeftConfidence > 2:
            hand_left_points = helper.removePoints(handLeft)
            p1 = [hand_left_points[0], hand_left_points[1]]
            p2 = [hand_left_points[18], hand_left_points[19]]
            distance = math.sqrt(((p1[0] - p2[0])**2) + ((p1[1] - p2[1])**2))
            LeftResult, Points = scale.scalePoints(hand_left_points, distance)
            handLeftResults, handLeftPoints = norm.move_to_wrist(
                LeftResult, poseResults[14], poseResults[15])
        else:
            handLeftResults = [
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
            ]

        # SQL command to insert the data in the table
        sql_command = "INSERT INTO poseDataset VALUES (NULL, " + str(
            handRightResults[0]) + ", " + str(handRightResults[1]) + "," + str(
                handRightResults[2]
            ) + "," + str(handRightResults[3]) + "," + str(
                handRightResults[4]
            ) + "," + str(handRightResults[5]) + "," + str(
                handRightResults[6]
            ) + "," + str(handRightResults[7]) + "," + str(
                handRightResults[8]) + "," + str(
                    handRightResults[9]) + "," + str(
                        handRightResults[10]) + "," + str(
                            handRightResults[11]) + "," + str(
                                handRightResults[12]
                            ) + "," + str(handRightResults[13]) + "," + str(
                                handRightResults[14]
                            ) + "," + str(handRightResults[15]) + "," + str(
                                handRightResults[16]
                            ) + "," + str(handRightResults[17]) + "," + str(
                                handRightResults[18]
                            ) + "," + str(handRightResults[19]) + "," + str(
                                handRightResults[20]
                            ) + "," + str(handRightResults[21]) + "," + str(
                                handRightResults[22]
                            ) + "," + str(handRightResults[23]) + "," + str(
                                handRightResults[24]
                            ) + "," + str(handRightResults[25]) + "," + str(
                                handRightResults[26]
                            ) + "," + str(handRightResults[27]) + "," + str(
                                handRightResults[28]
                            ) + "," + str(handRightResults[29]) + "," + str(
                                handRightResults[30]
                            ) + "," + str(handRightResults[31]) + "," + str(
                                handRightResults[32]
                            ) + "," + str(handRightResults[33]) + "," + str(
                                handRightResults[34]
                            ) + "," + str(handRightResults[35]) + "," + str(
                                handRightResults[36]
                            ) + "," + str(handRightResults[37]) + "," + str(
                                handRightResults[38]
                            ) + "," + str(handRightResults[39]) + "," + str(
                                handRightResults[40]
                            ) + "," + str(handRightResults[41]) + "," + str(
                                handLeftResults[0]
                            ) + ", " + str(handLeftResults[1]) + "," + str(
                                handLeftResults[2]
                            ) + "," + str(handLeftResults[3]) + "," + str(
                                handLeftResults[4]
                            ) + "," + str(handLeftResults[5]) + "," + str(
                                handLeftResults[6]
                            ) + "," + str(handLeftResults[7]) + "," + str(
                                handLeftResults[8]
                            ) + "," + str(handLeftResults[9]) + "," + str(
                                handLeftResults[10]
                            ) + "," + str(handLeftResults[11]) + "," + str(
                                handLeftResults[12]
                            ) + "," + str(handLeftResults[13]) + "," + str(
                                handLeftResults[14]
                            ) + "," + str(handLeftResults[15]) + "," + str(
                                handLeftResults[16]
                            ) + "," + str(handLeftResults[17]) + "," + str(
                                handLeftResults[18]
                            ) + "," + str(handLeftResults[19]) + "," + str(
                                handLeftResults[20]
                            ) + "," + str(handLeftResults[21]) + "," + str(
                                handLeftResults[22]
                            ) + "," + str(handLeftResults[23]) + "," + str(
                                handLeftResults[24]
                            ) + "," + str(handLeftResults[25]) + "," + str(
                                handLeftResults[26]
                            ) + "," + str(handLeftResults[27]) + "," + str(
                                handLeftResults[28]
                            ) + "," + str(handLeftResults[29]) + "," + str(
                                handLeftResults[30]
                            ) + "," + str(handLeftResults[31]) + "," + str(
                                handLeftResults[32]
                            ) + "," + str(handLeftResults[33]) + "," + str(
                                handLeftResults[34]
                            ) + "," + str(handLeftResults[35]) + "," + str(
                                handLeftResults[36]
                            ) + "," + str(handLeftResults[37]) + "," + str(
                                handLeftResults[38]
                            ) + "," + str(handLeftResults[39]) + "," + str(
                                handLeftResults[40]
                            ) + "," + str(handLeftResults[41]) + "," + str(
                                poseResults[0]
                            ) + ", " + str(poseResults[1]) + "," + str(
                                poseResults[2]
                            ) + "," + str(poseResults[3]) + "," + str(
                                poseResults[4]
                            ) + "," + str(poseResults[5]) + "," + str(
                                poseResults[6]
                            ) + "," + str(poseResults[7]) + "," + str(
                                poseResults[8]
                            ) + "," + str(poseResults[9]) + "," + str(
                                poseResults[10]
                            ) + "," + str(poseResults[11]) + "," + str(
                                poseResults[12]
                            ) + "," + str(poseResults[13]) + "," + str(
                                poseResults[14]
                            ) + "," + str(poseResults[15]) + "," + str(
                                poseResults[16]
                            ) + "," + str(poseResults[17]) + "," + str(
                                poseResults[30]
                            ) + "," + str(poseResults[31]) + "," + str(
                                poseResults[32]) + "," + str(
                                    poseResults[33]) + "," + str(
                                        poseResults[34]) + "," + str(
                                            poseResults[35]) + "," + str(
                                                poseResults[36]) + "," + str(
                                                    poseResults[37]
                                                ) + "," + parentName + ");"
        crsr.execute(sql_command)

    connection.commit()
    connection.close()
def populate_db():
    """
    Delete all records from database
    """
    connection = sqlite3.connect("data\\db\\main_dataset.db")
    crsr = connection.cursor()
    sql_command = "DELETE FROM rightHandDataset;"
    crsr.execute(sql_command)
    connection.commit()
    connection.close()

    folders = []
    files = []
    fileNames = []

    for entry in os.scandir('data\\datasets\\alphabets_dataset'):
        if entry.is_dir():
            folders.append(entry.path)
            for entry1 in os.scandir(entry.path):
                if entry1.is_dir():
                    folders.append(entry1.path)
                    for entry2 in os.scandir(entry1.path):
                        if entry2.is_dir():
                            folders.append(entry2.path)
                        elif entry2.is_file():
                            if os.path.splitext(entry2)[1] == ".json":
                                files.append(entry2.path)
                                fileNames.append(entry2.name)
                elif entry1.is_file():
                    if os.path.splitext(entry1)[1] == ".json":
                        files.append(entry1.path)
                        fileNames.append(entry1.name)
        elif entry.is_file():
            if os.path.splitext(entry)[1] == ".json":
                fileNames.append(entry1.name)

    # connecting to the database
    connection = sqlite3.connect("data\\db\\main_dataset.db")
    # cursor
    crsr = connection.cursor()

    count = 0

    for file in files:
        js = json.loads(open(files[count]).read())

        parent = (os.path.dirname(files[count])).split('\\')

        parentName = "'" + parent[len(parent) - 2] + "'"

        count += 1
        for items in js['people']:
            handRight = items["hand_right_keypoints_2d"]

        handPoints = helper.removePoints(handRight)
        p1 = [handPoints[0], handPoints[1]]
        p2 = [handPoints[18], handPoints[19]]
        distance = math.sqrt(((p1[0] - p2[0])**2) + ((p1[1] - p2[1])**2))

        Result, Points = scale.scalePoints(handPoints, distance)

        handRightResults, handRightPoints = move.centerPoints(Result)

        # SQL command to insert the data in the table
        sql_command = "INSERT INTO rightHandDataset VALUES (NULL, " + str(
            handRightResults[0]) + ", " + str(handRightResults[1]) + "," + str(
                handRightResults[2]
            ) + "," + str(handRightResults[3]) + "," + str(
                handRightResults[4]
            ) + "," + str(handRightResults[5]) + "," + str(
                handRightResults[6]
            ) + "," + str(handRightResults[7]) + "," + str(
                handRightResults[8]) + "," + str(
                    handRightResults[9]) + "," + str(
                        handRightResults[10]) + "," + str(
                            handRightResults[11]) + "," + str(
                                handRightResults[12]
                            ) + "," + str(handRightResults[13]) + "," + str(
                                handRightResults[14]
                            ) + "," + str(handRightResults[15]) + "," + str(
                                handRightResults[16]
                            ) + "," + str(handRightResults[17]) + "," + str(
                                handRightResults[18]
                            ) + "," + str(handRightResults[19]) + "," + str(
                                handRightResults[20]
                            ) + "," + str(handRightResults[21]) + "," + str(
                                handRightResults[22]
                            ) + "," + str(handRightResults[23]) + "," + str(
                                handRightResults[24]
                            ) + "," + str(handRightResults[25]) + "," + str(
                                handRightResults[26]
                            ) + "," + str(handRightResults[27]) + "," + str(
                                handRightResults[28]
                            ) + "," + str(handRightResults[29]) + "," + str(
                                handRightResults[30]
                            ) + "," + str(handRightResults[31]) + "," + str(
                                handRightResults[32]
                            ) + "," + str(handRightResults[33]) + "," + str(
                                handRightResults[34]
                            ) + "," + str(handRightResults[35]) + "," + str(
                                handRightResults[36]
                            ) + "," + str(handRightResults[37]) + "," + str(
                                handRightResults[38]) + "," + str(
                                    handRightResults[39]) + "," + str(
                                        handRightResults[40]) + "," + str(
                                            handRightResults[41]
                                        ) + "," + parentName + ");"
        crsr.execute(sql_command)

    connection.commit()
    connection.close()
def match_ann(fileName):
    js = json.loads(open(fileName).read())
    for items in js['people']:
        handRight = items["hand_right_keypoints_2d"]

    confPoints = helper.confidencePoints(handRight)
    confidence = helper.confidence(confPoints)
    if confidence > 10.2:
        handPoints = helper.removePoints(handRight)
        """
        experimenting with scaling 
        """
        p1 = [handPoints[0], handPoints[1]]
        p2 = [handPoints[18], handPoints[19]]
        distance = math.sqrt(((p1[0] - p2[0])**2) + ((p1[1] - p2[1])**2))

        Result, Points = scale.scalePoints(handPoints, distance)
        handRightResults, handRightPoints = move.centerPoints(handPoints)
        """
        extracting data from db
        """
        connection = sqlite3.connect("data\\db\\main_dataset.db")
        crsr = connection.cursor()

        # extracting x and y points
        sql = 'SELECT x1,y1'
        for x in range(2, 22):
            sql = sql + ',x' + str(x) + ',y' + str(x)
        sql = sql + ' FROM rightHandDataset WHERE 1'
        crsr.execute(sql)
        feature_res = crsr.fetchall()
        feature_res = np.asarray(feature_res)
        features = []
        for x in feature_res:
            features.append(x)

        # extracting labels
        crsr.execute('SELECT label FROM rightHandDataset WHERE 1')
        label_res = crsr.fetchall()
        labels = []
        for x in label_res:
            labels.append(x)

        #creating labelEncoder
        le = preprocessing.LabelEncoder()
        # Converting string labels into numbers.
        label_encoded = le.fit_transform(labels)

        label_encoded = to_categorical(label_encoded)

        X_train, X_test, y_train, y_test = train_test_split(features,
                                                            label_encoded,
                                                            test_size=0.2)

        scaler = StandardScaler().fit(X_train)
        X_train = scaler.transform(X_train)
        X_test = scaler.transform(X_test)

        y_pred = model.predict(scaler.transform(np.array([handRightResults])))

        C = np.argmax(y_pred)

        result = le.inverse_transform([C])

        return result[0]
    else:
        return "no confidence"