程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python realizes AI video recognition -- gesture control

編輯:Python

use opencv Recognize gestures

Realization principle

use opencv Library takes a picture , use mediapipe Know others' hands and identification points , And then use opencv Add logo information to the video , Last use opencv Synthesize a dynamic video output

Code

import cv2
import mediapipe as mp
class handDetector(): # classic OOP
# Set the initial conditions 
def __init__(self, mode=False, maxHands=2, detectionCon=0.5, trackCon=0.5):
self.mode = mode
self.maxHands = maxHands # At most, several hands appear at the same time 
self.detectionCon = detectionCon # Test reliability 
self.trackCon = trackCon # Tracking credibility 
self.mpHands = mp.solutions.hands # use mediapipe Find hands 
self.hands = self.mpHands.Hands(self.mode, self.maxHands,
self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils
# Find your hand in the picture and return to this frame 
def findHands(self, img, draw=True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Digital video input 
self.results = self.hands.process(imgRGB) # Handle video search 
if self.results.multi_hand_landmarks: # If you find the identification point on your hand 
for handLms in self.results.multi_hand_landmarks:
if draw: # Draw the marking dots on the recognized hand 
self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS)
return img
# After finding the hand, project the joint position of the hand and record the data 
def findPosition(self, img, handNo=0, draw=True):
lmList = [] # Record the identification point on your hand 
if self.results.multi_hand_landmarks:
myHand = self.results.multi_hand_landmarks[handNo]
# Traverse the identification data , Output after processing 
for idNum, lm in enumerate(myHand.landmark):
h, w, c = img.shape # Height , depth , The channel number 
cx, cy = int(lm.x * w), int(lm.y * h) # coordinates 
lmList.append([idNum, cx, cy]) # Can be here print Let's see what it looks like 
if draw: # Draw a blue dot at the identified point 
cv2.circle(img, (cx, cy), 15, (0, 0, 255), cv2.FILLED)
return lmList
def main():
wCam, hCam = 640, 480 # Camera shooting size 
cap = cv2.VideoCapture(0) # Create a class to shoot 
cap.set(3, wCam) # Scale settings 
cap.set(4, hCam)
detector = handDetector(detectionCon=0.8) # Minimum accuracy 
tipIds = [4, 8, 12, 16, 20] # Number of fingers 
while True:
success, img = cap.read() # Get a frame 
img = detector.findHands(img) # Find your hand and return the marked picture 
lmList = detector.findPosition(img, draw=False) # Punctuation then returns data 
if len(lmList) != 0: # If you find a hand with a marked dot 
fingers = []
# The bending angle of the thumb 
# If the thumb of the first 4 The pixel position of the marked point is lower than 3 A marker point , Then it is curved 
if lmList[tipIds[0]][1] > lmList[tipIds[0] - 1][1]:
fingers.append(1)
else:
fingers.append(0)
# The other four finger angles are judged 
for idNum in range(1, 5):
if lmList[tipIds[idNum]][2] < lmList[tipIds[idNum] - 2][2]:
fingers.append(1)
else:
fingers.append(0)
print(fingers)
# Simple finger binary , Five fingers are used to represent five digit binary numbers 
output = 0
if fingers[0] == 1: # Thumb up 
output += 1
if fingers[1] == 1: # The index finger stands up 
output += 2
if fingers[2] == 1: # The middle finger stands up 
output += 4
if fingers[3] == 1: # The ring finger stands up 
output += 8
if fingers[4] == 1: # Little finger up 
output += 16
# Processing video , Draw a box , Write the recognized number on it 
cv2.rectangle(img, (20, 225), (250, 425), (0, 0, 0), cv2.FILLED)
cv2.putText(img, str(output), (45, 375), cv2.FONT_HERSHEY_PLAIN,
10, (255, 255, 255), 25)
cv2.imshow("Image", img) # Display a processed picture 
cv2.waitKey(1) # Equivalent to the number of frames , This is 1ms One frame ,1s60 frame 
if __name__ == "__main__": # In this way, you won't run the program directly when importing this file 
main()

Running results

The author is directly in Python3 Self contained IDLE When running this program in , There will be strange flashbacks . The following operation results are in PyCharm Run in :

Functional expansion

Based on the decimal system of the finger itself , The author turns finger counting into binary , Can express 32 A digital . If you count with two hands, it can mean 1024 A digital .
The determination of finger count is relatively simple , It's a direct comparison x The height of the coordinates, not the angle between them . This may be calculated by the product of trigonometric function and vector and determine the angle more accurately .
Fingers can be used for sign language as well as numbers . If there is a sign language action library , Then this program can be well used to help people who can't understand sign language .
There are other uses for using fingers to represent numbers , For example, use gestures to control volume and screen brightness .
Interested students can try to realize the above functions . If you have any questions or thoughts, please leave a message in the private message and comment area !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved