Praktikum computer vision Virtual calculator with phyton in Anaconda

 Di virtual button calculator ini terlebih dahulu kita harus menginstall library-library yang dibutuhkan, dengan cara buka anaconda prompt kemudian masukkan perintah berikut :

pip install cv2

pip install cvzone

pip install mediapipe

Setelah menginstall, bukalah Jupyter Notebook nya yang terdapat diantara pilihan fitur di anaconda, Kemudian pada anaconda jupyter notebook kita masukkan perintah atau source code . Perintah nya yaitu sebagai berikut :

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),
                      (225, 225, 225), cv2.FILLED)
        cv2.rectangle(img, self.pos, (self.pos[0]+self.width, self.pos[1]+self.height),
                      (50, 50, 50), 3)
        cv2.putText(img, self.value, (self.pos[0]+25, self.pos[1]+46), cv2.FONT_HERSHEY_PLAIN,
                    2, (50, 50, 50), 2)
    
    def checkClick(self, x, y):
        if self.pos[0] < x < self.pos[0]+self.width and self.pos[1] < y < self.pos[1]+self.height :
            cv2.rectangle(img, self.pos, (self.pos[0]+self.width, self.pos[1]+self.height),
                          (255, 255, 255), cv2.FILLED)
            cv2.rectangle(img, self.pos, (self.pos[0]+self.width, self.pos[1]+self.height),
                          (50, 50, 50), 3)
            cv2.putText(img, self.value, (self.pos[0]+15, self.pos[1]+58), cv2.FONT_HERSHEY_PLAIN,
                        4, (0, 0, 0), 4)
            return True
        else:
            return False
            
cam = cv2.VideoCapture(0)
cam.set(3, 1280)
cam.set(4, 720)
detector = HandDetector(detectionCon=0.8, maxHands=1)
buttonListValues = [
    ['1', '2', '3', '+'],
    ['4', '5', '6', '-'],
    ['7', '8', '9', '/'],
    ['0', '.', '*', '=']
]
buttonList = []
for y in range(4):
    for x in range(4):
        xpos = x*80 + 320
        ypos = y*80 + 90
        buttonList.append(Button((xpos, ypos), 70, 70, buttonListValues[y][x]))
myEquation = ''
delayCounter = 0
while True:
    success, img = cam.read()
    img = cv2.flip(img, 1)
    
    hands, img = detector.findHands(img, flipType=False)
    
    cv2.rectangle(img, (320, 10), (320+310, 10+70),
                  (225, 225, 225), cv2.FILLED)
    cv2.rectangle(img, (320, 10), (320+310, 10+70),
                  (50, 50, 50), 3)
    
    for button in buttonList:
        button.draw(img)
    
    if hands:
        lmList = hands[0]['lmList']
        length, info, img = detector.findDistance(lmList[8][:2], lmList[12][:2], img)
        x, y = lmList[8][:2]
        if length < 50:
            for i, button in enumerate(buttonList):
                if button.checkClick(x, y) and delayCounter == 0:
                    myValue = buttonListValues[int(i/4)][int(i%4)]
                    if myValue == '=':
                        if len(myEquation) > 0:
                            if myEquation[0] != '+' and myEquation[0] != '-' and myEquation[0] != '*' and myEquation[0] != '/':
                                myEquation = str(eval(myEquation))
                    else:
                        myEquation += myValue
                    delayCounter = 1
    
    if delayCounter != 0:
        delayCounter += 1
        if delayCounter > 10:
            delayCounter = 0
    
    cv2.putText(img, myEquation, (320+10, 10+50), cv2.FONT_HERSHEY_PLAIN,
                3, (50, 50, 50), 3)
    cv2.imshow("Camera", img)
    key = cv2.waitKey(1)
    
    if key == ord('c'):
        myEquation = ''


link source code:

https://drive.google.com/file/d/1hjuLv5stsNYyHVPh_lErGneecHz4jlro/view?usp=drive_link


Hasil (output)

Link:  https://youtu.be/UceoR7FtuIo






Komentar

Postingan populer dari blog ini

Resep empal gentong (makanan khas cirebon)

Laporan ujian akhir semester VI - Poyek pemrograman mobile

UNIX KERNEL