RomRam1 commited on
Commit
15d9d18
·
verified ·
1 Parent(s): c89468d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -16
app.py CHANGED
@@ -1,54 +1,96 @@
 
 
 
 
1
  from flask import Flask, request, jsonify
2
- import cv2, numpy as np, math
3
  from cvzone.HandTrackingModule import HandDetector
4
  from cvzone.ClassificationModule import Classifier
5
 
6
- app = Flask(__name__)
7
 
8
- detector = HandDetector(maxHands=1)
9
- classifier = Classifier("Model_old/keras_model.h5", "Model_old/labels.txt")
10
 
 
11
  IMG_SIZE = 300
12
  OFFSET = 20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  @app.route("/predict", methods=["POST"])
15
  def predict():
 
 
16
  if "frame" not in request.files:
17
  return jsonify({"gesture": None, "confidence": 0})
18
 
19
  file = request.files["frame"]
20
- img = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)
 
21
 
22
- hands, _ = detector.findHands(img)
 
23
 
24
  if not hands:
 
 
25
  return jsonify({"gesture": None, "confidence": 0})
26
 
27
  hand = hands[0]
28
  x, y, w, h = hand["bbox"]
29
 
30
  imgWhite = np.ones((IMG_SIZE, IMG_SIZE, 3), np.uint8) * 255
31
- imgCrop = img[max(0,y-OFFSET):y+h+OFFSET, max(0,x-OFFSET):x+w+OFFSET]
 
 
 
 
32
 
33
  aspectRatio = h / w
 
34
  if aspectRatio > 1:
35
  k = IMG_SIZE / h
36
  wCal = math.ceil(k * w)
37
  imgResize = cv2.resize(imgCrop, (wCal, IMG_SIZE))
38
- imgWhite[:, (IMG_SIZE-wCal)//2:(IMG_SIZE+wCal)//2] = imgResize
 
39
  else:
40
  k = IMG_SIZE / w
41
  hCal = math.ceil(k * h)
42
  imgResize = cv2.resize(imgCrop, (IMG_SIZE, hCal))
43
- imgWhite[(IMG_SIZE-hCal)//2:(IMG_SIZE+hCal)//2, :] = imgResize
 
44
 
45
  prediction, index = classifier.getPrediction(imgWhite, draw=False)
 
 
 
 
 
 
 
 
46
 
47
- return jsonify({
48
- "gesture": classifier.labels[index],
49
- "confidence": float(prediction[index])
50
- })
 
 
51
 
52
- # REQUIRED for Hugging Face Spaces
53
- if __name__ == "__main__":
54
- app.run(host="0.0.0.0", port=7860)
 
1
+ import cv2
2
+ import numpy as np
3
+ import math
4
+ import os
5
  from flask import Flask, request, jsonify
 
6
  from cvzone.HandTrackingModule import HandDetector
7
  from cvzone.ClassificationModule import Classifier
8
 
9
+ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
10
 
11
+ MODEL_PATH = "Model_old/keras_model.h5"
12
+ LABELS_PATH = "Model_old/labels.txt"
13
 
14
+ CONFIDENCE_THRESHOLD = 0.5
15
  IMG_SIZE = 300
16
  OFFSET = 20
17
+ STABILITY_THRESHOLD = 3
18
+
19
+ app = Flask(__name__)
20
+
21
+ print("Loading hand detector...")
22
+ detector = HandDetector(maxHands=1)
23
+
24
+ print("Loading classifier...")
25
+ classifier = Classifier(MODEL_PATH, LABELS_PATH)
26
+
27
+ with open(LABELS_PATH) as f:
28
+ labels = [l.strip() for l in f.readlines()]
29
+
30
+ last_gesture = None
31
+ stable_gesture = None
32
+ stable_count = 0
33
+
34
 
35
  @app.route("/predict", methods=["POST"])
36
  def predict():
37
+ global last_gesture, stable_gesture, stable_count
38
+
39
  if "frame" not in request.files:
40
  return jsonify({"gesture": None, "confidence": 0})
41
 
42
  file = request.files["frame"]
43
+ img_bytes = np.frombuffer(file.read(), np.uint8)
44
+ frame = cv2.imdecode(img_bytes, cv2.IMREAD_COLOR)
45
 
46
+ frame = cv2.flip(frame, 1)
47
+ hands, _ = detector.findHands(frame)
48
 
49
  if not hands:
50
+ stable_count = 0
51
+ stable_gesture = None
52
  return jsonify({"gesture": None, "confidence": 0})
53
 
54
  hand = hands[0]
55
  x, y, w, h = hand["bbox"]
56
 
57
  imgWhite = np.ones((IMG_SIZE, IMG_SIZE, 3), np.uint8) * 255
58
+ imgCrop = frame[max(0, y - OFFSET):y + h + OFFSET,
59
+ max(0, x - OFFSET):x + w + OFFSET]
60
+
61
+ if imgCrop.size == 0:
62
+ return jsonify({"gesture": None, "confidence": 0})
63
 
64
  aspectRatio = h / w
65
+
66
  if aspectRatio > 1:
67
  k = IMG_SIZE / h
68
  wCal = math.ceil(k * w)
69
  imgResize = cv2.resize(imgCrop, (wCal, IMG_SIZE))
70
+ wGap = math.ceil((IMG_SIZE - wCal) / 2)
71
+ imgWhite[:, wGap:wCal + wGap] = imgResize
72
  else:
73
  k = IMG_SIZE / w
74
  hCal = math.ceil(k * h)
75
  imgResize = cv2.resize(imgCrop, (IMG_SIZE, hCal))
76
+ hGap = math.ceil((IMG_SIZE - hCal) / 2)
77
+ imgWhite[hGap:hCal + hGap, :] = imgResize
78
 
79
  prediction, index = classifier.getPrediction(imgWhite, draw=False)
80
+ confidence = float(prediction[index])
81
+ gesture = labels[index]
82
+
83
+ if gesture == stable_gesture:
84
+ stable_count += 1
85
+ else:
86
+ stable_gesture = gesture
87
+ stable_count = 1
88
 
89
+ if stable_count >= STABILITY_THRESHOLD and confidence >= CONFIDENCE_THRESHOLD:
90
+ last_gesture = gesture
91
+ return jsonify({
92
+ "gesture": gesture,
93
+ "confidence": confidence
94
+ })
95
 
96
+ return jsonify({"gesture": None, "confidence": 0})