import cv2 | import cv2 | ||||
import numpy as np | import numpy as np | ||||
from game import Game | |||||
# from Track import nothing | # from Track import nothing | ||||
# Farbwerte für die Erkennung (Beispiel: Rot, Grün, Blau) | # Farbwerte für die Erkennung (Beispiel: Rot, Grün, Blau) | ||||
colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0)] | colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0)] | ||||
lower_blue = np.array([90, 100, 100]) | lower_blue = np.array([90, 100, 100]) | ||||
upper_blue = np.array([130, 255, 255]) | upper_blue = np.array([130, 255, 255]) | ||||
CURRENT_SCORES= {'score_red': 0, | |||||
'score_green': 0, | |||||
'score_blue': 0 | |||||
} | |||||
# Funktion zur Farberkennung | # Funktion zur Farberkennung | ||||
def erkennung_farben(img): | def erkennung_farben(img): | ||||
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | ||||
return positions | return positions | ||||
def count_colors(red: int, green: int, blue: int) -> None: | |||||
currrent_scores = {'score_red': red, | |||||
'score_green': green, | |||||
'score_blue': blue | |||||
} | |||||
Game.set_scoreboard(currrent_scores) | |||||
# Hauptprogramm | # Hauptprogramm | ||||
if __name__ == "__main__": | if __name__ == "__main__": | ||||
# Videoquelle öffnen (kann auch eine Bilddatei sein) | # Videoquelle öffnen (kann auch eine Bilddatei sein) | ||||
# Fehlerbehandlung, wenn kein Bild gelesen werden kann | # Fehlerbehandlung, wenn kein Bild gelesen werden kann | ||||
if not ret: | if not ret: | ||||
break | break | ||||
# Farben erkennen | # Farben erkennen | ||||
farben_img, ergebnisse, count_red, count_green, count_blue = erkennung_farben(frame) | farben_img, ergebnisse, count_red, count_green, count_blue = erkennung_farben(frame) | ||||
# Add counted colours to the scoreboard in the correct field | |||||
count_colors(count_red, count_green, count_blue) | |||||
# Anzahl der Farben anzeigen | # Anzahl der Farben anzeigen | ||||
cv2.putText(farben_img, f"Rot: {count_red}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, colors[0], 2) | cv2.putText(farben_img, f"Rot: {count_red}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, colors[0], 2) | ||||
cv2.putText(farben_img, f"Gruen: {count_green}", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, colors[1], 2) | cv2.putText(farben_img, f"Gruen: {count_green}", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, colors[1], 2) |
import random | import random | ||||
class Game: | class Game: | ||||
def __init__(self, teamsize=0, correct_field=0): | def __init__(self, teamsize=0, correct_field=0): | ||||
self.teamsize = teamsize | self.teamsize = teamsize | ||||
self.scoreboard = {'score_red': 0, | self.scoreboard = {'score_red': 0, | ||||
'score_blue': 0, | |||||
'score_green': 0} | |||||
'score_green': 0, | |||||
'score_blue': 0 | |||||
} | |||||
self.questions = QuestionDataBase('src_folder/BackEnd/Database/EinsZweiOderDrei.db') | self.questions = QuestionDataBase('src_folder/BackEnd/Database/EinsZweiOderDrei.db') | ||||
self.available_questions = list(range(1, self.questions.num_rows())) | self.available_questions = list(range(1, self.questions.num_rows())) | ||||
self.field = correct_field | self.field = correct_field | ||||
self.question = {} | self.question = {} | ||||
self.final_score = {} | |||||
def set_teamsize(self, teamsize: int): | def set_teamsize(self, teamsize: int): | ||||
self.teamsize = teamsize | self.teamsize = teamsize | ||||
def set_scoreboard(self, current_scores: dict): | def set_scoreboard(self, current_scores: dict): | ||||
self.scoreboard = self.add_score(current_scores) | self.scoreboard = self.add_score(current_scores) | ||||
return self.scoreboard | return self.scoreboard | ||||
def reset_game(self): | def reset_game(self): | ||||
self.__init__() | |||||
self.scoreboard = {'score_red': 0, | |||||
'score_green': 0, | |||||
'score_blue': 0 | |||||
} | |||||
def get_question(self) -> dict: | |||||
def get_question(self): | |||||
questions = self.questions.cursor.execute("Select * from Questions") | questions = self.questions.cursor.execute("Select * from Questions") | ||||
field_names = [i[0] for i in questions.description] | field_names = [i[0] for i in questions.description] | ||||
questions_data = questions.fetchall() | questions_data = questions.fetchall() | ||||
random_question_number = random.choice(self.available_questions) | random_question_number = random.choice(self.available_questions) | ||||
self.available_questions.remove(random_question_number) | self.available_questions.remove(random_question_number) | ||||
question= list(questions_data[random_question_number]) | question= list(questions_data[random_question_number]) | ||||
self.question = dict(zip(field_names, question)) | self.question = dict(zip(field_names, question)) | ||||
self.shuffle_answeroptions() | |||||
self.add_correct_field() | self.add_correct_field() | ||||
return self.question | |||||
def shuffle_answeroptions(self): | |||||
answeroptions = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3'] | |||||
keys = [] | |||||
print(self.question) | |||||
for answer in answeroptions: | |||||
keys.append(self.question[answer]) | |||||
random.shuffle(keys) | |||||
for index, answer in enumerate(answeroptions): | |||||
self.question[answer] = keys[index] | |||||
def add_correct_field(self): | def add_correct_field(self): | ||||
answeroption = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3', 'Correct_answeroption'] | answeroption = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3', 'Correct_answeroption'] | ||||
def set_correct_field(self, correct_field: int): | def set_correct_field(self, correct_field: int): | ||||
self.field = correct_field | self.field = correct_field | ||||
def final_result(self): | |||||
self.final_score = dict(sorted(self.scoreboard.items(), key=lambda x: x[1], reverse=True)) | |||||
@app.route('/question', methods=['GET']) | @app.route('/question', methods=['GET']) | ||||
def get_question(): | def get_question(): | ||||
my_game.get_question() | my_game.get_question() | ||||
print(my_game.question) | |||||
return jsonify(my_game.question) | return jsonify(my_game.question) | ||||
@app.route('/teamsize', methods=['POST']) | @app.route('/teamsize', methods=['POST']) | ||||
def teamsize(): | def teamsize(): | ||||
team_size = request.json | team_size = request.json | ||||
my_game.set_teamsize(team_size.get('teamsize')) | my_game.set_teamsize(team_size.get('teamsize')) | ||||
print(f'Teamsize: {my_game.teamsize}\n') | |||||
return Response(status=200) | return Response(status=200) | ||||
@app.route('/scoreboard', methods=['GET']) | @app.route('/scoreboard', methods=['GET']) | ||||
def scoreboard(): | def scoreboard(): | ||||
return my_game.scoreboard, Response(status=200) | |||||
my_game.set_scoreboard() | |||||
print(my_game.scoreboard) | |||||
return jsonify(my_game.scoreboard) | |||||
@app.route('/check', methods=['GET']) | @app.route('/check', methods=['GET']) | ||||
def check(): | def check(): | ||||
my_game.add_score(scores) | my_game.add_score(scores) | ||||
return Response(status=200) | return Response(status=200) | ||||
@app.route('/winner', methods=['GET']) | |||||
def set_winner(): | |||||
my_game.final_result() | |||||
return jsonify(my_game.final_score) | |||||
def main(): | def main(): | ||||
## ASUS Router | |||||
## Router | |||||
# app.run(host='192.168.50.79', port=5555, debug=True) | # app.run(host='192.168.50.79', port=5555, debug=True) | ||||
## Postman | ## Postman | ||||
app.run(host='127.0.0.1', port=5555, debug=True) | |||||
app.run(host='127.0.0.1', port=5555, debug=True) | |||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
main() | main() | ||||
from flask import Flask, jsonify, Response | |||||
app = Flask(__name__) | |||||
@app.route('/CONNECTION', methods=['GET']) | |||||
def connection(): | |||||
return Response(status=200) | |||||
def main(): | |||||
app.run(host='127.0.0.1', port=5555, debug=True) | |||||
if __name__ == '__main__': | |||||
main() |