MEMBUAT COMPUTER VISION VIRTUAL BUTTON
COMPUTER VISION VIRTUAL BUTTON
Kecerdasan Buatan (Artificial Intelligence atau AI) adalah cabang ilmu komputer yang berfokus pada pembuatan mesin atau program yang dapat melakukan tugas yang biasanya memerlukan kecerdasan manusia. Tugas-tugas tersebut meliputi, tetapi tidak terbatas pada, pengenalan suara, pengenalan gambar, pemahaman bahasa alami, pengambilan keputusan, dan pemecahan masalah. Berikut adalah beberapa aspek penting dari AI:
6. setelah iu kita buat file lagi untuk menjalankan code kalkulatornya seperti berikut dan pilih yang jupyter notebook
8. Lalu masukan saja kode berikut ini
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 = ''


.png)

.png)

.png)
.png)
.png)
Komentar
Posting Komentar