-
Notifications
You must be signed in to change notification settings - Fork 0
/
Img_Drag_Adv.py
79 lines (61 loc) · 1.89 KB
/
Img_Drag_Adv.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
70
71
72
73
74
75
76
77
78
79
import cv2
import numpy as np
import Hand_tracking as htm
import cvzone
import os
# from cvzone.HandTrackingModule import HandDetector
cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
detector = htm.handDetector(detectionCon=0.8)
class DragImg():
def __init__(self, path, posOrigin, imgType):
self.path = path
self.posOrigin = posOrigin
self.imgType = imgType
if self.imgType == 'png':
self.img = cv2.imread(self.path,cv2.IMREAD_UNCHANGED)
else:
self.img = cv2.imread(self.path)
self.size = self.img.shape[:2]
def Update(self, cursor):
ox, oy = self.posOrigin
w, h = self.size
if ox<cursor[0]<ox + w and oy<cursor[1]<oy + h:
self.posOrigin = cursor[0]-w//2, cursor[1]-h//2
path = "Virtual Drag and Drop/images"
myList = os.listdir(path)
imgList = []
for x, imgPath in enumerate(myList):
if 'png' in imgPath:
imgType = 'png'
else:
imgType = 'jpg'
imgList.append(DragImg(f'{path}/{imgPath}', [50+x*300, 50], imgType))
while True:
success, frame = cap.read()
frame = cv2.flip(frame, 1)
frame = detector.findHands(frame)
lmList = detector.findPosition(frame)
if lmList:
length,info,frame = detector.findDistance(8, 12, frame)
print(length)
if length < 50:
cursor = lmList[8]
for imgObj in imgList:
imgObj.Update(cursor[1:])
try:
for imgObj in imgList:
h, w = imgObj.size
ox, oy = imgObj.posOrigin
if imgObj.imgType == 'png':
frame = cvzone.overlayPNG(frame, imgObj.img, [ox, oy])
else:
frame[oy:oy + h, ox:ox + w] = imgObj.img
except:
pass
cv2.imshow("Image", frame)
if cv2.waitKey(1) & 0xFF == ord("e"):
break
cap.release()
cv2.destroyAllWindows()