Skip to content

Commit

Permalink
Real time predictions code from Jupyter to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jmuozan committed May 15, 2024
1 parent f646bd9 commit 7d574a1
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 118 deletions.
118 changes: 1 addition & 117 deletions ML_Sequence_Recognition.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1333,123 +1333,7 @@
" break\n",
"\n",
" cap.release()\n",
" cv2.destroyAllWindows()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"import mediapipe as mp\n",
"from tensorflow.keras.models import load_model\n",
"\n",
"mp_holistic = mp.solutions.holistic\n",
"mp_drawing = mp.solutions.drawing_utils\n",
"\n",
"model_path = 'actions.h5'\n",
"model = load_model(model_path)\n",
"\n",
"reverse_label_map = {value: key for key, value in label_map.items()}\n",
"def mediapipe_detection(image, model):\n",
" image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
" image.flags.writeable = False\n",
" results = model.process(image)\n",
" image.flags.writeable = True\n",
" image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n",
" return image, results\n",
"\n",
"def draw_styled_landmarks(image, results):\n",
" if results.pose_landmarks:\n",
" mp_drawing.draw_landmarks(\n",
" image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS,\n",
" mp_drawing.DrawingSpec(color=(80,22,10), thickness=2, circle_radius=4),\n",
" mp_drawing.DrawingSpec(color=(80,44,121), thickness=2, circle_radius=2)\n",
" )\n",
" if results.left_hand_landmarks:\n",
" mp_drawing.draw_landmarks(\n",
" image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS,\n",
" mp_drawing.DrawingSpec(color=(121,22,76), thickness=2, circle_radius=4),\n",
" mp_drawing.DrawingSpec(color=(121,44,250), thickness=2, circle_radius=2)\n",
" )\n",
" if results.right_hand_landmarks:\n",
" mp_drawing.draw_landmarks(\n",
" image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS,\n",
" mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=4),\n",
" mp_drawing.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2)\n",
" )\n",
"\n",
"def extract_keypoints(results):\n",
" pose = np.array([[res.x, res.y, res.z] for res in results.pose_landmarks.landmark]).flatten() if results.pose_landmarks else np.zeros(33*3)\n",
" lh = np.array([[res.x, res.y, res.z] for res in results.left_hand_landmarks.landmark]).flatten() if results.left_hand_landmarks else np.zeros(21*3)\n",
" rh = np.array([[res.x, res.y, res.z] for res in results.right_hand_landmarks.landmark]).flatten() if results.right_hand_landmarks else np.zeros(21*3)\n",
" return np.concatenate([pose, lh, rh])\n",
"\n",
"sequence = []\n",
"sentence = []\n",
"threshold = 0.5\n",
"\n",
"cap = cv2.VideoCapture(0)\n",
"with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:\n",
" while cap.isOpened():\n",
" ret, frame = cap.read()\n",
" if not ret:\n",
" print(\"Error: Frame could not be read.\")\n",
" break\n",
"\n",
" frame = cv2.flip(frame, 1) \n",
"\n",
" image, results = mediapipe_detection(frame, holistic)\n",
" draw_styled_landmarks(image, results)\n",
"\n",
" if results.pose_landmarks or results.left_hand_landmarks or results.right_hand_landmarks:\n",
" keypoints = extract_keypoints(results)\n",
" sequence.append(keypoints)\n",
" sequence = sequence[-30:] # Keep the last 30 sequences\n",
"\n",
" if len(sequence) == 30:\n",
" res = model.predict(np.expand_dims(sequence, axis=0))[0]\n",
" action = actions[np.argmax(res)]\n",
" if action.endswith(\"_W\"):\n",
" color = (0, 0, 255) \n",
" elif action.endswith(\"_R\"):\n",
" color = (0, 255, 0) \n",
" else:\n",
" color = (255, 255, 255) \n",
"\n",
" # Display the predicted action on the screen\n",
" cv2.putText(image, f'Action: {action}', (15, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2, cv2.LINE_AA)\n",
"\n",
" cv2.imshow('OpenCV Feed', image)\n",
" if cv2.waitKey(10) & 0xFF == ord('q'):\n",
" break\n",
"\n",
" cap.release()\n",
" cv2.destroyAllWindows()\n"
" cv2.destroyAllWindows()"
]
}
],
Expand Down
97 changes: 96 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,99 @@ The model doesn't have the best accuracy but it partially works, I'm glad I did
- [ ] Proactive behaviours to find answers during the challenge
- [ ] Help others student’s projects
- [ ] Participation in feedbacks
- [ ] Explode
- [ ] Explode



### Real time predictions python code

```python
import cv2
import numpy as np
import mediapipe as mp
from tensorflow.keras.models import load_model

mp_holistic = mp.solutions.holistic
mp_drawing = mp.solutions.drawing_utils

model_path = 'actions.h5'
model = load_model(model_path)

reverse_label_map = {value: key for key, value in label_map.items()}
def mediapipe_detection(image, model):
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
results = model.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
return image, results

def draw_styled_landmarks(image, results):
if results.pose_landmarks:
mp_drawing.draw_landmarks(
image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(80,22,10), thickness=2, circle_radius=4),
mp_drawing.DrawingSpec(color=(80,44,121), thickness=2, circle_radius=2)
)
if results.left_hand_landmarks:
mp_drawing.draw_landmarks(
image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS,
mp_drawing.DrawingSpec(color=(121,22,76), thickness=2, circle_radius=4),
mp_drawing.DrawingSpec(color=(121,44,250), thickness=2, circle_radius=2)
)
if results.right_hand_landmarks:
mp_drawing.draw_landmarks(
image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS,
mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=4),
mp_drawing.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2)
)

def extract_keypoints(results):
pose = np.array([[res.x, res.y, res.z] for res in results.pose_landmarks.landmark]).flatten() if results.pose_landmarks else np.zeros(33*3)
lh = np.array([[res.x, res.y, res.z] for res in results.left_hand_landmarks.landmark]).flatten() if results.left_hand_landmarks else np.zeros(21*3)
rh = np.array([[res.x, res.y, res.z] for res in results.right_hand_landmarks.landmark]).flatten() if results.right_hand_landmarks else np.zeros(21*3)
return np.concatenate([pose, lh, rh])

sequence = []
sentence = []
threshold = 0.5

cap = cv2.VideoCapture(0)
with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Error: Frame could not be read.")
break

frame = cv2.flip(frame, 1)

image, results = mediapipe_detection(frame, holistic)
draw_styled_landmarks(image, results)

if results.pose_landmarks or results.left_hand_landmarks or results.right_hand_landmarks:
keypoints = extract_keypoints(results)
sequence.append(keypoints)
sequence = sequence[-30:] # Keep the last 30 sequences

if len(sequence) == 30:
res = model.predict(np.expand_dims(sequence, axis=0))[0]
action = actions[np.argmax(res)]
if action.endswith("_W"):
color = (0, 0, 255)
elif action.endswith("_R"):
color = (0, 255, 0)
else:
color = (255, 255, 255)

# Display the predicted action on the screen
cv2.putText(image, f'Action: {action}', (15, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2, cv2.LINE_AA)

cv2.imshow('OpenCV Feed', image)
if cv2.waitKey(10) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()
```

0 comments on commit 7d574a1

Please sign in to comment.