├── HandTrackingModule.py
├── Readme.md
├── img
├── click.png
└── result.gif
├── libraries.bat
├── main.py
└── requirements.txt
/HandTrackingModule.py:
--------------------------------------------------------------------------------
1 | import cv2
2 | import mediapipe as mp
3 | import time
4 | import math
5 |
6 | class handDetector():
7 | def __init__(self, mode=False, maxHands=2, modelComplexity=1, detectionCon=0.5, trackCon=0.5):
8 | self.mode = mode
9 | self.maxHands = maxHands
10 | self.modelComplexity = modelComplexity
11 | self.detectionCon = detectionCon
12 | self.trackCon = trackCon
13 |
14 | self.mpHands = mp.solutions.hands
15 | self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.modelComplexity, self.detectionCon, self.trackCon)
16 | self.mpDraw = mp.solutions.drawing_utils
17 | self.tipIds = [4, 8, 12, 16, 20]
18 |
19 | def findHands(self, img, draw=True):
20 | imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
21 | self.results = self.hands.process(imgRGB)
22 | #print(results.multi_hand_landmarks)
23 |
24 | if self.results.multi_hand_landmarks:
25 | for handLms in self.results.multi_hand_landmarks:
26 | if draw:
27 | self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS)
28 | return img
29 |
30 | def findPosition(self, img, handNo=0, draw=True):
31 | xList = []
32 | yList = []
33 | bbox = []
34 | self.lmList = []
35 | if self.results.multi_hand_landmarks:
36 | myHand = self.results.multi_hand_landmarks[handNo]
37 | for id, lm in enumerate(myHand.landmark):
38 | #print(id, lm)
39 | h, w, c = img.shape
40 | cx, cy = int(lm.x*w), int(lm.y*h)
41 | xList.append(cx)
42 | yList.append(cy)
43 | #print(id, cx, cy)
44 | self.lmList.append([id, cx, cy])
45 | if draw:
46 | cv2.circle(img, (cx, cy), 5, (255,0,255), cv2.FILLED)
47 | xmin, xmax = min(xList), max(xList)
48 | ymin, ymax = min(yList), max(yList)
49 | bbox = xmin, ymin, xmax, ymax
50 |
51 | if draw:
52 | cv2.rectangle(img, (bbox[0]-20, bbox[1]-20), (bbox[2]+20, bbox[3]+20), (0, 255, 0), 2)
53 | return self.lmList, bbox
54 |
55 | def findDistance(self, p1, p2, img, draw=True):
56 | x1, y1 = self.lmList[p1][1], self.lmList[p1][2]
57 | x2, y2 = self.lmList[p2][1], self.lmList[p2][2]
58 | cx, cy = (x1+x2)//2, (y1+y2)//2
59 |
60 | if draw:
61 | cv2.circle(img, (x1,y1), 15, (255,0,255), cv2.FILLED)
62 | cv2.circle(img, (x2,y2), 15, (255,0,255), cv2.FILLED)
63 | cv2.line(img, (x1,y1), (x2,y2), (255,0,255), 3)
64 | cv2.circle(img, (cx,cy), 15, (255,0,255), cv2.FILLED)
65 |
66 | length = math.hypot(x2-x1, y2-y1)
67 | return length, img, [x1, y1, x2, y2, cx, cy]
68 |
69 | def fingersUp(self):
70 | fingers = []
71 |
72 | # Thumb
73 | if self.lmList[self.tipIds[0]][1] < self.lmList[self.tipIds[0]-1][1]:
74 | fingers.append(1)
75 | else:
76 | fingers.append(0)
77 |
78 | # 4 Fingers
79 | for id in range(1,5):
80 | if self.lmList[self.tipIds[id]][2] < self.lmList[self.tipIds[id]-2][2]:
81 | fingers.append(1)
82 | else:
83 | fingers.append(0)
84 | return fingers
85 |
86 | def main():
87 | pTime = 0
88 | cTime = 0
89 | cap = cv2.VideoCapture(0)
90 | detector = handDetector()
91 | while True:
92 | success, img = cap.read()
93 | img = detector.findHands(img)
94 | lmList = detector.findPosition(img)
95 | if len(lmList) != 0:
96 | print(lmList[1])
97 |
98 | cTime = time.time()
99 | fps = 1. / (cTime - pTime)
100 | pTime = cTime
101 |
102 | cv2.putText(img, str(int(fps)), (10,70), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,255), 3)
103 |
104 | cv2.imshow("Image", img)
105 | cv2.waitKey(1)
106 |
107 |
108 | if __name__ == "__main__":
109 | main()
--------------------------------------------------------------------------------
/Readme.md:
--------------------------------------------------------------------------------
1 | # Drag and Drop
2 | In this project I am going to learn how to create a virtual drag and drop system using opencv and python
3 |
4 | ## How to install
5 | 1. Clone this repository on your computer
6 | `https://github.com/paveldat/drag_and_drop.git`
7 | 2. Install all the requirements
8 | `run libraries.bat` or
9 | `pip install -r requirements.txt`
10 | 3. Run the program
11 | `python main.py`
12 |
13 | ## Help
14 | You might face issue with webcam not showing and you get errors.
15 | To solve it just change the value in this line (for example to `1`).
16 | `cap = cv2.VideoCapture(0)`
17 | Increment this number until you see your webcam.
18 |
19 | ## Hand Landmarks
20 |
21 |
22 | ## Click
23 | In order to simulate a click, you need to connect the index and middle fingers on your hand. An example of a valid click is shown in the image below.
24 |
25 |
26 |
27 | ## Result
28 | 
--------------------------------------------------------------------------------
/img/click.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/paveldat/drag_and_drop/41d7cc1dce6950ddaeb5a0bef510730ddc632215/img/click.png
--------------------------------------------------------------------------------
/img/result.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/paveldat/drag_and_drop/41d7cc1dce6950ddaeb5a0bef510730ddc632215/img/result.gif
--------------------------------------------------------------------------------
/libraries.bat:
--------------------------------------------------------------------------------
1 | pip install opencv-python
2 | pip install numpy
3 | pip install cvzone
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | import cv2
2 | import HandTrackingModule as htm
3 | import cvzone
4 | import numpy as np
5 |
6 | cap = cv2.VideoCapture(0)
7 | cap.set(3, 1280)
8 | cap.set(4, 720)
9 | detector = htm.handDetector(detectionCon=0.8)
10 | colorR = (255, 0, 255)
11 |
12 | cx, cy, w, h = 100, 100, 200, 200
13 |
14 | class DragAndDrogRectangle():
15 | def __init__(self, posCenter, size=[200, 200]):
16 | self.posCenter = posCenter
17 | self.size = size
18 |
19 | def update(self, cursor):
20 | cx, cy = self.posCenter
21 | w, h = self.size
22 |
23 | # If the index finger tip is in rectangle region
24 | if cx - w//2 < cursor[0] < cx + w//2 and cy - h//2 < cursor[1] < cy + h//2:
25 | self.posCenter = cursor
26 |
27 | rectList = []
28 | for x in range(5):
29 | rectList.append(DragAndDrogRectangle([x*250+150, 150]))
30 |
31 | while True:
32 | success, img = cap.read()
33 | img = cv2.flip(img, 1)
34 | img = detector.findHands(img)
35 | lmList, _ = detector.findPosition(img)
36 |
37 | if len(lmList) != 0:
38 | length, _, _ = detector.findDistance(8, 12, img, draw=False)
39 | if length < 40:
40 | cursor = lmList[8][1:] # index finger tip landmark
41 | # call the update
42 | for rect in rectList:
43 | rect.update(cursor)
44 |
45 | # Draw
46 | imgNew = np.zeros_like(img, np.uint8)
47 | for rect in rectList:
48 | cx, cy = rect.posCenter
49 | w, h = rect.size
50 | cv2.rectangle(imgNew, (cx - w//2, cy - h//2), (cx + w//2, cy + h//2), colorR, cv2.FILLED)
51 | cvzone.cornerRect(imgNew, (cx - w//2, cy - h//2, w, h), 20, rt=0)
52 |
53 | out = img.copy()
54 | alpha = 0.1
55 | mask = imgNew.astype(bool)
56 | out[mask] = cv2.addWeighted(img, alpha, imgNew, 1-alpha, 0)[mask]
57 |
58 | cv2.imshow("Image", out)
59 | cv2.waitKey(1)
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | opencv-python
2 | numpy
3 | cvzone
--------------------------------------------------------------------------------