Hello all, today I show you how to write a palm recognition system in Python + OpenCV (26 lines of code). This tutorial requires minimal knowledge of OpenCV.
What we get in the output
Before we start
You will need:
- Python 3;
Installation is done through the package manager:
pip install opencv-python
pip install mediapipe
Writing the code
I decided to use mediapipe because it has an AI already trained to recognize the palm.
First you need to understand that the palm is made up of joints, which can be expressed as dots:
Next, we write the code itself:
import cv2 import mediapipe as mp cap = cv2.VideoCapture(0) #Camera hands = mp.solutions.hands.Hands(max_num_hands=1) #AI object for palm definition draw = mp.solutions.drawing_utils #For drawing the palm while True: #Closing the window if cv2.waitKey(1) & 0xFF == 27: break success, image = cap.read() #read image from camera image = cv2.flip(image, -1) #Flip the image for the correct image imageRGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #Convert to rgb results = hands.process(imageRGB) #work on mediapipe if results.multi_hand_landmarks: for handLms in results.multi_hand_landmarks: for id, lm in enumerate(handLms.landmark): h, w, c = image.shape cx, cy = int(lm.x * w), int(lm.y * h) draw.draw_landmarks(image, handLms, mp.solutions.hands.HAND_CONNECTIONS) cv2.imshow("Hand", image) #Display the image
The solution to the error: ImportError: DLL load failed can be found here.
There are comments in the code, but I suggest breaking it down in detail:
OpenCV and mediapipe imports:
import cv2 import mediapipe as mp
Necessary objects: camera (cap), neuronics to determine the hands (hands) with arguments max_num_hands - the maximum number of hands - and our "draw".
cap = cv2.VideoCapture(0) hands = mp.solutions.hands.Hands(max_num_hands=1) draw = mp.solutions.drawing_utils
Exit from loop if key number 27 is pressed (Esc):
if cv2.waitKey(1) & 0xFF == 27: break
We read the image from the camera, reflect the image vertically and horizontally, convert it to RGB. And, most interestingly, we give our picture to the palm definition.
success, image = cap.read() image = cv2.flip(image, -1) imageRGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = hands.process(imageRGB)
The first if-block checks if palms were found at all (any object that is not empty is true). Then we use the for loop to "loop" through the object with the set of these points. The second for - go through the points themselves from the set
draw.draw_landmarks is a handy utility that draws a palm on an image, with the following arguments: image, set of points, and what to draw (in our case, a hand).
if results.multi_hand_landmarks: for handLms in results.multi_hand_landmarks: for id, lm in enumerate(handLms.landmark): h, w, c = image.shape cx, cy = int(lm.x * w), int(lm.y * h) draw.draw_landmarks(image, handLms, mp.solutions.hands.HAND_CONNECTIONS)
Display the picture.
We wrote palm recognition in Python in 26 lines of code, cool isn't it?