@@ -1,5 +1,6 @@ | |||
import cv2 | |||
import numpy as np | |||
from game import Game | |||
# from Track import nothing | |||
# Farbwerte für die Erkennung (Beispiel: Rot, Grün, Blau) | |||
colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0)] | |||
@@ -15,6 +16,12 @@ upper_green = np.array([70, 255, 255]) | |||
lower_blue = np.array([90, 100, 100]) | |||
upper_blue = np.array([130, 255, 255]) | |||
CURRENT_SCORES= {'score_red': 0, | |||
'score_green': 0, | |||
'score_blue': 0 | |||
} | |||
# Funktion zur Farberkennung | |||
def erkennung_farben(img): | |||
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | |||
@@ -91,6 +98,13 @@ def ermittle_position(results, img_width): | |||
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 | |||
if __name__ == "__main__": | |||
# Videoquelle öffnen (kann auch eine Bilddatei sein) | |||
@@ -103,10 +117,12 @@ if __name__ == "__main__": | |||
# Fehlerbehandlung, wenn kein Bild gelesen werden kann | |||
if not ret: | |||
break | |||
# Farben erkennen | |||
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 | |||
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) |
@@ -2,18 +2,19 @@ from Database.database import QuestionDataBase | |||
import random | |||
class Game: | |||
def __init__(self, teamsize=0, correct_field=0): | |||
self.teamsize = teamsize | |||
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.available_questions = list(range(1, self.questions.num_rows())) | |||
self.field = correct_field | |||
self.question = {} | |||
self.final_score = {} | |||
def set_teamsize(self, teamsize: int): | |||
self.teamsize = teamsize | |||
@@ -27,22 +28,36 @@ class Game: | |||
def set_scoreboard(self, current_scores: dict): | |||
self.scoreboard = self.add_score(current_scores) | |||
return self.scoreboard | |||
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") | |||
field_names = [i[0] for i in questions.description] | |||
questions_data = questions.fetchall() | |||
random_question_number = random.choice(self.available_questions) | |||
self.available_questions.remove(random_question_number) | |||
question= list(questions_data[random_question_number]) | |||
self.question = dict(zip(field_names, question)) | |||
self.shuffle_answeroptions() | |||
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): | |||
answeroption = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3', 'Correct_answeroption'] | |||
@@ -56,4 +71,8 @@ class Game: | |||
def set_correct_field(self, correct_field: int): | |||
self.field = correct_field | |||
def final_result(self): | |||
self.final_score = dict(sorted(self.scoreboard.items(), key=lambda x: x[1], reverse=True)) | |||
@@ -8,17 +8,21 @@ my_game = Game() | |||
@app.route('/question', methods=['GET']) | |||
def get_question(): | |||
my_game.get_question() | |||
print(my_game.question) | |||
return jsonify(my_game.question) | |||
@app.route('/teamsize', methods=['POST']) | |||
def teamsize(): | |||
team_size = request.json | |||
my_game.set_teamsize(team_size.get('teamsize')) | |||
print(f'Teamsize: {my_game.teamsize}\n') | |||
return Response(status=200) | |||
@app.route('/scoreboard', methods=['GET']) | |||
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']) | |||
def check(): | |||
@@ -35,15 +39,21 @@ def get_scores(): | |||
my_game.add_score(scores) | |||
return Response(status=200) | |||
@app.route('/winner', methods=['GET']) | |||
def set_winner(): | |||
my_game.final_result() | |||
return jsonify(my_game.final_score) | |||
def main(): | |||
## ASUS Router | |||
## Router | |||
# app.run(host='192.168.50.79', port=5555, debug=True) | |||
## 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__': | |||
main() | |||
@@ -1,15 +0,0 @@ | |||
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() |