Skip to content

Instantly share code, notes, and snippets.

@sithumonline
Last active January 9, 2022 07:54
Show Gist options
  • Select an option

  • Save sithumonline/a5c52cc49e4295b5377d790780df0ba2 to your computer and use it in GitHub Desktop.

Select an option

Save sithumonline/a5c52cc49e4295b5377d790780df0ba2 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
from cvzone.HandTrackingModule import HandDetector
import cvzone
cap = cv2.VideoCapture(0)
cap.set(3, 1920)
cap.set(4, 1080)
detector = HandDetector(detectionCon=0.8)
colorR = (21, 50, 175)
# cx, cy, w, h = 100, 100, 200, 200
class DragRect():
def __init__(self, posCenter, size=[100, 100]):
self.posCenter = posCenter
self.size = size
def update(self, cursor):
cx, cy = self.posCenter
w, h = self.size
x1, y1, x2, y2 = cx - w // 2, cy - h // 2, cx + w // w, cy + h // 2
if X1 < cursor[0] < X2 and y1 < cursor[1] < y2:
# colorR = (0, 255, 0)
self.posCenter = cursor
rectList = []
for x in range(8):
rectList.append(DragRect([x * 150 + 110, 160]))
while True:
_, img = cap.read()
img = cv2.flip(img, 1)
img = detector.findHands(img)
lmList, _ = detector.findPosition(img)
if lmList:
l, _, _ = detector.findDistance(4, 8, img)
if l < 40:
cursor = lmList[8]
for rect in rectList:
rect.update(cursor)
# Drag solid
# for rect in rectList
# cx, cy = rect.posCenter
# w, h = rect.size
# x1, y1, x2, y2 = cx-w//2, cy-h//2, cx+w//2, cy+h//2
# cv2.rectangle(img, (x1, y1), (x2, y2), colorR, cv2.FILLED)
# cvzone.cornerRect(img, (cx-w//2, cy-h//2, w, h), 28, rt=0)
## Draw Transperency
imgNew = np.zeros_like(img)
count = 0
for rect in rectList:
cx, cy = rect.posCenter
w, h = rect.size
x1, y1, x2, y2 = cx-w//2, cy-h//2, cx+w//2, cy+h//2
cv2.rectangle(imgNew, (x1, y1), (x2, y2), colorR, cv2.FILLED)
cvzone.cornerRect(imgNew, (cx-w//2, cy-h//2, w, h), 20, rt=0)
myName = ["C", "A", "T", "I", "N", "O", "R", "S"]
cv2.putText(imgNew, myName[count], (x1+20, y2-20), cv2.FONT_HERSHEY_COMPLEX, 3, (1, 1, 1), 5)
cv2.putText(img, 'Word Create Game', (300, 70), cv2.FONT_HERSKEY_DUPLEX, 2, (255, 0, 0), 4)
out = img.copy()
alpha = 0.1
mask = imgNew.astype(bool)
out[mask] = cv2.addWeighted(img, alpha, imgNew, 1 - alpha, 0)[mask]
cv.imshow("Image", out)
cv2.waitKey(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment