-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcotrolRobotLiveStreamServerDetectionYolo.py
More file actions
123 lines (98 loc) · 3.6 KB
/
Copy pathcotrolRobotLiveStreamServerDetectionYolo.py
File metadata and controls
123 lines (98 loc) · 3.6 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
from kafka import KafkaConsumer
import cv2
import numpy as np
from flask import Flask, render_template, request, Response, jsonify
from kafka import KafkaProducer
import json
app = Flask(__name__)
SERVER = "192.168.195.99"
KAFKA_SERVER_PORT = 9092
topic1 = 'test'
topic = 'carControl'
topic2 = 'serverDetection'
consumer = KafkaConsumer(
topic1,
bootstrap_servers=[f"{SERVER}:{KAFKA_SERVER_PORT}"],
auto_offset_reset='latest'
)
consumer2 = KafkaConsumer(
topic2,
bootstrap_servers=[f"{SERVER}:{KAFKA_SERVER_PORT}"],
auto_offset_reset='latest'
)
producer = KafkaProducer(bootstrap_servers=[f"{SERVER}:{KAFKA_SERVER_PORT}"], value_serializer=lambda x: json.dumps(x).encode('utf-8'))
#_______________________________ the functions car control __________________________
def forward():
producer.send(topic, 'forward')
def backward():
producer.send(topic, 'backward')
def left():
producer.send(topic, 'left')
def right():
producer.send(topic, 'right')
def stop():
producer.send(topic, 'stop')
def auto():
producer.send(topic, 'auto')
def manual():
producer.send(topic, 'manual')
#_____________________________________________________________________
#_______________________________ the functions live stream __________________________
def gen_frames():
for msg in consumer:
nparr = np.frombuffer(msg.value, np.uint8)
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
# resize the frame to a smaller size
frame = cv2.resize(frame, (640, 480))
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 50])[1].tobytes() + b'\r\n')
#_____________________________________________________________________
#_______________________________ the functions live stream2 __________________________
def gen_frames_detect():
for msg in consumer2:
nparr = np.frombuffer(msg.value, np.uint8)
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
# resize the frame to a smaller size
frame = cv2.resize(frame, (640, 480))
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 50])[1].tobytes() + b'\r\n')
#_____________________________________________________________________
#_______________________________ Routes __________________________
@app.route('/')
def index():
return render_template('index.html')
@app.route("/", methods=["POST", "GET"])
def control():
if request.method == "POST":
data = request.get_json()
command = data.get('command')
if command == 'forward':
forward()
elif command == 'backward':
backward()
elif command == 'left':
left()
elif command == 'right':
right()
elif command == 'stop':
stop()
elif command == 'manual':
manual()
elif command == 'auto':
auto()
return jsonify({'status': 'success'})
return render_template("index.html")
@app.route('/video_feed')
def video_feed():
return Response(gen_frames(),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/video_feed_video')
def video_feed_video():
return Response(gen_frames(),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/video_feed_detect')
def video_feed_detect():
return Response(gen_frames_detect(),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)