-
Notifications
You must be signed in to change notification settings - Fork 1
/
prediction.py
109 lines (86 loc) · 5.85 KB
/
prediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import pickle
import cv2
import mediapipe as mp
import numpy as np
from PIL import Image
import requests
from io import BytesIO
model_dict = pickle.load(open('./models/stacked_model_new.p', 'rb'))
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']
model = model_dict['model']
# get url from backend
# def prediction(url):
# response = requests.get(url)
# print(response)
# img = Image.open(BytesIO(response.content))
# img.save('image.jpg')
# #cap = cv2.VideoCapture(0)
def prediction():
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
hands = mp_hands.Hands(static_image_mode=False, min_detection_confidence=0.3)
hands.maxHands = 1
data_aux = []
x_ = []
y_ = []
frame = cv2.imread('image.jpg')
H,W, _ = frame.shape
#print(response.content)
#H, W, _ = frame.shape
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_rgb)
if results.multi_hand_landmarks:
if(len(results.multi_hand_landmarks) == 1):
for hand_landmarks in results.multi_hand_landmarks:
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
x_.append(x)
y_.append(y)
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
data_aux.append(x - min(x_))
data_aux.append(y - min(y_))
x1 = int(min(x_) * W) - 10
y1 = int(min(y_) * H) - 10
x2 = int(max(x_) * W) - 10
y2 = int(max(y_) * H) - 10
if(len(data_aux) == 42):
prediction = model.predict([np.asarray(data_aux)])
predicted_character = labels[prediction[0]]
return predicted_character
# cv2.putText(frame, predicted_character, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 0, 0), 3,
# cv2.LINE_AA)
else:
return "Too many Hands"
# url = '''
# data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxASEhMSEhESEhMWExISEhERFxUTFRUSFRUWFxUSGBYYICkgGCYlHRMVITEhJSkrLi4uGB8zODMtNygtLisBCgo
# KDg0OFxAQGS0iHyUtKysrKystLSstLS0uLS0tLSstLS0tKy0tLSstLTctLSstLS0tKy0tLS0tKy0tLTc3Lf/AABEIALcBEwMBIgACEQEDEQH/xAAbAAEAAQUBAAAAAAAAAAAAAAAABAIDBQYHAf
# /EAD8QAAIBAgMEBwUFBAsAAAAAAAABAgMRBCExBRJBUQYTImFxgZEyQlKhsWJywdHhM0OSogcUFSRTY3OCssLw/8QAGQEBAAMBAQAAAAAAAAAAAAAAAAECAwQF/8QAIREBAQACAQQDAQ
# EAAAAAAAAAAAECEQMhMUFRBBITYRT/2gAMAwEAAhEDEQA/AOyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
# AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADw9NKxVOuqkqlKe7VhJqSekrPO64pmybL2xTrJK6hUt2qTeaa1t8S718imOe2ufFcevdkQAXZAAAAAA
# AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC1iK8ILenJRXN/wDsyDS6QYSTssRTvyk93/lYidLsDXq0r0O1JJ3hknJO2cW8r5aHJMVTq05NVYTpu+amnF/Mxz5Msb2dfD8fDkx3vq7tCpF6ST8GmV
# nCaGNnFZSf6G29HNtS6jtVJ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AAAAAAAAAAAAAAAAAAAxG0ujuGrXbh1c379O0Xfm1o/
# NGXAs2mWzs0fF9HMVSzptV493Zn/C9fJsx1Pabi3GacZLWMk4teKeZ0kjY3A0qy3atOM1w3lmvB6ryM7h6aTl9tOpbRT4olQxFy9jehsNaFRwfwVO1HwT1XncwuJwGLoftKUnFe/T7cfHLNeaRS
# 42NJljWajWKusMFh9oJ6Mn0aqfEhZkOsZ4WlUAQ2wAHQ5QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGNx+w8NWznTSl8cOxL1WvncweJ6LVoZ0aiqL4KnZl5SWT87G3ArcZVpnY0KUcT
# HJ4etda2g5L1WTPDfgV/Nf8AX+AANGQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/9k='''
# with open("url.txt", "rb") as url_file:
# url =
# url= "https://img.freepik.com/free-photo/reaching-female-hand_628469-419.jpg"
# print(prediction(url))