这次我们是将会进行一个小的矩形手势的移动.
import cv2
from cvzone.HandTrackingModule import HandDetector
cap = cv2.VideoCapture(0)
# cap.set(3, 1280)
# cap.set(4, 720)
colorR = (0, 0, 255)
detector = HandDetector(detectionCon=0.7)
cx, cy, w, h = 100, 100, 200, 200
while True:
success, img = cap.read()
img = cv2.flip(img, 1)
detector.findHands(img)
lmList, _ = detector.findPosition(img)
if lmList:
# 判断8和12的距离
l, _, _ = detector.findDistance(8, 12, img)
# print(l)
# 如果食指和中指的距离大于70,就主动放弃拖动,
if l < 70:
cursor = lmList[8]
if cx - w // 2 < cursor[0] < cx + w // 2 and cy - h // 2 < cursor[1] < cy + h // 2:
colorR = 0, 255, 0
cx, cy = cursor
else:
colorR = 0, 0, 255
# 创建方框
cv2.rectangle(img, (cx - w // 2, cy - h // 2), (cx + w // 2, cy + h // 2), colorR, cv2.FILLED)
cv2.imshow("Image", img)
cv2.waitKey(1)