Tutorial Membuat Kalkulator Hand-Tracking dengan OpenCV dan cvzone
1. Bahan yang Diperlukan:
Python: Bahasa pemrograman yang akan digunakan.
OpenCV: Library untuk pemrosesan gambar dan video.
cvzone: Library yang memudahkan penggunaan OpenCV untuk proyek AI/Computer Vision.
HandTrackingModule dari cvzone: Modul untuk mendeteksi tangan.
Kamera: Bisa menggunakan webcam laptop atau kamera eksternal.
2. Langkah-langkah Pembuatan:
Langkah 1: Install Library yang Diperlukan
Sebelum memulai, pastikan Anda sudah menginstal Python dan pip. Kemudian, instal library yang dibutuhkan dengan menjalankan perintah berikut di terminal atau command prompt:
pip install opencv-python
pip install cvzone
Langkah 2: Membuat Kelas Button
Buat kelas Button untuk mewakili tombol pada kalkulator. Kelas ini akan memiliki metode untuk menggambar tombol pada layar dan memeriksa apakah tombol tersebut diklik.
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]+10, self.pos[1]+self.height//2+10), cv2.FONT_HERSHEY_PLAIN, 1.5, (50, 50, 50), 2)
def checkClick(self, x, y, img):
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]+10, self.pos[1]+self.height//2+15), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 0), 3)
return True
else:
return False
Langkah 3: Menginisialisasi Kamera dan Detector
Menggunakan OpenCV untuk menangkap video dari kamera dan menggunakan HandDetector dari cvzone untuk mendeteksi tangan.
# Initialize the camera
cam = cv2.VideoCapture(0)
cam.set(3, 1280) # Set camera width
cam.set(4, 720) # Set camera height
detector = HandDetector(detectionCon=0.8, maxHands=1)
Langkah 4: Membuat Tombol Kalkulator
Buat layout tombol untuk kalkulator. Setiap tombol akan diwakili oleh sebuah objek Button.
# Define button layout
buttonListValues = [
['1', '2', '3', '+'],
['4', '5', '6', '-'],
['7', '8', '9', '*'],
['0', '.', '/', '='],
['Del', 'Reset']
]
buttonList = []
for i in range(5):
for j in range(4):
if i == 4 and j > 1:
break
xpos = j * 60 + 320 # Smaller button width
ypos = i * 60 + 90 # Smaller button height
buttonList.append(Button((xpos, ypos), 50, 50, buttonListValues[i][j]))
Langkah 5: Membuat Loop Utama
Loop utama untuk menangkap frame dari kamera, mendeteksi tangan, memproses input, dan menampilkan hasil.
myEquation = ''
delayCounter = 0
while True:
success, img = cam.read()
if not success:
break
img = cv2.flip(img, 1)
hands, img = detector.findHands(img, flipType=False)
# Draw the calculator display
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)
# Draw buttons
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 < 40: # Reduced distance for click detection
for i, button in enumerate(buttonList):
if button.checkClick(x, y, img) and delayCounter == 0:
myValue = button.value
if myValue == '=':
try:
myEquation = str(eval(myEquation))
except:
myEquation = 'Error'
elif myValue == 'Del':
myEquation = myEquation[:-1]
elif myValue == 'Reset':
myEquation = ''
else:
myEquation += myValue
delayCounter = 1
# Delay counter to prevent multiple clicks
if delayCounter != 0:
delayCounter += 1
if delayCounter > 30: # Increased delay counter duration
delayCounter = 0
# Display the equation/result
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 = ''
# Release the camera and close all windows
cam.release()
cv2.destroyAllWindows()
Dengan mengikuti langkah-langkah di atas, Anda dapat membuat kalkulator berbasis hand-tracking yang dapat mendeteksi gerakan tangan dan menjalankan operasi kalkulator sesuai dengan input yang diberikan.

Komentar
Posting Komentar