-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
160 lines (136 loc) · 5.41 KB
/
server.py
File metadata and controls
160 lines (136 loc) · 5.41 KB
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
157
158
159
160
from flask import Flask, render_template, request, make_response, Response, jsonify
import json
import cv2
import os
import numpy as np
import midas_processing as mp
import base64
import random
import torch
def get_base_url(port:int) -> str:
'''
Returns the base URL to the webserver if available.
i.e. if the webserver is running on coding.ai-camp.org port 12345, then the base url is '/<your project id>/port/12345/'
Inputs: port (int) - the port number of the webserver
Outputs: base_url (str) - the base url to the webserver
'''
try:
info = json.load(open(os.path.join(os.environ['HOME'], '.smc', 'info.json'), 'r'))
project_id = info['project_id']
base_url = f'/{project_id}/port/{port}/'
except Exception as e:
print(f'Server is probably running in production, so a base url does not apply: \n{e}')
base_url = '/'
return base_url
'''
to run
flask --app server run
or
python server.py
to exit
ctrl + c
'''
global switch, cap, tracker, vision_mode, thing_the_website_should_say
switch=1
vision_mode=1 # 1 is normal, 2 is computer vision, 3 is rainbows and unicorns
port = 5000
base_url = get_base_url(port)
thing_the_website_should_say = "None"
# Flask App
app = Flask(__name__)
# OpenCV Webcam
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
tracker = mp.MiDaS()
def euclid_dist(x1, y1, x2, y2):
return np.linalg.norm([x2 - x1, y2 - y1])
# Home Page
@app.route(f"{base_url}", methods=['GET', 'POST'])
def index():
print("Loading Home Page...")
global switch, cap, vision_mode
if request.method == 'POST':
print("Post Request Received")
print(request.form)
if request.form.get('option') == 'cv':
print("Computer Vision Mode")
vision_mode = 2
elif request.form.get('option') == 'tv':
print("Technical Vision Mode")
vision_mode = 3
else:
print("Normal Vision Mode")
vision_mode = 1
elif request.method == 'GET':
return render_template("index.html")
return render_template("index.html")
@app.route(f"{base_url}/video_feed/")
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
# 127.0.0.1:5000/info
@app.route(f"{base_url}/info/", methods=['GET', 'POST'])
def return_info():
global thing_the_website_should_say # Add this line to access the global variable
return make_response(jsonify({'info': thing_the_website_should_say}), 200)
def gen_frames():
global computer_vision, cap, tracker, thing_the_website_should_say
while True:
try:
success, image = cap.read()
except:
print("Camera not found")
break
if vision_mode == 2: # if computer vision mode
results = tracker.yolo_model.predict(image)
for result in results:
boxes = result.boxes.xyxy
labels = result.boxes.cls
for box, label in zip(boxes, labels):
x1, y1, x2, y2 = box[:4].tolist()
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(image, result.names[int(label)], (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
try:
ret, buffer = cv2.imencode('.jpg', image)
img_64 = base64.b64encode(buffer)
#print(img_64)
#pred = tracker.identifier.predict(img_64)
#print(pred)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
except Exception as e:
print("exception thrown when trying to encode image")
pass
elif vision_mode == 3: # if midas view mode (formerly technical)
tracker.filter(tracker.normalize(tracker.predict(image)), vibrate="Website", colorful_image=image) # show both vibration info and verbal warning
image = tracker.website_image
thing_the_website_should_say = tracker.current_warning
try:
ret, buffer = cv2.imencode('.jpg', image)
img_64 = base64.b64encode(buffer)
#print(img_64)
#pred = tracker.identifier.predict(img_64)
#print(pred)
frame = buffer.tobytes()
#print(tracker.states)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
except Exception as e:
print("exception thrown when trying to encode image")
pass
else:
try:
ret, buffer = cv2.imencode('.jpg', image)
#print(img_64)
#pred = tracker.identifier.predict(img_64)
#print(pred)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
except Exception as e:
print("exception thrown when trying to encode image")
pass
if __name__ == "__main__":
app.run(debug=True)