• openCV实践项目:拖拽虚拟方块


    一、项目效果:

    学校宿舍今天搬家,累麻了,突然发现展示处理的也很粗糙,就这样吧嘿嘿~~~

    二、核心流程:

    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. 读取摄像头视频,画矩形:

    1. import cv2
    2. import time
    3. import numpy as np
    4. # 调用摄像头 0 默认摄像头
    5. cap = cv2.VideoCapture(0)
    6. # 初始方块数据
    7. x = 100
    8. y = 100
    9. w = 100
    10. h = 100
    11. # 读取一帧帧照片
    12. while True:
    13. # 返回frame图片
    14. rec,frame = cap.read()
    15. # 镜像
    16. frame = cv2.flip(frame,1)
    17. # 画矩形
    18. cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 255), -1)
    19. # 显示画面
    20. cv2.imshow('frame',frame)
    21. # 退出条件
    22. if cv2.waitKey(1) & 0xFF == ord('q'):
    23. break
    24. cap.release()
    25. cv2.destroyAllWindows()

    这是很基础的一步操作,此时我们运行这段代码,摄像头打开,我们会惊讶地看到自己英俊的脸庞,且左上角有个100*100的紫色矩形。

    2. 导入mediapipe处理手指坐标

    pip install mediapipe

    此时可能出现一些问题,比如openCV突然用不了了,没关系,卸载了重新下。

    mediapipe详细信息:Hands - mediapipe (google.github.io)

    简单来说,它会返回给我们21个手指关键点的坐标,即它在视频画面的位置比例( 0~1 ),我们乘以对应画面的宽高,就能得到手指对应的坐标了。

    本次用到食指和中指指尖,也就是8号和12号。

    2.1 配置一些基础信息:

    1. import cv2
    2. import time
    3. import numpy as np
    4. import mediapipe as mp
    5. mp_drawing = mp.solutions.drawing_utils
    6. mp_drawing_styles = mp.solutions.drawing_styles
    7. mp_hands = mp.solutions.hands
    8. hands = mp_hands.Hands(
    9. static_image_mode=True,
    10. max_num_hands=2,
    11. min_detection_confidence=0.5)

    2.2 在处理每一帧图像时,加入:

    1. frame.flags.writeable = False
    2. frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    3. # 返回结果
    4. results = hands.process(frame)
    5. frame.flags.writeable = True
    6. frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

    当我们在视频流中读取每一帧图片时,将其从BGR转为RGB供给mediapipe生成的hands对象读取,它会返回这张图片中手指关键点的信息,我们只需要继续对其作画,画在每一帧图片上。

    1. # 如果结果不为空
    2. if results.multi_hand_landmarks:
    3. # 遍历双手(根据读取顺序,一只只手遍历、画画)
    4. for hand_landmarks in results.multi_hand_landmarks:
    5. mp_drawing.draw_landmarks(
    6. frame,
    7. hand_landmarks,
    8. mp_hands.HAND_CONNECTIONS,
    9. mp_drawing_styles.get_default_hand_landmarks_style(),
    10. mp_drawing_styles.get_default_hand_connections_style())

    2.3 至此步骤完整代码

    1. import cv2
    2. import time
    3. import numpy as np
    4. import mediapipe as mp
    5. mp_drawing = mp.solutions.drawing_utils
    6. mp_drawing_styles = mp.solutions.drawing_styles
    7. mp_hands = mp.solutions.hands
    8. hands = mp_hands.Hands(
    9. static_image_mode=True,
    10. max_num_hands=2,
    11. min_detection_confidence=0.5)
    12. # 调用摄像头 0 默认摄像头
    13. cap = cv2.VideoCapture(0)
    14. # 方块初始数组
    15. x = 100
    16. y = 100
    17. w = 100
    18. h = 100
    19. # 读取一帧帧照片
    20. while True:
    21. # 返回frame图片
    22. rec,frame = cap.read()
    23. # 镜像
    24. frame = cv2.flip(frame,1)
    25. frame.flags.writeable = False
    26. frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    27. # 返回结果
    28. results = hands.process(frame)
    29. frame.flags.writeable = True
    30. frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    31. # 如果结果不为空
    32. if results.multi_hand_landmarks:
    33. # 遍历双手(根据读取顺序,一只只手遍历、画画)
    34. # results.multi_hand_landmarks n双手
    35. # hand_landmarks 每只手上21个点信息
    36. for hand_landmarks in results.multi_hand_landmarks:
    37. mp_drawing.draw_landmarks(
    38. frame,
    39. hand_landmarks,
    40. mp_hands.HAND_CONNECTIONS,
    41. mp_drawing_styles.get_default_hand_landmarks_style(),
    42. mp_drawing_styles.get_default_hand_connections_style())
    43. # 画矩形
    44. cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 255), -1)
    45. # 显示画面
    46. cv2.imshow('frame',frame)
    47. # 退出条件
    48. if cv2.waitKey(1) & 0xFF == ord('q'):
    49. break
    50. cap.release()
    51. cv2.destroyAllWindows()

    此时我们运行看一下还挺有意思的:

    3. 位置计算

    我们这个实验要求拖动方块,那肯定也有不拖动的时候,因此不妨根据上一步获取食指(8)中指(12)指尖的位置,如果这俩离得近,我们就在他与方块重合的时候,根据手指的位置改变方块的坐标。

    完整代码:

    1. import cv2
    2. import time
    3. import math
    4. import numpy as np
    5. import mediapipe as mp
    6. # mediapipe配置
    7. mp_drawing = mp.solutions.drawing_utils
    8. mp_drawing_styles = mp.solutions.drawing_styles
    9. mp_hands = mp.solutions.hands
    10. hands = mp_hands.Hands(
    11. static_image_mode=True,
    12. max_num_hands=2,
    13. min_detection_confidence=0.5)
    14. # 调用摄像头 0 默认摄像头
    15. cap = cv2.VideoCapture(0)
    16. # cv2.namedWindow("frame", 0)
    17. # cv2.resizeWindow("frame", 960, 640)
    18. # 获取画面宽度、高度
    19. width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    20. height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    21. # 方块初始数组
    22. x = 100
    23. y = 100
    24. w = 100
    25. h = 100
    26. L1 = 0
    27. L2 = 0
    28. on_square = False
    29. square_color = (0, 255, 0)
    30. # 读取一帧帧照片
    31. while True:
    32. # 返回frame图片
    33. rec,frame = cap.read()
    34. # 镜像
    35. frame = cv2.flip(frame,1)
    36. frame.flags.writeable = False
    37. frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    38. # 返回结果
    39. results = hands.process(frame)
    40. frame.flags.writeable = True
    41. frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    42. # 如果结果不为空
    43. if results.multi_hand_landmarks:
    44. # 遍历双手(根据读取顺序,一只只手遍历、画画)
    45. # results.multi_hand_landmarks n双手
    46. # hand_landmarks 每只手上21个点信息
    47. for hand_landmarks in results.multi_hand_landmarks:
    48. mp_drawing.draw_landmarks(
    49. frame,
    50. hand_landmarks,
    51. mp_hands.HAND_CONNECTIONS,
    52. mp_drawing_styles.get_default_hand_landmarks_style(),
    53. mp_drawing_styles.get_default_hand_connections_style())
    54. # 记录手指每个点的x y 坐标
    55. x_list = []
    56. y_list = []
    57. for landmark in hand_landmarks.landmark:
    58. x_list.append(landmark.x)
    59. y_list.append(landmark.y)
    60. # 获取食指指尖
    61. index_finger_x, index_finger_y = int(x_list[8] * width),int(y_list[8] * height)
    62. # 获取中指
    63. middle_finger_x,middle_finger_y = int(x_list[12] * width), int(y_list[12] * height)
    64. # 计算两指尖距离
    65. finger_distance = math.hypot((middle_finger_x - index_finger_x), (middle_finger_y - index_finger_y))
    66. # 如果双指合并(两之间距离近)
    67. if finger_distance < 60:
    68. # X坐标范围 Y坐标范围
    69. if (index_finger_x > x and index_finger_x < (x + w)) and (
    70. index_finger_y > y and index_finger_y < (y + h)):
    71. if on_square == False:
    72. L1 = index_finger_x - x
    73. L2 = index_finger_y - y
    74. square_color = (255, 0, 255)
    75. on_square = True
    76. else:
    77. # 双指不合并/分开
    78. on_square = False
    79. square_color = (0, 255, 0)
    80. # 更新坐标
    81. if on_square:
    82. x = index_finger_x - L1
    83. y = index_finger_y - L2
    84. # 图像融合 使方块不遮挡视频图片
    85. overlay = frame.copy()
    86. cv2.rectangle(frame, (x, y), (x + w, y + h), square_color, -1)
    87. frame = cv2.addWeighted(overlay, 0.5, frame, 1 - 0.5, 0)
    88. # 显示画面
    89. cv2.imshow('frame',frame)
    90. # 退出条件
    91. if cv2.waitKey(1) & 0xFF == ord('q'):
    92. break
    93. cap.release()
    94. cv2.destroyAllWindows()

  • 相关阅读:
    python学习笔记4-二分查找
    C++笔记 13 (STL初识)
    竞争性谈判文件
    一个月备考通过PMP
    C语言程序设计笔记(浙大翁恺版) 第十三周:文件
    如何快速开通使用腾讯云云点播服务?
    Django项目 - 合并PDF文件
    sqlite3的lib和头文件在哪下载 2023/9/19 上午10:46:43
    想要成功申请公派访问学者,特别需要注意这两点 !
    Windows的内存管理机制
  • 原文地址:https://blog.csdn.net/suic009/article/details/126534975