import math import time import cv2 import numpy as np from cvzone.HandTrackingModule import HandDetector from cvzone.ClassificationModule import Classifier classifier = Classifier("model/keras_model.h5","model/labels.txt") labels = ["HELLO","HOW ARE YOU","WHERE ARE YOU"] cap = cv2.VideoCapture(0) detector = HandDetector(maxHands=2,) offset = 20 imgSize = 300 while True: success,img = cap.read() imgoutput = img.copy() hands,img = detector.findHands(img) if hands: hand = hands[0] x,y,w,h = hand['bbox'] imgWhite = np.ones((imgSize,imgSize,3),np.uint8)*255 imgCrop = img[y-offset:y+h+offset,x-offset:x+w+offset] imgCropshape = imgCrop.shape ascpectRatio = h/w if ascpectRatio>1: k = imgSize/h wcal = math.ceil(k*w) imgResize = cv2.resize(imgCrop,(wcal,imgSize)) imageResizeshape = imgResize.shape wgap = math.ceil((imgSize-wcal)/2) imgWhite[:,wgap:wcal+wgap] = imgResize prediction , index = classifier.getPrediction(imgWhite,draw=False) print(prediction,index) else: k = imgSize/w hcal = math.ceil(k*h) imgResize = cv2.resize(imgCrop,(imgSize,hcal)) imageResizeshape = imgResize.shape hgap = math.ceil((imgSize-hcal)/2) imgWhite[hgap:hcal+hgap,:] = imgResize prediction,index = classifier.getPrediction(imgWhite,draw=False) print(prediction,index) cv2.putText(imgoutput, labels[index], (x, y - 26), cv2.FONT_HERSHEY_COMPLEX, 1.7, (98, 200, 55)) #cv2.imshow("ImageCrop",imgCrop) #cv2.imshow("ImageWhite",imgWhite) cv2.imshow("image",imgoutput) cv2.waitKey(1)