-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
156 lines (107 loc) · 4.02 KB
/
app.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from AI.SpeedTesting import mainLoop, getCumulativeArray
from HandAI.main import mainHandLoop
from APIStuff.OpenAPI.initialPrompt import main, secondaryPromptMain
from flask import Flask, request
from flask_cors import CORS
from io import BytesIO
import json
import base64
import numpy as np
#something
app = Flask(__name__)
CORS(app)
import base64
from PIL import Image
from io import BytesIO
def webp_to_jpg(webp_base64_string, output_file_path='output.jpg'):
# Remove the "data:image/webp;base64," prefix
webp_base64_string = webp_base64_string.replace("data:image/webp;base64,", "")
# Decode base64 string to binary data
binary_data = base64.b64decode(webp_base64_string)
# Open WebP image using Pillow
with Image.open(BytesIO(binary_data)) as webp_image:
# Save as JPG
webp_image.convert('RGB').save(output_file_path, format='JPEG')
# Example usage
@app.route("/", methods=["POST","GET"])
def index():
if request.method == "GET":
return "hello world"
data = request.json["imageData"]
image = Image.open(BytesIO(data))
numpy_array = np.array(image)
webp_to_jpg(numpy_array)
return {"1":"hello world"}
@app.route("/api/frame", methods=["POST"])
def imageProcess():
base64_string = request.json["imageData"]
_, data = base64_string.split(',', 1)
# Decode the base64 data
decoded_data = base64.b64decode(data)
# Create a PIL Image
image = Image.open(BytesIO(decoded_data))
with BytesIO() as jpeg_buffer:
image.save(jpeg_buffer, format="JPEG")
jpeg_buffer.seek(0)
jpeg_image = Image.open(jpeg_buffer)
# Now convert the JPEG image to a NumPy array
numpy_array = np.array(jpeg_image)
# Here, numpy_array is ready and you can use it with mainLoop or any other function
newImg = list(map(str, mainLoop(numpy_array)))
img_str = json.dumps(newImg)
# Send as JSON
return img_str
@app.route("/api/emotionsArray", methods=["POST"])
def getEmotionsArray():
array = getCumulativeArray()
array = main()
newImg = list(map(str, array))
img_str = json.dumps(newImg)
return img_str
@app.route("/api/promt", methods=["POST"])
def getGPTOutput():
# Get array from json request - might not work ahahahh loser
jsonResponse = request.json()
# Parse the JSON data
parsedData = json.loads(jsonResponse)
# Access the array
dataArray = parsedData['data']
prompt = main(dataArray)
# newImg = list(map(str, array))
# img_str = json.dumps(newImg)
return img_str
@app.route("/api/frame", methods=["POST"])
def handGestureGen(): # Pass in webcam image
base64_string = request.json["imageData"]
_, data = base64_string.split(',', 1)
# Decode the base64 data
decoded_data = base64.b64decode(data)
# Create a PIL Image
image = Image.open(BytesIO(decoded_data))
with BytesIO() as jpeg_buffer:
image.save(jpeg_buffer, format="JPEG")
jpeg_buffer.seek(0)
jpeg_image = Image.open(jpeg_buffer)
# Now convert the JPEG image to a NumPy array
numpy_array = np.array(jpeg_image)
# Here, numpy_array is ready and you can use it with mainLoop or any other function
numpy_array = getCumulativeArray()
topGesturesArray = list(map(str, mainHandLoop(numpy_array)))
indexOfMax = np.argmax(topGesturesArray)
maxGesture = topGesturesArray.max()
arraySum = topGesturesArray.sum()
if (maxGesture / arraySum) > 0.3: #Threshold val, where lower is less strict
if indexOfMax == 0: #If thumb up
output = secondaryPromptMain()
else if indexOfMax == 1: # Thumb down
# restart whole process?, or just regenerate prompt? - would be tough because don't know original emotions.
else if indexOfMax == 2: # Palm out
# Close the webcam
else: # None
# carry on until gesture found
else:
return
# Send as JSON
# return img_str
if __name__ == "__main__":
app.run(port=2223)