# https://www.geeksforgeeks.org/face-and-hand-landmarks-detection-using-python-mediapipe-opencv/?ref=rp
# Import Libraries
import cv2
import time
import mediapipe as mp
import numpy as np
from tensorflow.keras.models import load_model
# Load the gesture recognizer model
model = load_model('mp_hand_gesture')
# Load class names
f = open('gesture.names', 'r')
classNames = f.read().split('\n')
f.close()
print(classNames)
# Grabbing the Holistic Model from Mediapipe and
# Initializing the Model
mp_holistic = mp.solutions.holistic
holistic_model = mp_holistic.Holistic(
min_detection_confidence=0.5,
min_tracking_confidence=0.5
)
# Initializing the drawng utils for drawing the facial landmarks on image
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
# (0) in VideoCapture is used to connect to your computer's default camera
capture = cv2.VideoCapture(0)
# Initializing current time and precious time for calculating the FPS
previousTime = 0
currentTime = 0
while capture.isOpened():
# capture frame by frame
ret, frame = capture.read()
# resizing the frame for better view
frame = cv2.resize(frame, (800, 600))
x, y, c = frame.shape
# Converting the from from BGR to RGB
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Making predictions using holistic model
# To improve performance, optionally mark the image as not writable to
# pass by reference.
# image.flags.writable = False
results = holistic_model.process(image)
# image.flags.writable = True
# Converting back the RGB image to BGR
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
mp_drawing.draw_landmarks(
image,
results.face_landmarks,
mp_holistic.FACEMESH_CONTOURS,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles
.get_default_face_mesh_contours_style()
)
# Drawing Right hand Land Marks
mp_drawing.draw_landmarks(
image,
results.right_hand_landmarks,
mp_holistic.HAND_CONNECTIONS
)
# Drawing Left hand Land Marks
mp_drawing.draw_landmarks(
image,
results.left_hand_landmarks,
mp_holistic.HAND_CONNECTIONS
)
# Calculating the FPS
currentTime = time.time()
fps = 1 / (currentTime-previousTime)
previousTime = currentTime
if results.right_hand_landmarks:
print('right_hand_landmarks:',results.right_hand_landmarks)
print('right_hand_landmarks landmark:',results.right_hand_landmarks.landmark)
landmarks = []
for lm in results.right_hand_landmarks.landmark:
# print(id, lm)
lmx = int(lm.x * x)
lmy = int(lm.y * y)
landmarks.append([lmx, lmy])
# Predict gesture
prediction = model.predict([landmarks])
# print(prediction)
classID = np.argmax(prediction)
className = classNames[classID]
# show the prediction on the frame
cv2.putText(image, className, (10, 50), cv2.FONT_HERSHEY_SIMPLEX,
1, (0,0,255), 2, cv2.LINE_AA)
# Displaying FPS on the image
cv2.putText(image, str(int(fps))+" FPS", (10, 70), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
# Display the resulting image
cv2.imshow("Facial and Hand Landmarks", image)
# Enter key 'q' to break the loop
if cv2.waitKey(5) & 0xFF == ord('q'):
break
# When all the process is done
# Release the capture and destroy all windows
capture.release()
cv2.destroyAllWindows()
网友评论