├── README.md
├── data_training.py
├── inference.py
└── data_collection.py
/README.md:
--------------------------------------------------------------------------------
1 | # liveEmoji
2 |
3 |
Connect with me
4 | If you have any queries regarding any of the topic I discussed in this video feel free to talk to e using below links:
5 | facebook : https://m.facebook.com/proogramminghub
6 | instagram : @programming_hut
7 | twitter : https://twitter.com/programming_hut
8 | github : https://github.com/Pawandeep-prog
9 | discord : https://discord.gg/G5Cunyg
10 | linkedin : https://www.linkedin.com/in/programminghut
11 | youtube : https://www.youtube.com/c/programminghutofficial
12 |
--------------------------------------------------------------------------------
/data_training.py:
--------------------------------------------------------------------------------
1 | import os
2 | import numpy as np
3 | import cv2
4 | from tensorflow.keras.utils import to_categorical
5 |
6 | from keras.layers import Input, Dense
7 | from keras.models import Model
8 |
9 | is_init = False
10 | size = -1
11 |
12 | label = []
13 | dictionary = {}
14 | c = 0
15 |
16 | for i in os.listdir():
17 | if i.split(".")[-1] == "npy" and not(i.split(".")[0] == "labels"):
18 | if not(is_init):
19 | is_init = True
20 | X = np.load(i)
21 | size = X.shape[0]
22 | y = np.array([i.split('.')[0]]*size).reshape(-1,1)
23 | else:
24 | X = np.concatenate((X, np.load(i)))
25 | y = np.concatenate((y, np.array([i.split('.')[0]]*size).reshape(-1,1)))
26 |
27 | label.append(i.split('.')[0])
28 | dictionary[i.split('.')[0]] = c
29 | c = c+1
30 |
31 |
32 | for i in range(y.shape[0]):
33 | y[i, 0] = dictionary[y[i, 0]]
34 | y = np.array(y, dtype="int32")
35 |
36 | ### hello = 0 nope = 1 ---> [1,0] ... [0,1]
37 |
38 | y = to_categorical(y)
39 |
40 | X_new = X.copy()
41 | y_new = y.copy()
42 | counter = 0
43 |
44 | cnt = np.arange(X.shape[0])
45 | np.random.shuffle(cnt)
46 |
47 | for i in cnt:
48 | X_new[counter] = X[i]
49 | y_new[counter] = y[i]
50 | counter = counter + 1
51 |
52 |
53 | ip = Input(shape=(X.shape[1]))
54 |
55 | m = Dense(512, activation="relu")(ip)
56 | m = Dense(256, activation="relu")(m)
57 |
58 | op = Dense(y.shape[1], activation="softmax")(m)
59 |
60 | model = Model(inputs=ip, outputs=op)
61 |
62 | model.compile(optimizer='rmsprop', loss="categorical_crossentropy", metrics=['acc'])
63 |
64 | model.fit(X, y, epochs=50)
65 |
66 |
67 | model.save("model.h5")
68 | np.save("labels.npy", np.array(label))
69 |
--------------------------------------------------------------------------------
/inference.py:
--------------------------------------------------------------------------------
1 | import cv2
2 | import numpy as np
3 | import mediapipe as mp
4 | from keras.models import load_model
5 |
6 |
7 | model = load_model("model.h5")
8 | label = np.load("labels.npy")
9 |
10 |
11 |
12 | holistic = mp.solutions.holistic
13 | hands = mp.solutions.hands
14 | holis = holistic.Holistic()
15 | drawing = mp.solutions.drawing_utils
16 |
17 | cap = cv2.VideoCapture(0)
18 |
19 |
20 |
21 | while True:
22 | lst = []
23 |
24 | _, frm = cap.read()
25 |
26 | frm = cv2.flip(frm, 1)
27 |
28 | res = holis.process(cv2.cvtColor(frm, cv2.COLOR_BGR2RGB))
29 |
30 |
31 | if res.face_landmarks:
32 | for i in res.face_landmarks.landmark:
33 | lst.append(i.x - res.face_landmarks.landmark[1].x)
34 | lst.append(i.y - res.face_landmarks.landmark[1].y)
35 |
36 | if res.left_hand_landmarks:
37 | for i in res.left_hand_landmarks.landmark:
38 | lst.append(i.x - res.left_hand_landmarks.landmark[8].x)
39 | lst.append(i.y - res.left_hand_landmarks.landmark[8].y)
40 | else:
41 | for i in range(42):
42 | lst.append(0.0)
43 |
44 | if res.right_hand_landmarks:
45 | for i in res.right_hand_landmarks.landmark:
46 | lst.append(i.x - res.right_hand_landmarks.landmark[8].x)
47 | lst.append(i.y - res.right_hand_landmarks.landmark[8].y)
48 | else:
49 | for i in range(42):
50 | lst.append(0.0)
51 |
52 | lst = np.array(lst).reshape(1,-1)
53 |
54 | pred = label[np.argmax(model.predict(lst))]
55 |
56 | print(pred)
57 | cv2.putText(frm, pred, (50,50),cv2.FONT_ITALIC, 1, (255,0,0),2)
58 |
59 |
60 | drawing.draw_landmarks(frm, res.face_landmarks, holistic.FACEMESH_CONTOURS)
61 | drawing.draw_landmarks(frm, res.left_hand_landmarks, hands.HAND_CONNECTIONS)
62 | drawing.draw_landmarks(frm, res.right_hand_landmarks, hands.HAND_CONNECTIONS)
63 |
64 | cv2.imshow("window", frm)
65 |
66 | if cv2.waitKey(1) == 27:
67 | cv2.destroyAllWindows()
68 | cap.release()
69 | break
70 |
71 |
--------------------------------------------------------------------------------
/data_collection.py:
--------------------------------------------------------------------------------
1 | import mediapipe as mp
2 | import numpy as np
3 | import cv2
4 |
5 | cap = cv2.VideoCapture(0)
6 |
7 | name = input("Enter the name of the data : ")
8 |
9 | holistic = mp.solutions.holistic
10 | hands = mp.solutions.hands
11 | holis = holistic.Holistic()
12 | drawing = mp.solutions.drawing_utils
13 |
14 | X = []
15 | data_size = 0
16 |
17 | while True:
18 | lst = []
19 |
20 | _, frm = cap.read()
21 |
22 | frm = cv2.flip(frm, 1)
23 |
24 | res = holis.process(cv2.cvtColor(frm, cv2.COLOR_BGR2RGB))
25 |
26 |
27 | if res.face_landmarks:
28 | for i in res.face_landmarks.landmark:
29 | lst.append(i.x - res.face_landmarks.landmark[1].x)
30 | lst.append(i.y - res.face_landmarks.landmark[1].y)
31 |
32 | if res.left_hand_landmarks:
33 | for i in res.left_hand_landmarks.landmark:
34 | lst.append(i.x - res.left_hand_landmarks.landmark[8].x)
35 | lst.append(i.y - res.left_hand_landmarks.landmark[8].y)
36 | else:
37 | for i in range(42):
38 | lst.append(0.0)
39 |
40 | if res.right_hand_landmarks:
41 | for i in res.right_hand_landmarks.landmark:
42 | lst.append(i.x - res.right_hand_landmarks.landmark[8].x)
43 | lst.append(i.y - res.right_hand_landmarks.landmark[8].y)
44 | else:
45 | for i in range(42):
46 | lst.append(0.0)
47 |
48 |
49 | X.append(lst)
50 | data_size = data_size+1
51 |
52 |
53 |
54 | drawing.draw_landmarks(frm, res.face_landmarks, holistic.FACEMESH_CONTOURS)
55 | drawing.draw_landmarks(frm, res.left_hand_landmarks, hands.HAND_CONNECTIONS)
56 | drawing.draw_landmarks(frm, res.right_hand_landmarks, hands.HAND_CONNECTIONS)
57 |
58 | cv2.putText(frm, str(data_size), (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0),2)
59 |
60 | cv2.imshow("window", frm)
61 |
62 | if cv2.waitKey(1) == 27 or data_size>99:
63 | cv2.destroyAllWindows()
64 | cap.release()
65 | break
66 |
67 |
68 | np.save(f"{name}.npy", np.array(X))
69 | print(np.array(X).shape)
70 |
--------------------------------------------------------------------------------