Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
main.py 1.34 KiB
import numpy as np

from cvzone.HandTrackingModule import HandDetector

from Func.ClassifierModule import Classifier

from Func.Helpers import *

offset = 20
size = 300

capture = cv2.VideoCapture(0)
detector = HandDetector(maxHands=1)
model_path = "Model/keras_model.h5"
label_path = "Model/labels.txt"

classifier = Classifier(model_path, label_path)
letters = GetLabels(label_path)

if not capture.isOpened():
    print("Failed to open video capture.")
    exit()


def process_frame():
    success, img = capture.read()
    imgOutput = img.copy()

    if not success:
        return None

    hands, img = detector.findHands(img)

    if hands:
        hand = hands[0]
        x, y, w, h = hand['bbox']

        imgFixed = np.ones((size, size, 3), np.uint8)
        imgCropped = img[y - offset:y + h + offset, x - offset:x + w + offset]

        aspectRatio = h / w

        if aspectRatio > 1:
            CalculateWidth(size, h, w, imgCropped, imgFixed)
            prediction, index = GetPrediction(classifier, imgFixed)

        else:
            CalculateHeight(size, h, w, imgCropped, imgFixed)
            prediction, index = GetPrediction(classifier, imgFixed)

        cv2.putText(imgOutput, letters[index], (x, y), cv2.FONT_HERSHEY_TRIPLEX, 2, (255, 0, 255), 2)

    _, buffer = cv2.imencode('.jpg', imgOutput)
    frame = buffer.tobytes()
    return frame