-
Notifications
You must be signed in to change notification settings - Fork 0
/
V_DragNDrop.py
69 lines (57 loc) · 1.86 KB
/
V_DragNDrop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import cv2
import mediapipe as mp
import numpy as np
import Hand_tracking as htm
# from cvzone.HandTrackingModule import HandDetector
cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
detector = htm.handDetector(detectionCon=0.8)
# detector = HandDetector(detectionCon=0.8)
colorR = (255, 0, 255)
cx, cy, w, h = 100, 100, 150, 150
class DragRect():
def __init__(self, posCenter, size=[200,200]):
self.posCenter = posCenter
self.size = size
def Update(self, cursor):
cx, cy = self.posCenter
w, h = self.size
if cx - w//2<cursor[0]<cx + w//2 and cy - h//2<cursor[1]<cy + h//2:
self.posCenter = cursor
rectList = []
for x in range(5):
rectList.append(DragRect([x*250+150,150]))
while True:
success, frame = cap.read()
frame = cv2.flip(frame, 1)
frame = detector.findHands(frame)
# hands, frame = detector.findHands(frame)
lmList = detector.findPosition(frame)
# if len(lmList) != 0:
# print(lmList[8])
# if hands:
# hand1 = hands[0]
# lmList = hand1["lmList"]
# cursor = lmList[8]
# length,info,frame = detector.findDistance(lmList[8], lmList[12], frame)
# print(length)
# if length<30:
# print(cursor)
if lmList:
cursor = lmList[8]
# length,info,frame = detector.findDistance(lmList[8], lmList[12], frame)
length,info,frame = detector.findDistance(8, 12, frame)
print(length)
if length<50:
for rect in rectList:
rect.Update(cursor[1:])
for rect in rectList:
cx, cy = rect.posCenter
w, h = rect.size
cv2.rectangle(frame, (cx - w//2, cy - h//2), (cx + w//2, cy + h//2), colorR, cv2.FILLED)
cv2.imshow("Image", frame)
if cv2.waitKey(1) & 0xFF == ord("e"):
break
cap.release()
cv2.destroyAllWindows()