from flask import Flask, render_template, Response import cv2 import gradio as gr from cvzone.HandTrackingModule import HandDetector from cvzone.ClassificationModule import Classifier import numpy as np import math app = Flask(__name__) # Load the HandDetector and Classifier detector = HandDetector(maxHands=1) classifier = Classifier("keras_model.h5", "labels.txt") offset = 20 imgSize = 300 # Define labels labels = ["iam", "ok", "going", "no", "yes", "hi"] def process_frame(frame): imgOutput = frame.copy() hands, _ = detector.findHands(frame) if hands: hand = hands[0] x, y, w, h = hand['bbox'] imgWhite = np.ones((imgSize, imgSize, 3), np.uint8) * 255 imgCrop = frame[y - offset:y + h + offset, x - offset:x + w + offset] if imgCrop.size != 0: imgCropShape = imgCrop.shape aspectRatio = h / w if aspectRatio > 1: k = imgSize / h wCal = math.ceil(k * w) imgResize = cv2.resize(imgCrop, (wCal, imgSize)) imgResizeShape = imgResize.shape wGap = math.ceil((imgSize - wCal) / 2) imgWhite[:, wGap: wCal + wGap] = imgResize else: k = imgSize / w hCal = math.ceil(k * h) imgResize = cv2.resize(imgCrop, (imgSize, hCal)) imgResizeShape = imgResize.shape hGap = math.ceil((imgSize - hCal) / 2) imgWhite[hGap: hCal + hGap, :] = imgResize prediction, index = classifier.getPrediction(imgWhite, draw=False) cv2.rectangle(imgOutput, (x - offset, y - offset - 70), (x - offset + 400, y - offset + 60 - 50), (0, 255, 0), cv2.FILLED) cv2.putText(imgOutput, labels[index], (x, y - 30), cv2.FONT_HERSHEY_COMPLEX, 2, (0, 0, 0), 2) cv2.rectangle(imgOutput, (x - offset, y - offset), (x + w + offset, y + h + offset), (0, 255, 0), 4) _, img_encoded = cv2.imencode('.jpg', imgOutput) return img_encoded.tobytes() def generate_frames(): cap = cv2.VideoCapture(0) while True: success, frame = cap.read() if not success: break else: frame = process_frame(frame) yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') @app.route('/') def index(): return render_template('index.html') @app.route('/video_feed') def video_feed(): return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame') if __name__ == '__main__': app.run(debug=True)