一、项目效果二、核心流程1、opencv读取视频流、在每一帧图片上画一个矩形。
2、使用mediapipe获取手指关键点坐标。
3、根据手指坐标位置和矩形的坐标位置,判断手指点是否在矩形上,如果在则矩形跟随手指移动。
三、代码流程环境准备:
python: 3.8.8
opencv: 4.2.0.32
mediapipe: 0.8.10.1
注:
1、opencv版本过高或过低可能出现一些如摄像头打不开、闪退等问题,python版本影响opencv可选择的版本。
2、pip install mediapipe 后可能导致opencv无法正常使用,卸了重新下载,习惯了就好。
1. 读取摄像头视频,画矩形import cv2import timeimport numpy as np  # 调用摄像头 0 默认摄像头 cap = cv2.videocapture(0) # 初始方块数据x = 100y = 100w = 100h = 100 # 读取一帧帧照片while true:    # 返回frame图片    rec,frame = cap.read()        # 镜像    frame = cv2.flip(frame,1)        # 画矩形     cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 255), -1)     # 显示画面    cv2.imshow('frame',frame)        # 退出条件    if cv2.waitkey(1) & 0xff == ord('q'):        break    cap.release()cv2.destroyallwindows()
这是很基础的一步操作,此时我们运行这段代码,摄像头打开,我们会惊讶地看到自己英俊的脸庞,且左上角有个100*100的紫色矩形。
2. 导入mediapipe处理手指坐标pip install mediapipe
此时可能出现一些问题,比如opencv突然用不了了,没关系,卸载了重新下。
mediapipe详细信息:hands - mediapipe (google.github.io)
简单来说,它会返回给我们21个手指关键点的坐标,即它在视频画面的位置比例( 0~1 ),我们乘以对应画面的宽高,就能得到手指对应的坐标了。
本次用到食指和中指指尖,也就是8号和12号。
2.1 配置一些基础信息
import cv2import timeimport numpy as npimport mediapipe as mp  mp_drawing = mp.solutions.drawing_utilsmp_drawing_styles = mp.solutions.drawing_stylesmp_hands = mp.solutions.hands hands =  mp_hands.hands(    static_image_mode=true,    max_num_hands=2,    min_detection_confidence=0.5)
2.2 在处理每一帧图像时,加入
    frame.flags.writeable = false    frame = cv2.cvtcolor(frame, cv2.color_bgr2rgb)    # 返回结果    results = hands.process(frame)     frame.flags.writeable = true    frame = cv2.cvtcolor(frame, cv2.color_rgb2bgr)
当我们在视频流中读取每一帧图片时,将其从bgr转为rgb供给mediapipe生成的hands对象读取,它会返回这张图片中手指关键点的信息,我们只需要继续对其作画,画在每一帧图片上。
    # 如果结果不为空    if results.multi_hand_landmarks:         # 遍历双手(根据读取顺序,一只只手遍历、画画)        for hand_landmarks in results.multi_hand_landmarks:            mp_drawing.draw_landmarks(                frame,                hand_landmarks,                mp_hands.hand_connections,                mp_drawing_styles.get_default_hand_landmarks_style(),                mp_drawing_styles.get_default_hand_connections_style())
