Spaces:
Runtime error
Runtime error
HuggingFace-SK
commited on
Commit
·
74a2dda
1
Parent(s):
283fc23
use flask api instead of socket io
Browse files- __pycache__/app.cpython-38.pyc +0 -0
- app.py +20 -98
- templates/index.html +29 -4
__pycache__/app.cpython-38.pyc
ADDED
|
Binary file (8.02 kB). View file
|
|
|
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import cv2
|
|
| 2 |
import base64
|
| 3 |
import numpy as np
|
| 4 |
import io
|
| 5 |
-
from flask import Flask, render_template, Response
|
| 6 |
from flask_socketio import SocketIO, emit
|
| 7 |
from PIL import Image
|
| 8 |
from time import time as unix_time
|
|
@@ -201,16 +201,17 @@ video_frames=[]
|
|
| 201 |
|
| 202 |
|
| 203 |
app = Flask(__name__)
|
| 204 |
-
|
| 205 |
|
| 206 |
@app.route('/')
|
| 207 |
def index():
|
| 208 |
return render_template('index.html')
|
| 209 |
|
| 210 |
|
| 211 |
-
@
|
| 212 |
-
def handle_video_frame(
|
| 213 |
-
|
|
|
|
| 214 |
response_frame = data_uri_to_image(frame)
|
| 215 |
decimg = response_frame
|
| 216 |
|
|
@@ -282,102 +283,13 @@ def handle_video_frame(frame):
|
|
| 282 |
letter_result='_'
|
| 283 |
else:
|
| 284 |
letter_result='>'
|
| 285 |
-
|
| 286 |
-
same_letter_time = round((unix_time()) - frame_time, 2)
|
| 287 |
-
|
| 288 |
-
#print(frame_time, same_letter_time)
|
| 289 |
-
|
| 290 |
-
if old_letter_result != letter_result:
|
| 291 |
-
frame_time = (unix_time())# Log Time
|
| 292 |
-
same_letter_time=0
|
| 293 |
-
|
| 294 |
-
old_letter_result = letter_result
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
else:
|
| 303 |
-
local_same_letter_time=0
|
| 304 |
-
if(no_hand_flag==0):
|
| 305 |
-
same_letter_time = round((unix_time()) - frame_time, 2)
|
| 306 |
-
|
| 307 |
-
local_same_letter_time = round((unix_time()) - frame_time, 2)
|
| 308 |
-
|
| 309 |
-
letterscore = 0
|
| 310 |
-
#print(brightness(final_image))
|
| 311 |
-
|
| 312 |
-
if local_same_letter_time>1.2 and brightness(final_image) < 40:
|
| 313 |
-
char_list.pop()
|
| 314 |
-
print (string.join(char_list))
|
| 315 |
-
frame_time = (unix_time())# Log Time
|
| 316 |
-
same_letter_time=0
|
| 317 |
-
|
| 318 |
-
if same_letter_time>1.4 and brightness(final_image) > 40:
|
| 319 |
-
frame_time = (unix_time())# Log Time
|
| 320 |
-
same_letter_time=0
|
| 321 |
-
no_hand_flag=1
|
| 322 |
-
|
| 323 |
-
same_letter_time_width = 10 + int(same_letter_time*100)
|
| 324 |
-
if same_letter_time_width > 190:
|
| 325 |
-
same_letter_time_width = 190
|
| 326 |
-
iheight, iwidth = final_image.shape[:2]
|
| 327 |
-
cv2.rectangle(final_image, (0, 0), (iwidth, 50), (255,255,255), -1)
|
| 328 |
-
|
| 329 |
-
cv2.rectangle(final_image, (0, 50), (200, 100), (255,255,255), -1)
|
| 330 |
-
cv2.rectangle(final_image, (8, 58), (192, 72), (255,100,100), -1)
|
| 331 |
-
cv2.rectangle(final_image, (10, 60), (190, 70), (255,200,200), -1)
|
| 332 |
-
cv2.rectangle(final_image, (10, 60), (same_letter_time_width, 70), (255,100,100), -1)
|
| 333 |
-
|
| 334 |
-
letterscore_width = 10+ int(letterscore*100)
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
cv2.rectangle(final_image, (8, 78), (192, 92), (100,100,200), -1)
|
| 338 |
-
cv2.rectangle(final_image, (10, 80), (190, 90), (150,175,255), -1)
|
| 339 |
-
cv2.rectangle(final_image, (10, 80), (letterscore_width, 90), (100,100,200), -1)
|
| 340 |
-
|
| 341 |
-
cv2.putText(final_image, f"{(letterscore_width)}", # Display result
|
| 342 |
-
(12, 70), cv2.FONT_HERSHEY_DUPLEX,
|
| 343 |
-
0.5, (0, 0, 0), 1, cv2.LINE_AA)
|
| 344 |
-
|
| 345 |
-
cv2.putText(final_image, f"[ {letter_result} ] {''.join(char_list)} |", # Display result
|
| 346 |
-
(20, 40), cv2.FONT_HERSHEY_DUPLEX,
|
| 347 |
-
0.7, (0, 0, 0), 2, cv2.LINE_AA)
|
| 348 |
-
|
| 349 |
-
print(letter_result, same_letter_time)
|
| 350 |
-
|
| 351 |
except Exception as e:
|
| 352 |
# Ha! The catch err{throw err} scenario, it was actually quite useful in debugging though
|
| 353 |
print(e)
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
if same_letter_time > 1.7 and RESULT.handedness[0][0].display_name == 'Right': # Right hand
|
| 359 |
-
char_list.append(letter_result)
|
| 360 |
-
string = ""
|
| 361 |
-
print (string.join(char_list))
|
| 362 |
-
frame_time = (unix_time())# Log Time
|
| 363 |
-
same_letter_time=0
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
if same_letter_time > 0.9 and RESULT.handedness[0][0].display_name == 'Left':
|
| 367 |
-
if same_letter_time > 1.2 and letter_result=='_':
|
| 368 |
-
char_list.append(" ")
|
| 369 |
-
string = ""
|
| 370 |
-
print (string.join(char_list))
|
| 371 |
-
frame_time = (unix_time())# Log Time
|
| 372 |
-
same_letter_time=0
|
| 373 |
-
|
| 374 |
-
if letter_result == ">":
|
| 375 |
-
string=""
|
| 376 |
-
if(string.join(char_list) != ''):
|
| 377 |
-
os.system(f'echo {string.join(char_list)} | espeak -p 70 -s 140')
|
| 378 |
-
char_list=[]
|
| 379 |
-
print (string.join(char_list))
|
| 380 |
-
|
| 381 |
|
| 382 |
def data_uri_to_image(data_uri):
|
| 383 |
header, encoded = data_uri.split(',', 1)
|
|
@@ -386,5 +298,15 @@ def data_uri_to_image(data_uri):
|
|
| 386 |
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 387 |
return image
|
| 388 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
if (__name__ == '__main__'):
|
| 390 |
app.run( host='0.0.0.0', port=7860)
|
|
|
|
| 2 |
import base64
|
| 3 |
import numpy as np
|
| 4 |
import io
|
| 5 |
+
from flask import Flask, render_template, Response, request, jsonify
|
| 6 |
from flask_socketio import SocketIO, emit
|
| 7 |
from PIL import Image
|
| 8 |
from time import time as unix_time
|
|
|
|
| 201 |
|
| 202 |
|
| 203 |
app = Flask(__name__)
|
| 204 |
+
|
| 205 |
|
| 206 |
@app.route('/')
|
| 207 |
def index():
|
| 208 |
return render_template('index.html')
|
| 209 |
|
| 210 |
|
| 211 |
+
@app.route('/api/data', methods=['POST'])
|
| 212 |
+
def handle_video_frame():
|
| 213 |
+
frame = request.json.get('key')
|
| 214 |
+
#print(request.json)
|
| 215 |
response_frame = data_uri_to_image(frame)
|
| 216 |
decimg = response_frame
|
| 217 |
|
|
|
|
| 283 |
letter_result='_'
|
| 284 |
else:
|
| 285 |
letter_result='>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
except Exception as e:
|
| 287 |
# Ha! The catch err{throw err} scenario, it was actually quite useful in debugging though
|
| 288 |
print(e)
|
| 289 |
+
frame_data = image_to_data_uri(final_image)
|
| 290 |
+
#print(frame_data)
|
| 291 |
+
|
| 292 |
+
return jsonify({"result": letter_result, "frame": frame_data}), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
def data_uri_to_image(data_uri):
|
| 295 |
header, encoded = data_uri.split(',', 1)
|
|
|
|
| 298 |
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 299 |
return image
|
| 300 |
|
| 301 |
+
def image_to_data_uri(image):
|
| 302 |
+
# Encode the image as a JPEG
|
| 303 |
+
_, buffer = cv2.imencode('.jpg', image)
|
| 304 |
+
# Convert the buffer to bytes
|
| 305 |
+
image_bytes = buffer.tobytes()
|
| 306 |
+
# Encode the bytes to Base64
|
| 307 |
+
base64_encoded = base64.b64encode(image_bytes).decode('utf-8')
|
| 308 |
+
# Create the Data URI
|
| 309 |
+
data_uri = f"data:image/jpeg;base64,{base64_encoded}"
|
| 310 |
+
return data_uri
|
| 311 |
if (__name__ == '__main__'):
|
| 312 |
app.run( host='0.0.0.0', port=7860)
|
templates/index.html
CHANGED
|
@@ -8,10 +8,12 @@
|
|
| 8 |
|
| 9 |
<body>
|
| 10 |
<video style="display:none" id="video" width="640" height="480" autoplay></video>
|
| 11 |
-
<canvas id="canvas" style="display:
|
|
|
|
|
|
|
| 12 |
<script>
|
| 13 |
-
|
| 14 |
-
|
| 15 |
// Get access to the user's camera
|
| 16 |
navigator.mediaDevices.getUserMedia({ video: true })
|
| 17 |
.then(function (stream) {
|
|
@@ -48,10 +50,33 @@
|
|
| 48 |
//console.log(jpeg)
|
| 49 |
// Do something with the data URL, such as display it in an image element
|
| 50 |
//document.getElementById('image').src = dataUrl;
|
| 51 |
-
|
| 52 |
console.log("emitted")
|
| 53 |
setTimeout(snap, 100)
|
| 54 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
</script>
|
| 56 |
<button id="snap" onclick="snap()">Snap</button>
|
| 57 |
<img id="image" />
|
|
|
|
| 8 |
|
| 9 |
<body>
|
| 10 |
<video style="display:none" id="video" width="640" height="480" autoplay></video>
|
| 11 |
+
<canvas id="canvas" style="display:none;"></canvas>
|
| 12 |
+
<img id="image"></img>
|
| 13 |
+
<p id="result"></p>
|
| 14 |
<script>
|
| 15 |
+
|
| 16 |
+
globalData=0
|
| 17 |
// Get access to the user's camera
|
| 18 |
navigator.mediaDevices.getUserMedia({ video: true })
|
| 19 |
.then(function (stream) {
|
|
|
|
| 50 |
//console.log(jpeg)
|
| 51 |
// Do something with the data URL, such as display it in an image element
|
| 52 |
//document.getElementById('image').src = dataUrl;
|
| 53 |
+
send_frame(dataUrl)
|
| 54 |
console.log("emitted")
|
| 55 |
setTimeout(snap, 100)
|
| 56 |
};
|
| 57 |
+
function send_frame(dataUrl) {
|
| 58 |
+
const dataToSend = {
|
| 59 |
+
key: dataUrl
|
| 60 |
+
};
|
| 61 |
+
|
| 62 |
+
fetch('/api/data', {
|
| 63 |
+
method: 'POST',
|
| 64 |
+
headers: {
|
| 65 |
+
'Content-Type': 'application/json'
|
| 66 |
+
},
|
| 67 |
+
body: JSON.stringify(dataToSend) // Convert the data to JSON
|
| 68 |
+
})
|
| 69 |
+
.then(response => response.json())
|
| 70 |
+
.then(data => {
|
| 71 |
+
//console.log(data.frame)
|
| 72 |
+
document.getElementById("image").src=data.frame
|
| 73 |
+
globalData=data
|
| 74 |
+
document.getElementById("result").innerText=data.result
|
| 75 |
+
})
|
| 76 |
+
.catch((error) => {
|
| 77 |
+
console.error('Error:', error);
|
| 78 |
+
});
|
| 79 |
+
}
|
| 80 |
</script>
|
| 81 |
<button id="snap" onclick="snap()">Snap</button>
|
| 82 |
<img id="image" />
|