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)] | ||||
color_names = ["Rot", "Gruen", "Blau"] | color_names = ["Rot", "Gruen", "Blau"] | ||||
# video cap | |||||
# Farbgrenzen für die Erkennung | # Farbgrenzen für die Erkennung | ||||
lower_red = np.array([0, 100, 100]) | |||||
upper_red = np.array([10, 255, 255]) | |||||
lower_red = np.array([80, 160, 150]) | |||||
upper_red = np.array([255, 255, 255]) | |||||
lower_green = np.array([40, 100, 100]) | |||||
upper_green = np.array([70, 255, 255]) | |||||
lower_green = np.array([40, 50, 160]) | |||||
upper_green = np.array([80, 255, 255]) | |||||
lower_blue = np.array([90, 100, 100]) | |||||
lower_blue = np.array([95, 180, 90]) | |||||
upper_blue = np.array([130, 255, 255]) | upper_blue = np.array([130, 255, 255]) | ||||
CURRENT_SCORES= {'score_red': 0, | CURRENT_SCORES= {'score_red': 0, | ||||
'score_green': 0, | 'score_green': 0, | ||||
'score_blue': 0 | 'score_blue': 0 | ||||
def ermittle_position(results, img_width): | def ermittle_position(results, img_width): | ||||
positions = [] | positions = [] | ||||
# pos_to_num = {"Rechts": 3, | |||||
# "Mitte": 2, | |||||
# "Links": 1, | |||||
# "Nicht gefunden": 0} | |||||
for result in results: | for result in results: | ||||
if result is None: | if result is None: | ||||
position = "Nicht gefunden" | |||||
position = 0 | |||||
else: | else: | ||||
x = result[0] | x = result[0] | ||||
if x < img_width / 3: | if x < img_width / 3: | ||||
position = "Rechts" | |||||
position = 3 | |||||
elif x < 2 * img_width / 3: | elif x < 2 * img_width / 3: | ||||
position = "Mitte" | |||||
position = 2 | |||||
else: | else: | ||||
position = "Links" | |||||
position = 1 | |||||
positions.append(position) | positions.append(position) | ||||
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) | |||||
# def count_colors(my_game: Game, red: int, green: int, blue: int) -> None: | |||||
# currrent_scores = {'score_red': red, | |||||
# 'score_green': green, | |||||
# 'score_blue': blue | |||||
# } | |||||
# my_game.set_scoreboard(currrent_scores) | |||||
# Hauptprogramm | |||||
if __name__ == "__main__": | |||||
# Videoquelle öffnen (kann auch eine Bilddatei sein) | |||||
video = cv2.VideoCapture(1) # Hier "0" für die Kamera verwenden | |||||
def main(): | |||||
# Videoquelle öffnen (kann auch eine Bilddatei sein) | |||||
video = cv2.VideoCapture(0) # Hier "0" für die Kamera verwenden | |||||
while True: | while True: | ||||
# Einzelbild von der Videoquelle lesen | |||||
ret, frame = video.read() | |||||
ret, frame = video.read(0) | |||||
# Fehlerbehandlung, wenn kein Bild gelesen werden kann | # Fehlerbehandlung, wenn kein Bild gelesen werden kann | ||||
if not ret: | if not ret: | ||||
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 | # Add counted colours to the scoreboard in the correct field | ||||
count_colors(count_red, count_green, count_blue) | |||||
# 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"Gruen: {count_green}", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, colors[1], 2) | |||||
cv2.putText(farben_img, f"Blau: {count_blue}", (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 1, colors[2], 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"Blau: {count_blue}", (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 1, colors[2], 2) | |||||
# Positionen ermitteln | # Positionen ermitteln | ||||
img_width = frame.shape[1] | img_width = frame.shape[1] | ||||
cv2.line(farben_img, (img_width // 3, 0), (img_width // 3, frame.shape[0]), (0, 0, 0), 2) | cv2.line(farben_img, (img_width // 3, 0), (img_width // 3, frame.shape[0]), (0, 0, 0), 2) | ||||
cv2.line(farben_img, (2 * img_width // 3, 0), (2 * img_width // 3, frame.shape[0]), (0, 0, 0), 2) | cv2.line(farben_img, (2 * img_width // 3, 0), (2 * img_width // 3, frame.shape[0]), (0, 0, 0), 2) | ||||
# Bild anzeigen | |||||
# Bild anzeigen | |||||
cv2.imshow("Farberkennung", farben_img) | cv2.imshow("Farberkennung", farben_img) | ||||
# Auf "q" drücken, um die Schleife zu beenden | |||||
# Auf "q" drücken, um die Schleife zu beenden | |||||
if cv2.waitKey(1) & 0xFF == ord('q'): | if cv2.waitKey(1) & 0xFF == ord('q'): | ||||
break | break | ||||
# Videoquelle und Fenster schließen | |||||
# Videoquelle und Fenster schließen | |||||
video.release() | video.release() | ||||
cv2.destroyAllWindows() | cv2.destroyAllWindows() | ||||
if __name__ == "__main__": | |||||
main() |
('Wie heißt der höchste Berg Europas?', 'Zugspitze', 'Matterhorn', 'Montblanc', 'Montblanc'), | ('Wie heißt der höchste Berg Europas?', 'Zugspitze', 'Matterhorn', 'Montblanc', 'Montblanc'), | ||||
('Wie lange braucht das Licht in etwa von der Sonne zur Erde?', '80 Sekunden', '8 Minuten', '7 Tage', '8 Minuten'), | ('Wie lange braucht das Licht in etwa von der Sonne zur Erde?', '80 Sekunden', '8 Minuten', '7 Tage', '8 Minuten'), | ||||
('Was hat James Watt erfunden?', 'Glühbirne', 'Elektrischen Widerstand', 'Dampfmaschine', 'Dampfmaschine'), | ('Was hat James Watt erfunden?', 'Glühbirne', 'Elektrischen Widerstand', 'Dampfmaschine', 'Dampfmaschine'), | ||||
('Wie hieß der erste Deutsch Bundeskanzler?', 'Helmut Schmidt', 'Konrad Adenauer', 'Willy Brandt', 'Konrad Adenauer'), | |||||
('Wie hieß der erste Deutsche Bundeskanzler?', 'Helmut Schmidt', 'Konrad Adenauer', 'Willy Brandt', 'Konrad Adenauer'), | |||||
('Von wem stammt das Gemälde der Mona Lisa?', 'Picasso', 'Vincent van Gogh', 'Leonardo da Vinci', 'Leonardo da Vinci'), | ('Von wem stammt das Gemälde der Mona Lisa?', 'Picasso', 'Vincent van Gogh', 'Leonardo da Vinci', 'Leonardo da Vinci'), | ||||
('Wie heißt der Erfinder der Jeanshose?', 'Tom Wrangler', 'Levi Strauss', 'Peter Diesel', 'Levi Strauss'), | ('Wie heißt der Erfinder der Jeanshose?', 'Tom Wrangler', 'Levi Strauss', 'Peter Diesel', 'Levi Strauss'), | ||||
('Welches Land gewann 2014 die Fußball Weltmeisterschaft?', 'Brasilien', 'Argentien', 'Deutschland', 'Deutschland'), | ('Welches Land gewann 2014 die Fußball Weltmeisterschaft?', 'Brasilien', 'Argentien', 'Deutschland', 'Deutschland'), |
import cv2 | |||||
import numpy as np | |||||
class Camera(): | |||||
def __init__(self) -> None: | |||||
self.colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0)] | |||||
self.color_names = ["Rot", "Gruen", "Blau"] | |||||
self.lower_red = np.array([80, 160, 150]) | |||||
self.upper_red = np.array([255, 255, 255]) | |||||
self.lower_green = np.array([40, 50, 160]) | |||||
self.upper_green = np.array([80, 255, 255]) | |||||
self.lower_blue = np.array([95, 180, 90]) | |||||
self.upper_blue = np.array([130, 255, 255]) | |||||
self.video = cv2.VideoCapture(0) | |||||
self.image = np.ndarray([]) | |||||
self.picture_counter = 0 | |||||
self.evaluate_picture = False | |||||
def get_frame(self) -> np.ndarray: | |||||
try: | |||||
_, self.image = self.video.read(0) | |||||
except Exception as err: | |||||
print("Can not capture the video..\n") | |||||
print(err) | |||||
return self.image | |||||
def set_take_picture(self, take: bool): | |||||
self.evaluate_picture = take | |||||
def take_picture(self) -> None: | |||||
file_name = 'score_round'+ str(self.picture_counter) + '.png' | |||||
cv2.imwrite(file_name, self.image) | |||||
self.picture_counter = self.picture_counter + 1 | |||||
self.set_take_picture(False) | |||||
def detect_color(self, image): | |||||
hsv_img = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) | |||||
count_red, count_green, count_blue= 0,0,0 | |||||
results = [] | |||||
for i, color in enumerate(self.colors): | |||||
if i == 0: | |||||
lower = self.lower_red | |||||
upper = self.upper_red | |||||
elif i == 1: | |||||
lower = self.lower_green | |||||
upper = self.upper_green | |||||
elif i == 2: | |||||
lower = self.lower_blue | |||||
upper = self.upper_blue | |||||
mask = cv2.inRange(hsv_img, lower, upper) | |||||
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | |||||
center = None | |||||
count = 0 | |||||
for contour in contours: | |||||
if count < 3: | |||||
if 100 < cv2.contourArea(contour): | |||||
M = cv2.moments(contour) | |||||
if M["m00"] > 0: | |||||
cX = int(M["m10"] / M["m00"]) | |||||
cY = int(M["m01"] / M["m00"]) | |||||
center = (cX, cY) | |||||
count += 1 | |||||
x, y, w, h = cv2.boundingRect(contour) | |||||
cv2.rectangle(image, (x, y), (x + w, y + h), color, 2) | |||||
cv2.putText(image, self.color_names[i], (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2) | |||||
if i == 0: | |||||
count_red += 1 | |||||
elif i == 1: | |||||
count_green += 1 | |||||
elif i == 2: | |||||
count_blue += 1 | |||||
results.append(center) | |||||
current_score= {'score_red': count_red, | |||||
'score_green': count_green, | |||||
'score_blue': count_blue | |||||
} | |||||
return results, image, current_score | |||||
def determine_position(self,results, img_width): | |||||
positions = [] | |||||
for result in results: | |||||
if result is None: | |||||
position = 0 | |||||
else: | |||||
x = result[0] | |||||
if x < img_width / 3: | |||||
position = 3 | |||||
elif x < 2 * img_width / 3: | |||||
position = 2 | |||||
else: | |||||
position = 1 | |||||
positions.append(position) | |||||
return positions | |||||
def check_correct_field(self, correct_field: int): | |||||
pass | |||||
def current_score(self, scores: dict): | |||||
return scores | |||||
def main(): | |||||
my_camera = Camera() | |||||
while True: | |||||
frame = my_camera.get_frame() | |||||
results, image, current_score = my_camera.detect_color(frame) | |||||
if my_camera.evaluate_picture: | |||||
my_camera.take_picture() | |||||
my_camera.current_score(current_score) | |||||
cv2.putText(frame, f"Rot: {current_score['score_red']}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, my_camera.colors[0], 2) | |||||
cv2.putText(frame, f"Gruen: {current_score['score_green']}", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, my_camera.colors[1], 2) | |||||
cv2.putText(frame, f"Blau: {current_score['score_blue']}", (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 1, my_camera.colors[2], 2) | |||||
img_width = frame.shape[1] | |||||
positions = my_camera.determine_position(results, img_width) | |||||
for i, position in enumerate(positions): | |||||
cv2.putText(image, f"{my_camera.color_names[i]}: {position}", (10, 150 + 30 * i), | |||||
cv2.FONT_HERSHEY_SIMPLEX, 1, my_camera.colors[i], 2) | |||||
cv2.line(img=image, pt1=(img_width // 3, 0), pt2=(img_width // 3, frame.shape[0]), color=(0, 0, 0), thickness=2) | |||||
cv2.line(img=image, pt1=(2 * img_width // 3, 0), pt2=(2 * img_width // 3, frame.shape[0]), color=(0, 0, 0), thickness=2) | |||||
cv2.imshow("Farberkennung", frame) | |||||
print(current_score) | |||||
if cv2.waitKey(1) & 0xFF == ord('q'): | |||||
break | |||||
my_camera.video.release() | |||||
cv2.destroyAllWindows() | |||||
if __name__ == "__main__": | |||||
main() |
from Database.database import QuestionDataBase | |||||
import random | import random | ||||
from Database.database import QuestionDataBase | |||||
class Game: | class Game: |
from flask import Flask, jsonify, Response, request | from flask import Flask, jsonify, Response, request | ||||
from game import Game | from game import Game | ||||
from camera import Camera | |||||
app = Flask(__name__) | app = Flask(__name__) | ||||
my_game = Game() | my_game = Game() | ||||
my_camera = Camera() | |||||
@app.route('/question', methods=['GET']) | @app.route('/question', methods=['GET']) | ||||
def get_question(): | def get_question(): | ||||
@app.route('/scoreboard', methods=['GET']) | @app.route('/scoreboard', methods=['GET']) | ||||
def scoreboard(): | def scoreboard(): | ||||
my_game.set_scoreboard() | |||||
print(my_game.scoreboard) | print(my_game.scoreboard) | ||||
return jsonify(my_game.scoreboard) | return jsonify(my_game.scoreboard) | ||||
@app.route('/check', methods=['GET']) | @app.route('/check', methods=['GET']) | ||||
def check(): | def check(): | ||||
# my_camera.check_correct_field(my_game.field) | |||||
my_camera.set_take_picture(True) | |||||
return my_game.scoreboard | return my_game.scoreboard | ||||
@app.route('/reset', methods=['GET']) | @app.route('/reset', methods=['GET']) | ||||
def main(): | def main(): | ||||
## 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) | |||||