2.3 至此步骤完整代码
import cv2import timeimport numpy as npimport mediapipe as mp  mp_drawing = mp.solutions.drawing_utilsmp_drawing_styles = mp.solutions.drawing_stylesmp_hands = mp.solutions.hands hands =  mp_hands.hands(    static_image_mode=true,    max_num_hands=2,    min_detection_confidence=0.5)  # 调用摄像头 0 默认摄像头 cap = cv2.videocapture(0) # 方块初始数组x = 100y = 100w = 100h = 100  # 读取一帧帧照片while true:    # 返回frame图片    rec,frame = cap.read()        # 镜像    frame = cv2.flip(frame,1)                frame.flags.writeable = false    frame = cv2.cvtcolor(frame, cv2.color_bgr2rgb)    # 返回结果    results = hands.process(frame)     frame.flags.writeable = true    frame = cv2.cvtcolor(frame, cv2.color_rgb2bgr)            # 如果结果不为空    if results.multi_hand_landmarks:         # 遍历双手(根据读取顺序,一只只手遍历、画画)        # results.multi_hand_landmarks n双手        # hand_landmarks 每只手上21个点信息        for hand_landmarks in results.multi_hand_landmarks:            mp_drawing.draw_landmarks(                frame,                hand_landmarks,                mp_hands.hand_connections,                mp_drawing_styles.get_default_hand_landmarks_style(),                mp_drawing_styles.get_default_hand_connections_style())            # 画矩形     cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 255), -1)     # 显示画面    cv2.imshow('frame',frame)        # 退出条件    if cv2.waitkey(1) & 0xff == ord('q'):        break    cap.release()cv2.destroyallwindows()
3. 位置计算我们这个实验要求拖动方块,那肯定也有不拖动的时候,因此不妨根据上一步获取食指(8)和中指(12)指尖的位置,如果这俩离得近,我们就在他与方块重合的时候,根据手指的位置改变方块的坐标。
完整代码
import cv2import timeimport mathimport numpy as npimport mediapipe as mp # mediapipe配置mp_drawing = mp.solutions.drawing_utilsmp_drawing_styles = mp.solutions.drawing_stylesmp_hands = mp.solutions.handshands =  mp_hands.hands(    static_image_mode=true,    max_num_hands=2,    min_detection_confidence=0.5)  # 调用摄像头 0 默认摄像头 cap = cv2.videocapture(0) # cv2.namedwindow("frame", 0)# cv2.resizewindow("frame", 960, 640)  # 获取画面宽度、高度width = int(cap.get(cv2.cap_prop_frame_width))height = int(cap.get(cv2.cap_prop_frame_height))  # 方块初始数组x = 100y = 100w = 100h = 100 l1 = 0l2 = 0 on_square = falsesquare_color = (0, 255, 0) # 读取一帧帧照片while true:    # 返回frame图片    rec,frame = cap.read()        # 镜像    frame = cv2.flip(frame,1)                frame.flags.writeable = false    frame = cv2.cvtcolor(frame, cv2.color_bgr2rgb)    # 返回结果    results = hands.process(frame)     frame.flags.writeable = true    frame = cv2.cvtcolor(frame, cv2.color_rgb2bgr)            # 如果结果不为空    if results.multi_hand_landmarks:          # 遍历双手(根据读取顺序,一只只手遍历、画画)        # results.multi_hand_landmarks n双手        # hand_landmarks 每只手上21个点信息        for hand_landmarks in results.multi_hand_landmarks:            mp_drawing.draw_landmarks(                frame,                hand_landmarks,                mp_hands.hand_connections,                mp_drawing_styles.get_default_hand_landmarks_style(),                mp_drawing_styles.get_default_hand_connections_style())                        # 记录手指每个点的x y 坐标            x_list = []            y_list = []            for landmark in hand_landmarks.landmark:                x_list.append(landmark.x)                y_list.append(landmark.y)                                        # 获取食指指尖            index_finger_x, index_finger_y = int(x_list[8] * width),int(y_list[8] * height)             # 获取中指            middle_finger_x,middle_finger_y = int(x_list[12] * width), int(y_list[12] * height)              # 计算两指尖距离            finger_distance = math.hypot((middle_finger_x - index_finger_x), (middle_finger_y - index_finger_y))             # 如果双指合并(两之间距离近)            if finger_distance < 60:                 # x坐标范围 y坐标范围                if (index_finger_x > x and index_finger_x < (x + w)) and (                        index_finger_y > y and index_finger_y < (y + h)):                     if on_square == false:                        l1 = index_finger_x - x                        l2 = index_finger_y - y                        square_color = (255, 0, 255)                        on_square = true             else:                # 双指不合并/分开                on_square = false                square_color = (0, 255, 0)             # 更新坐标            if on_square:                x = index_finger_x - l1                y = index_finger_y - l2                             # 图像融合 使方块不遮挡视频图片    overlay = frame.copy()    cv2.rectangle(frame, (x, y), (x + w, y + h), square_color, -1)    frame = cv2.addweighted(overlay, 0.5, frame, 1 - 0.5, 0)         # 显示画面    cv2.imshow('frame',frame)        # 退出条件    if cv2.waitkey(1) & 0xff == ord('q'):        break    cap.release()cv2.destroyallwindows()
以上就是python+opencv怎么实现拖拽虚拟方块效果的详细内容。
   
 
   