Skip to content
Snippets Groups Projects
Commit f3ff164b authored by Simon Flaisch's avatar Simon Flaisch
Browse files

Switched to fastapi, made some overall improvements and prepared the code for the latency-tests

parent e933887f
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ import paho.mqtt.client as mqtt ...@@ -4,7 +4,7 @@ import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server. # The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, reason_code, properties): def on_connect(client, userdata, flags, reason_code, properties):
print(f"Connected with result code {reason_code}") print(f"Connected with result code {reason_code}")
client.subscribe("Topic1") client.subscribe("Finger")
# The callback for when a PUBLISH message is received from the server. # The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg): def on_message(client, userdata, msg):
......
import cv2
from imutils.video import VideoStream
cv2.namedWindow("preview")
vc = VideoStream("http://localhost:88/video_feed").start()
while True:
frame = vc.read()
cv2.imshow("preview", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyWindow("preview")
vc.release()
\ No newline at end of file
...@@ -4,6 +4,6 @@ ...@@ -4,6 +4,6 @@
</head> </head>
<body> <body>
<h1>HHZ Livestream</h1> <h1>HHZ Livestream</h1>
<img src="http://localhost:88/video_feed"> <img src="http://localhost:89/video_feed">
</body> </body>
</html> </html>
\ No newline at end of file
import os
import cv2 import cv2
import uvicorn
import warnings import warnings
import numpy as np import numpy as np
import pyttsx3 import pyttsx3
...@@ -11,11 +11,8 @@ import json ...@@ -11,11 +11,8 @@ import json
import requests import requests
from PIL import Image from PIL import Image
from imutils.video import VideoStream from imutils.video import VideoStream
from flask import Response from fastapi import FastAPI
from flask import Flask from fastapi.responses import StreamingResponse
from flask import render_template
import argparse
import datetime
import imutils import imutils
...@@ -29,19 +26,38 @@ engine = pyttsx3.init('sapi5') ...@@ -29,19 +26,38 @@ engine = pyttsx3.init('sapi5')
app = Flask(__name__) app = FastAPI()
# Definiere Kamera und starte Videoaufnahme # Definiere Kamera und starte Videoaufnahme
video_capture =VideoStream(src=0).start() try:
video_capture =VideoStream("http://localhost:80/video_feed").start()
except Exception as e:
print(f"We couldn't reach the camera")
exit()
# Definiere, welche Cascade OpenCV verwenden soll # Definiere, welche Cascade OpenCV verwenden soll
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
time.sleep(2.0) time.sleep(2.0)
class Main: class Main:
def __init__(self):
self.outputFrame = None
self.lock = threading.Lock()
self.wait_in_thread = None
self.streamThread = None
self.analysis_results = None
self.analysis_thread = None
self.speaking_thread = None # Verfolgt den aktuellen Sprech-Thread
self.main_time = 0
self.last_chat_time = 0 # Zeitpunkt des letzten Sprechens API kosten beachten
self.currentclient = None
self.unacked_publish = set()
self.flag = False
self.selection = 3
self.it = 0
def stream(self): def stream(self):
app.run(host="localhost", port=88, debug=True, uvicorn.run(host="localhost", port=89, app=app)
threaded=True, use_reloader=False)
def streamingThread(self): def streamingThread(self):
self.streamThread = threading.Thread(target=self.stream) self.streamThread = threading.Thread(target=self.stream)
self.streamThread.daemon = True self.streamThread.daemon = True
...@@ -92,21 +108,6 @@ class Main: ...@@ -92,21 +108,6 @@ class Main:
while len(self.unacked_publish): while len(self.unacked_publish):
time.sleep(0.1) time.sleep(0.1)
msg.wait_for_publish() msg.wait_for_publish()
def __init__(self):
self.outputFrame = None
self.lock = threading.Lock()
self.wait_in_thread = None
self.streamThread = None
self.analysis_results = None
self.analysis_thread = None
self.speaking_thread = None # Verfolgt den aktuellen Sprech-Thread
self.main_time = 0
self.last_chat_time = 0 # Zeitpunkt des letzten Sprechens API kosten beachten
self.currentclient = None
self.unacked_publish = set()
self.flag = False
self.selection = 3
self.it = 0
def speak(self, text): def speak(self, text):
...@@ -167,8 +168,6 @@ class Main: ...@@ -167,8 +168,6 @@ class Main:
def face_recognition(self, mqttc): def face_recognition(self, mqttc):
while True: while True:
#cv2.namedWindow("video_capture", cv2.WINDOW_NORMAL)
#cv2.resizeWindow("video_capture", 800, 600)
frame = video_capture.read() frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
...@@ -231,7 +230,6 @@ class Main: ...@@ -231,7 +230,6 @@ class Main:
frame = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR) frame = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR)
if self.speaking_thread and self.speaking_thread.is_alive(): if self.speaking_thread and self.speaking_thread.is_alive():
#self.flag = False
self.it = self.it + 1 self.it = self.it + 1
frame_pillow = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))# Auslagern frame_pillow = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))# Auslagern
...@@ -273,12 +271,12 @@ if __name__ == '__main__': ...@@ -273,12 +271,12 @@ if __name__ == '__main__':
main = Main() main = Main()
main.welcome_message() main.welcome_message()
main.streamingThread() main.streamingThread()
@app.route("/video_feed") @app.get("/video_feed")
def video_feed(): def video_feed():
# return the response generated along with the specific media # return the response generated along with the specific media
# type (mime type) # type (mime type)
return Response(main.generate(), return StreamingResponse(main.generate(),
mimetype="multipart/x-mixed-replace; boundary=frame") media_type="multipart/x-mixed-replace; boundary=frame")
main.currentclient =main.connectwithmqtt(adress="localhost", targetport=1883) main.currentclient =main.connectwithmqtt(adress="localhost", targetport=1883)
main.face_recognition(mqttc=main.currentclient) main.face_recognition(mqttc=main.currentclient)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment