Paulie-Aditya commited on
Commit
6baec95
·
verified ·
1 Parent(s): 8cfacba

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. prediction.py +76 -0
prediction.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()