-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
103 lines (82 loc) · 2.64 KB
/
flask_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
from flask import Flask, Response
import cv2
import gtts, playsound, os
import emoji
from PIL import Image, ImageFont, ImageDraw
import numpy as np
import urllib.request as urlib
from pynput import keyboard
app = Flask(__name__)
video = cv2.VideoCapture(0)
USE_PHONE = False
count = 0
command = "Default"
tts = gtts.gTTS("Turn left", tld="co.nz")
fn_left = "turnleft.mp3"
tts.save(fn_left)
tts = gtts.gTTS("Turn right", tld="co.nz")
fn_right = "turnright.mp3"
tts.save(fn_right)
tts = gtts.gTTS("Go forward", tld="co.nz")
fn_forward = "goforward.mp3"
tts.save(fn_forward)
@app.route('/')
def index():
return "Default Message"
def draw_emoji(image, emoji=':left_arrow:'):
# font issue
image = Image.fromarray(image)
draw = ImageDraw.Draw(image)
tick=str(emoji.emojize(emoji))
font = ImageFont.truetype("Arial Unicode.ttf",32)
draw.text((40, 80),tick,(255,255,255),font=font)
image = np.array(image)
return image
def on_press(key):
pass
def on_release(key):
global command
print('{0} released'.format(key))
if key == keyboard.Key.right:
playsound.playsound(fn_right)
command = "Turn right!"
if key == keyboard.Key.left:
playsound.playsound(fn_left)
command = "Turn left!"
if key == keyboard.Key.up:
playsound.playsound(fn_forward)
command = "Go forward!"
URL = "http://172.20.10.5:8080/shot.jpg?rnd=493155"
def gen(video):
while True:
global count
global command
if USE_PHONE:
response = urlib.urlopen(URL)
#response_numpy = np.array(response)
# read image as an numpy array
image = np.asarray(bytearray(response.read()), dtype="uint8")
# use imdecode function
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
else:
success, image = video.read()
frame_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
frame_gray = cv2.equalizeHist(frame_gray)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image, command, (100, 100), font, 3, (255, 255, 255), 3)
ret, jpeg = cv2.imencode('.jpg', image)
frame = jpeg.tobytes()
count += 1
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/video_feed')
def video_feed():
global video
listener = keyboard.Listener(
on_press=on_press,
on_release=on_release)
listener.start()
return Response(gen(video),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=2204, threaded=True)