Advertisement
Guest User

CALCULATOR.PY

a guest
Mar 29th, 2022
61
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import cv2
  2. from cvzone.HandTrackingModule import HandDetector
  3.  
  4. class Button:
  5.     def __init__(self, pos, width, height, value):
  6.  
  7.         self.pos = pos
  8.         self.width = width
  9.         self.height = height
  10.         self.value = value
  11.  
  12.     def draw(self, img):
  13.         cv2.rectangle(img, self.pos, (self.pos[0] + self.width, self.pos[1] + self.height), (256, 256, 256), cv2.FILLED)
  14.         cv2.rectangle(img, self.pos, (self.pos[0] + self.width, self.pos[1] + self.height), (0, 0, 0), 4)
  15.         cv2.putText(img, self.value, (self.pos[0] + 40, self.pos[1] + 60), cv2.FONT_HERSHEY_PLAIN, 2, (50, 50, 50), 2)
  16.  
  17.  
  18. cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
  19. cap.set(3, 1280)
  20. cap.set(4, 720)
  21. detector = HandDetector(detectionCon=0.8, maxHands=1)
  22.  
  23. buttonListValues = [['7', '8', '9', '*'], ['4', '5', '6', '-'], ['1', '2', '3', '+'], ['0', '/', '.', '=']]
  24.  
  25.  
  26. buttonList = []
  27. for x in range(4):
  28.     for y in range(4):
  29.         xpos = x*100 + 800
  30.         ypos = y*100 + 150
  31.         buttonList.append(Button((xpos, ypos), 100, 100, buttonListValues[y][x]))
  32.  
  33. myEquation = '10-5'
  34.  
  35. while True:
  36.     success, img = cap.read()
  37.     img = cv2.flip(img, 1)
  38.    
  39.     hands, img = detector.findHands(img, flipType=False)
  40.  
  41.     cv2.rectangle(img, (800, 70), (800 + 400, 70 + 100), (256, 256, 256), cv2.FILLED)
  42.     cv2.rectangle(img, (800, 70), (800 + 400, 70 + 100), (0, 0, 0), 4)
  43.     for button in buttonList:
  44.         button.draw(img)
  45.        
  46.     if hands:
  47.         lmList = hands[0]['lmList']
  48.         length, _, img = detector.findDistance(lmList[8], lmList[12], img)
  49.         print(length)
  50.         x, y = lmList[8]
  51.  
  52.     cv2.putText(img, myEquation, (810, 130), cv2.FONT_HERSHEY_PLAIN, 4, (50, 50, 50), 4)
  53.  
  54.     cv2.imshow("Image", img)
  55.     cv2.waitKey(1)
  56.  
  57.  
Advertisement
RAW Paste Data Copied
Advertisement