Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- from cvzone.HandTrackingModule import HandDetector
- class Button:
- def __init__(self, pos, width, height, value):
- self.pos = pos
- self.width = width
- self.height = height
- self.value = value
- def draw(self, img):
- cv2.rectangle(img, self.pos, (self.pos[0] + self.width, self.pos[1] + self.height), (256, 256, 256), cv2.FILLED)
- cv2.rectangle(img, self.pos, (self.pos[0] + self.width, self.pos[1] + self.height), (0, 0, 0), 4)
- cv2.putText(img, self.value, (self.pos[0] + 40, self.pos[1] + 60), cv2.FONT_HERSHEY_PLAIN, 2, (50, 50, 50), 2)
- cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
- cap.set(3, 1280)
- cap.set(4, 720)
- detector = HandDetector(detectionCon=0.8, maxHands=1)
- buttonListValues = [['7', '8', '9', '*'], ['4', '5', '6', '-'], ['1', '2', '3', '+'], ['0', '/', '.', '=']]
- buttonList = []
- for x in range(4):
- for y in range(4):
- xpos = x*100 + 800
- ypos = y*100 + 150
- buttonList.append(Button((xpos, ypos), 100, 100, buttonListValues[y][x]))
- myEquation = '10-5'
- while True:
- success, img = cap.read()
- img = cv2.flip(img, 1)
- hands, img = detector.findHands(img, flipType=False)
- cv2.rectangle(img, (800, 70), (800 + 400, 70 + 100), (256, 256, 256), cv2.FILLED)
- cv2.rectangle(img, (800, 70), (800 + 400, 70 + 100), (0, 0, 0), 4)
- for button in buttonList:
- button.draw(img)
- if hands:
- lmList = hands[0]['lmList']
- length, _, img = detector.findDistance(lmList[8], lmList[12], img)
- print(length)
- x, y = lmList[8]
- cv2.putText(img, myEquation, (810, 130), cv2.FONT_HERSHEY_PLAIN, 4, (50, 50, 50), 4)
- cv2.imshow("Image", img)
- cv2.waitKey(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement