Spaces:
Runtime error
Runtime error
Commit
·
ac809d0
1
Parent(s):
1e70190
Uploaded model and main file
Browse files- .gitignore +1 -0
- app.py +76 -0
- requirements.txt +42 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
*.p
|
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import cv2
|
3 |
+
import mediapipe as mp
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
import requests
|
7 |
+
from io import BytesIO
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
model_dict = pickle.load(open('stacked_model_new.p', 'rb'))
|
11 |
+
|
12 |
+
labels = ['A','B','C','D','E','F','G','H','I','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y']
|
13 |
+
|
14 |
+
model = model_dict['model']
|
15 |
+
|
16 |
+
|
17 |
+
# get url from backend
|
18 |
+
|
19 |
+
def predict(url):
|
20 |
+
response = requests.get(url)
|
21 |
+
print(response)
|
22 |
+
img = Image.open(BytesIO(response.content))
|
23 |
+
img.save('image.jpg')
|
24 |
+
mp_hands = mp.solutions.hands
|
25 |
+
mp_drawing = mp.solutions.drawing_utils
|
26 |
+
mp_drawing_styles = mp.solutions.drawing_styles
|
27 |
+
|
28 |
+
hands = mp_hands.Hands(static_image_mode=False, min_detection_confidence=0.3)
|
29 |
+
hands.maxHands = 1
|
30 |
+
|
31 |
+
data_aux = []
|
32 |
+
x_ = []
|
33 |
+
y_ = []
|
34 |
+
|
35 |
+
frame = cv2.imread('image.jpg')
|
36 |
+
|
37 |
+
H,W, _ = frame.shape
|
38 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
39 |
+
|
40 |
+
results = hands.process(frame_rgb)
|
41 |
+
if results.multi_hand_landmarks:
|
42 |
+
if(len(results.multi_hand_landmarks) == 1):
|
43 |
+
|
44 |
+
for hand_landmarks in results.multi_hand_landmarks:
|
45 |
+
for i in range(len(hand_landmarks.landmark)):
|
46 |
+
x = hand_landmarks.landmark[i].x
|
47 |
+
y = hand_landmarks.landmark[i].y
|
48 |
+
|
49 |
+
x_.append(x)
|
50 |
+
y_.append(y)
|
51 |
+
|
52 |
+
for i in range(len(hand_landmarks.landmark)):
|
53 |
+
x = hand_landmarks.landmark[i].x
|
54 |
+
y = hand_landmarks.landmark[i].y
|
55 |
+
data_aux.append(x - min(x_))
|
56 |
+
data_aux.append(y - min(y_))
|
57 |
+
|
58 |
+
x1 = int(min(x_) * W) - 10
|
59 |
+
y1 = int(min(y_) * H) - 10
|
60 |
+
|
61 |
+
x2 = int(max(x_) * W) - 10
|
62 |
+
y2 = int(max(y_) * H) - 10
|
63 |
+
|
64 |
+
if(len(data_aux) == 42):
|
65 |
+
prediction = model.predict([np.asarray(data_aux)])
|
66 |
+
|
67 |
+
predicted_character = labels[prediction[0]]
|
68 |
+
|
69 |
+
return {"prediction":predicted_character}
|
70 |
+
else:
|
71 |
+
|
72 |
+
return {"prediction": "Too many Hands"}
|
73 |
+
|
74 |
+
|
75 |
+
iface = gr.Interface(fn=predict, inputs="image", outputs="text", title="Image to Text Model")
|
76 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
absl-py==2.1.0
|
2 |
+
attrs==23.2.0
|
3 |
+
blinker==1.7.0
|
4 |
+
certifi==2024.2.2
|
5 |
+
cffi==1.16.0
|
6 |
+
charset-normalizer==3.3.2
|
7 |
+
click==8.1.7
|
8 |
+
colorama==0.4.6
|
9 |
+
contourpy==1.2.1
|
10 |
+
cycler==0.12.1
|
11 |
+
Flask==3.0.3
|
12 |
+
flatbuffers==24.3.25
|
13 |
+
fonttools==4.51.0
|
14 |
+
idna==3.7
|
15 |
+
itsdangerous==2.2.0
|
16 |
+
jax==0.4.26
|
17 |
+
Jinja2==3.1.3
|
18 |
+
joblib==1.4.0
|
19 |
+
kiwisolver==1.4.5
|
20 |
+
MarkupSafe==2.1.5
|
21 |
+
matplotlib==3.8.4
|
22 |
+
mediapipe==0.10.11
|
23 |
+
ml-dtypes==0.4.0
|
24 |
+
numpy==1.26.4
|
25 |
+
opencv-contrib-python==4.9.0.80
|
26 |
+
opencv-python==4.9.0.80
|
27 |
+
opt-einsum==3.3.0
|
28 |
+
packaging==24.0
|
29 |
+
pillow==10.3.0
|
30 |
+
protobuf==3.20.3
|
31 |
+
pycparser==2.22
|
32 |
+
pyparsing==3.1.2
|
33 |
+
python-dateutil==2.9.0.post0
|
34 |
+
requests==2.31.0
|
35 |
+
scikit-learn==1.2.2
|
36 |
+
scipy==1.13.0
|
37 |
+
six==1.16.0
|
38 |
+
sounddevice==0.4.6
|
39 |
+
threadpoolctl==3.4.0
|
40 |
+
urllib3==2.2.1
|
41 |
+
Werkzeug==3.0.2
|
42 |
+
xgboost==2.0.3
|