python+mediapipe+opencv实现手部关键点检测功能(手势识别)
程序员文章站
2024-01-02 12:59:16
目录一、mediapipe是什么?二、使用步骤1.引入库2.主代码3.识别结果补充:一、mediapipe是什么?mediapipe官网二、使用步骤1.引入库代码如下:import cv2from m...
一、mediapipe是什么?
二、使用步骤
1.引入库
代码如下:
import cv2 from mediapipe import solutions import time
2.主代码
代码如下:
cap = cv2.videocapture(0) mphands = solutions.hands hands = mphands.hands() mpdraw = solutions.drawing_utils ptime = 0 count = 0 while true: success, img = cap.read() imgrgb = cv2.cvtcolor(img, cv2.color_bgr2rgb) results = hands.process(imgrgb) if results.multi_hand_landmarks: for handlms in results.multi_hand_landmarks: mpdraw.draw_landmarks(img, handlms, mphands.hand_connections) ctime = time.time() fps = 1 / (ctime - ptime) ptime = ctime cv2.puttext(img, str(int(fps)), (25, 50), cv2.font_hershey_plain, 2, (255, 0, 0), 3) cv2.imshow("image", img) cv2.waitkey(1)
3.识别结果
以上就是今天要讲的内容,本文仅仅简单介绍了mediapipe的使用,而mediapipe提供了大量关于图像识别等的方法。
补充:
下面看下基于mediapipe人脸网状识别。
1.下载mediapipe库:
pip install mediapipe
2.完整代码:
import cv2 import mediapipe as mp import time mp_drawing = mp.solutions.drawing_utils mp_face_mesh = mp.solutions.face_mesh drawing_spec = mp_drawing.drawingspec(thickness=1, circle_radius=1) cap = cv2.videocapture("3.mp4") with mp_face_mesh.facemesh( min_detection_confidence=0.5, min_tracking_confidence=0.5) as face_mesh: while cap.isopened(): success, image = cap.read() if not success: print("ignoring empty camera frame.") # if loading a video, use 'break' instead of 'continue'. continue # flip the image horizontally for a later selfie-view display, and convert # the bgr image to rgb. image = cv2.cvtcolor(cv2.flip(image, 1), cv2.color_bgr2rgb) # to improve performance, optionally mark the image as not writeable to # pass by reference. image.flags.writeable = false results = face_mesh.process(image) time.sleep(0.02) # draw the face mesh annotations on the image. image.flags.writeable = true image = cv2.cvtcolor(image, cv2.color_rgb2bgr) if results.multi_face_landmarks: for face_landmarks in results.multi_face_landmarks: mp_drawing.draw_landmarks( image=image, landmark_list=face_landmarks, connections=mp_face_mesh.face_connections, landmark_drawing_spec=drawing_spec, connection_drawing_spec=drawing_spec) cv2.imshow('mediapipe facemesh', image) if cv2.waitkey(5) & 0xff == 27: break cap.release()
到此这篇关于python+mediapipe+opencv实现手部关键点检测功能(手势识别)的文章就介绍到这了,更多相关python mediapipe opencv手势识别内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!