Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,34 @@
|
|
| 1 |
-
# Install required dependencies
|
| 2 |
-
!pip install -q mediapipe tensorflow opencv-python-headless gradio Pillow numpy
|
| 3 |
-
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import tensorflow as tf
|
| 7 |
import cv2
|
|
@@ -18,7 +45,7 @@ class handTracker():
|
|
| 18 |
self.modelComplexity = modelComplexity
|
| 19 |
self.detectionConfidence = detectionConfidence
|
| 20 |
self.trackConfidence = trackConfidence
|
| 21 |
-
|
| 22 |
self.mpHands = mp.solutions.hands
|
| 23 |
self.hands = self.mpHands.Hands(
|
| 24 |
static_image_mode=self.mode,
|
|
@@ -26,51 +53,51 @@ class handTracker():
|
|
| 26 |
model_complexity=self.modelComplexity,
|
| 27 |
min_detection_confidence=self.detectionConfidence,
|
| 28 |
min_tracking_confidence=self.trackConfidence)
|
| 29 |
-
|
| 30 |
self.mpDraw = mp.solutions.drawing_utils
|
| 31 |
self.mpDrawStyles = mp.solutions.drawing_styles
|
| 32 |
-
|
| 33 |
def findAndDrawHands(self, frame):
|
| 34 |
RGBimage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 35 |
self.results = self.hands.process(RGBimage)
|
| 36 |
-
|
| 37 |
if self.results.multi_hand_landmarks:
|
| 38 |
for handLms in self.results.multi_hand_landmarks:
|
| 39 |
self.mpDraw.draw_landmarks(
|
| 40 |
-
frame,
|
| 41 |
handLms,
|
| 42 |
self.mpHands.HAND_CONNECTIONS,
|
| 43 |
self.mpDrawStyles.get_default_hand_landmarks_style(),
|
| 44 |
self.mpDrawStyles.get_default_hand_connections_style())
|
| 45 |
return frame
|
| 46 |
-
|
| 47 |
def findLandmarks(self, frame, handNo=0):
|
| 48 |
landmarkList = []
|
| 49 |
x_list = []
|
| 50 |
y_list = []
|
| 51 |
bbox = []
|
| 52 |
-
|
| 53 |
if self.results.multi_hand_landmarks:
|
| 54 |
if handNo < len(self.results.multi_hand_landmarks):
|
| 55 |
myHand = self.results.multi_hand_landmarks[handNo]
|
| 56 |
-
|
| 57 |
for id, lm in enumerate(myHand.landmark):
|
| 58 |
h, w, c = frame.shape
|
| 59 |
cx, cy = int(lm.x * w), int(lm.y * h)
|
| 60 |
x_list.append(cx)
|
| 61 |
y_list.append(cy)
|
| 62 |
landmarkList.append([id, cx, cy])
|
| 63 |
-
|
| 64 |
if x_list and y_list:
|
| 65 |
xmin, xmax = min(x_list), max(x_list)
|
| 66 |
ymin, ymax = min(y_list), max(y_list)
|
| 67 |
-
|
| 68 |
padding = 20
|
| 69 |
xmin = max(0, xmin - padding)
|
| 70 |
ymin = max(0, ymin - padding)
|
| 71 |
boxW = min(w - xmin, xmax - xmin + 2*padding)
|
| 72 |
boxH = min(h - ymin, ymax - ymin + 2*padding)
|
| 73 |
-
|
| 74 |
if boxW > boxH:
|
| 75 |
diff = boxW - boxH
|
| 76 |
ymin = max(0, ymin - diff//2)
|
|
@@ -79,7 +106,7 @@ class handTracker():
|
|
| 79 |
diff = boxH - boxW
|
| 80 |
xmin = max(0, xmin - diff//2)
|
| 81 |
boxW = min(w - xmin, boxH)
|
| 82 |
-
|
| 83 |
bbox = [xmin, ymin, boxW, boxH]
|
| 84 |
return landmarkList, bbox
|
| 85 |
|
|
@@ -97,10 +124,10 @@ def load_model_with_compatibility(model_path):
|
|
| 97 |
if 'groups' in kwargs:
|
| 98 |
del kwargs['groups']
|
| 99 |
super(CustomDepthwiseConv2D, self).__init__(**kwargs)
|
| 100 |
-
|
| 101 |
custom_objects = {'DepthwiseConv2D': CustomDepthwiseConv2D}
|
| 102 |
model = tf.keras.models.load_model(
|
| 103 |
-
model_path,
|
| 104 |
custom_objects=custom_objects,
|
| 105 |
compile=False
|
| 106 |
)
|
|
@@ -114,7 +141,7 @@ def create_simple_asl_model():
|
|
| 114 |
labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
|
| 115 |
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
|
| 116 |
'T', 'U', 'V', 'W', 'X', 'Y']
|
| 117 |
-
|
| 118 |
print("Creating a new compatible model...")
|
| 119 |
model = tf.keras.Sequential([
|
| 120 |
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),
|
|
@@ -128,7 +155,7 @@ def create_simple_asl_model():
|
|
| 128 |
tf.keras.layers.Dense(len(labels), activation='softmax')
|
| 129 |
])
|
| 130 |
model.compile(optimizer='adam',
|
| 131 |
-
loss='sparse_categorical_crossentropy',
|
| 132 |
metrics=['accuracy'])
|
| 133 |
return model
|
| 134 |
|
|
@@ -145,7 +172,7 @@ def preprocess_hand_roi(hand_roi, target_shape):
|
|
| 145 |
hand_roi_rgb = cv2.cvtColor(hand_roi, cv2.COLOR_GRAY2RGB)
|
| 146 |
else:
|
| 147 |
hand_roi_rgb = hand_roi.copy()
|
| 148 |
-
|
| 149 |
resized = cv2.resize(hand_roi_rgb, (target_shape[0], target_shape[1]))
|
| 150 |
normalized = resized.astype('float32') / 255.0
|
| 151 |
else:
|
|
@@ -153,12 +180,12 @@ def preprocess_hand_roi(hand_roi, target_shape):
|
|
| 153 |
hand_roi_gray = cv2.cvtColor(hand_roi, cv2.COLOR_BGR2GRAY)
|
| 154 |
else:
|
| 155 |
hand_roi_gray = hand_roi
|
| 156 |
-
|
| 157 |
resized = cv2.resize(hand_roi_gray, (target_shape[0], target_shape[1]))
|
| 158 |
normalized = resized.astype('float32') / 255.0
|
| 159 |
if len(normalized.shape) == 2:
|
| 160 |
normalized = normalized[..., np.newaxis]
|
| 161 |
-
|
| 162 |
return np.expand_dims(normalized, axis=0), resized
|
| 163 |
|
| 164 |
def process_image(input_image):
|
|
@@ -166,16 +193,16 @@ def process_image(input_image):
|
|
| 166 |
tracker = handTracker(detectionConfidence=0.7)
|
| 167 |
frame_with_hands = tracker.findAndDrawHands(frame.copy())
|
| 168 |
landmarks, bbox = tracker.findLandmarks(frame)
|
| 169 |
-
|
| 170 |
if not bbox:
|
| 171 |
return "No hand detected", None
|
| 172 |
-
|
| 173 |
x, y, w, h = bbox
|
| 174 |
hand_roi = frame[y:y+h, x:x+w]
|
| 175 |
cv2.rectangle(frame_with_hands, (x, y), (x+w, y+h), (0, 255, 0), 2)
|
| 176 |
-
|
| 177 |
model_input, _ = preprocess_hand_roi(hand_roi, model_input_shape)
|
| 178 |
-
|
| 179 |
try:
|
| 180 |
prediction = model.predict(model_input, verbose=0)[0]
|
| 181 |
predicted_class = np.argmax(prediction)
|
|
@@ -183,11 +210,11 @@ def process_image(input_image):
|
|
| 183 |
letter = labels[predicted_class] if predicted_class < len(labels) else "Unknown"
|
| 184 |
except:
|
| 185 |
return "Prediction error", None
|
| 186 |
-
|
| 187 |
result_text = f"Prediction: {letter} (Confidence: {confidence:.2f})"
|
| 188 |
-
cv2.putText(frame_with_hands, result_text, (10, 30),
|
| 189 |
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
|
| 190 |
-
|
| 191 |
output_image = cv2.cvtColor(frame_with_hands, cv2.COLOR_BGR2RGB)
|
| 192 |
return result_text, Image.fromarray(output_image)
|
| 193 |
|
|
@@ -204,4 +231,4 @@ interface = gr.Interface(
|
|
| 204 |
)
|
| 205 |
|
| 206 |
if __name__ == "__main__":
|
| 207 |
-
interface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import sys
|
| 4 |
+
import pkg_resources
|
| 5 |
+
import warnings
|
| 6 |
+
warnings.filterwarnings("ignore")
|
| 7 |
+
|
| 8 |
+
def install_package(package, version=None):
|
| 9 |
+
package_spec = f"{package}=={version}" if version else package
|
| 10 |
+
print(f"Installing {package_spec}...")
|
| 11 |
+
try:
|
| 12 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-cache-dir", package_spec])
|
| 13 |
+
except subprocess.CalledProcessError as e:
|
| 14 |
+
print(f"Failed to install {package_spec}: {e}")
|
| 15 |
+
raise
|
| 16 |
+
|
| 17 |
+
# Required packages
|
| 18 |
+
required_packages = {
|
| 19 |
+
"mediapipe": None,
|
| 20 |
+
"tensorflow": None,
|
| 21 |
+
"opencv-python-headless": None,
|
| 22 |
+
"gradio": None,
|
| 23 |
+
"Pillow": None,
|
| 24 |
+
"numpy": None
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
installed_packages = {pkg.key for pkg in pkg_resources.working_set}
|
| 28 |
+
for package, version in required_packages.items():
|
| 29 |
+
if package not in installed_packages:
|
| 30 |
+
install_package(package, version)
|
| 31 |
+
|
| 32 |
import numpy as np
|
| 33 |
import tensorflow as tf
|
| 34 |
import cv2
|
|
|
|
| 45 |
self.modelComplexity = modelComplexity
|
| 46 |
self.detectionConfidence = detectionConfidence
|
| 47 |
self.trackConfidence = trackConfidence
|
| 48 |
+
|
| 49 |
self.mpHands = mp.solutions.hands
|
| 50 |
self.hands = self.mpHands.Hands(
|
| 51 |
static_image_mode=self.mode,
|
|
|
|
| 53 |
model_complexity=self.modelComplexity,
|
| 54 |
min_detection_confidence=self.detectionConfidence,
|
| 55 |
min_tracking_confidence=self.trackConfidence)
|
| 56 |
+
|
| 57 |
self.mpDraw = mp.solutions.drawing_utils
|
| 58 |
self.mpDrawStyles = mp.solutions.drawing_styles
|
| 59 |
+
|
| 60 |
def findAndDrawHands(self, frame):
|
| 61 |
RGBimage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 62 |
self.results = self.hands.process(RGBimage)
|
| 63 |
+
|
| 64 |
if self.results.multi_hand_landmarks:
|
| 65 |
for handLms in self.results.multi_hand_landmarks:
|
| 66 |
self.mpDraw.draw_landmarks(
|
| 67 |
+
frame,
|
| 68 |
handLms,
|
| 69 |
self.mpHands.HAND_CONNECTIONS,
|
| 70 |
self.mpDrawStyles.get_default_hand_landmarks_style(),
|
| 71 |
self.mpDrawStyles.get_default_hand_connections_style())
|
| 72 |
return frame
|
| 73 |
+
|
| 74 |
def findLandmarks(self, frame, handNo=0):
|
| 75 |
landmarkList = []
|
| 76 |
x_list = []
|
| 77 |
y_list = []
|
| 78 |
bbox = []
|
| 79 |
+
|
| 80 |
if self.results.multi_hand_landmarks:
|
| 81 |
if handNo < len(self.results.multi_hand_landmarks):
|
| 82 |
myHand = self.results.multi_hand_landmarks[handNo]
|
| 83 |
+
|
| 84 |
for id, lm in enumerate(myHand.landmark):
|
| 85 |
h, w, c = frame.shape
|
| 86 |
cx, cy = int(lm.x * w), int(lm.y * h)
|
| 87 |
x_list.append(cx)
|
| 88 |
y_list.append(cy)
|
| 89 |
landmarkList.append([id, cx, cy])
|
| 90 |
+
|
| 91 |
if x_list and y_list:
|
| 92 |
xmin, xmax = min(x_list), max(x_list)
|
| 93 |
ymin, ymax = min(y_list), max(y_list)
|
| 94 |
+
|
| 95 |
padding = 20
|
| 96 |
xmin = max(0, xmin - padding)
|
| 97 |
ymin = max(0, ymin - padding)
|
| 98 |
boxW = min(w - xmin, xmax - xmin + 2*padding)
|
| 99 |
boxH = min(h - ymin, ymax - ymin + 2*padding)
|
| 100 |
+
|
| 101 |
if boxW > boxH:
|
| 102 |
diff = boxW - boxH
|
| 103 |
ymin = max(0, ymin - diff//2)
|
|
|
|
| 106 |
diff = boxH - boxW
|
| 107 |
xmin = max(0, xmin - diff//2)
|
| 108 |
boxW = min(w - xmin, boxH)
|
| 109 |
+
|
| 110 |
bbox = [xmin, ymin, boxW, boxH]
|
| 111 |
return landmarkList, bbox
|
| 112 |
|
|
|
|
| 124 |
if 'groups' in kwargs:
|
| 125 |
del kwargs['groups']
|
| 126 |
super(CustomDepthwiseConv2D, self).__init__(**kwargs)
|
| 127 |
+
|
| 128 |
custom_objects = {'DepthwiseConv2D': CustomDepthwiseConv2D}
|
| 129 |
model = tf.keras.models.load_model(
|
| 130 |
+
model_path,
|
| 131 |
custom_objects=custom_objects,
|
| 132 |
compile=False
|
| 133 |
)
|
|
|
|
| 141 |
labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
|
| 142 |
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
|
| 143 |
'T', 'U', 'V', 'W', 'X', 'Y']
|
| 144 |
+
|
| 145 |
print("Creating a new compatible model...")
|
| 146 |
model = tf.keras.Sequential([
|
| 147 |
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),
|
|
|
|
| 155 |
tf.keras.layers.Dense(len(labels), activation='softmax')
|
| 156 |
])
|
| 157 |
model.compile(optimizer='adam',
|
| 158 |
+
loss='sparse_categorical_crossentropy',
|
| 159 |
metrics=['accuracy'])
|
| 160 |
return model
|
| 161 |
|
|
|
|
| 172 |
hand_roi_rgb = cv2.cvtColor(hand_roi, cv2.COLOR_GRAY2RGB)
|
| 173 |
else:
|
| 174 |
hand_roi_rgb = hand_roi.copy()
|
| 175 |
+
|
| 176 |
resized = cv2.resize(hand_roi_rgb, (target_shape[0], target_shape[1]))
|
| 177 |
normalized = resized.astype('float32') / 255.0
|
| 178 |
else:
|
|
|
|
| 180 |
hand_roi_gray = cv2.cvtColor(hand_roi, cv2.COLOR_BGR2GRAY)
|
| 181 |
else:
|
| 182 |
hand_roi_gray = hand_roi
|
| 183 |
+
|
| 184 |
resized = cv2.resize(hand_roi_gray, (target_shape[0], target_shape[1]))
|
| 185 |
normalized = resized.astype('float32') / 255.0
|
| 186 |
if len(normalized.shape) == 2:
|
| 187 |
normalized = normalized[..., np.newaxis]
|
| 188 |
+
|
| 189 |
return np.expand_dims(normalized, axis=0), resized
|
| 190 |
|
| 191 |
def process_image(input_image):
|
|
|
|
| 193 |
tracker = handTracker(detectionConfidence=0.7)
|
| 194 |
frame_with_hands = tracker.findAndDrawHands(frame.copy())
|
| 195 |
landmarks, bbox = tracker.findLandmarks(frame)
|
| 196 |
+
|
| 197 |
if not bbox:
|
| 198 |
return "No hand detected", None
|
| 199 |
+
|
| 200 |
x, y, w, h = bbox
|
| 201 |
hand_roi = frame[y:y+h, x:x+w]
|
| 202 |
cv2.rectangle(frame_with_hands, (x, y), (x+w, y+h), (0, 255, 0), 2)
|
| 203 |
+
|
| 204 |
model_input, _ = preprocess_hand_roi(hand_roi, model_input_shape)
|
| 205 |
+
|
| 206 |
try:
|
| 207 |
prediction = model.predict(model_input, verbose=0)[0]
|
| 208 |
predicted_class = np.argmax(prediction)
|
|
|
|
| 210 |
letter = labels[predicted_class] if predicted_class < len(labels) else "Unknown"
|
| 211 |
except:
|
| 212 |
return "Prediction error", None
|
| 213 |
+
|
| 214 |
result_text = f"Prediction: {letter} (Confidence: {confidence:.2f})"
|
| 215 |
+
cv2.putText(frame_with_hands, result_text, (10, 30),
|
| 216 |
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
|
| 217 |
+
|
| 218 |
output_image = cv2.cvtColor(frame_with_hands, cv2.COLOR_BGR2RGB)
|
| 219 |
return result_text, Image.fromarray(output_image)
|
| 220 |
|
|
|
|
| 231 |
)
|
| 232 |
|
| 233 |
if __name__ == "__main__":
|
| 234 |
+
interface.launch(share=True)
|