@@ -1,22 +1,24 @@ | |||
import cv2 | |||
import numpy as np | |||
from game import Game | |||
# 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)] | |||
color_names = ["Rot", "Gruen", "Blau"] | |||
# video cap | |||
# 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]) | |||
CURRENT_SCORES= {'score_red': 0, | |||
'score_green': 0, | |||
'score_blue': 0 | |||
@@ -82,37 +84,41 @@ def erkennung_farben(img): | |||
def ermittle_position(results, img_width): | |||
positions = [] | |||
# pos_to_num = {"Rechts": 3, | |||
# "Mitte": 2, | |||
# "Links": 1, | |||
# "Nicht gefunden": 0} | |||
for result in results: | |||
if result is None: | |||
position = "Nicht gefunden" | |||
position = 0 | |||
else: | |||
x = result[0] | |||
if x < img_width / 3: | |||
position = "Rechts" | |||
position = 3 | |||
elif x < 2 * img_width / 3: | |||
position = "Mitte" | |||
position = 2 | |||
else: | |||
position = "Links" | |||
position = 1 | |||
positions.append(position) | |||
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: | |||
# Einzelbild von der Videoquelle lesen | |||
ret, frame = video.read() | |||
ret, frame = video.read(0) | |||
# Fehlerbehandlung, wenn kein Bild gelesen werden kann | |||
if not ret: | |||
@@ -121,12 +127,14 @@ if __name__ == "__main__": | |||
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) | |||
# 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) | |||
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 | |||
img_width = frame.shape[1] | |||
@@ -141,13 +149,17 @@ if __name__ == "__main__": | |||
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) | |||
# Bild anzeigen | |||
# Bild anzeigen | |||
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'): | |||
break | |||
# Videoquelle und Fenster schließen | |||
# Videoquelle und Fenster schließen | |||
video.release() | |||
cv2.destroyAllWindows() | |||
if __name__ == "__main__": | |||
main() |
@@ -16,7 +16,7 @@ questions = [('Wie heißt der längste Fluss auf der Welt?', 'Amazonas', 'Nil', | |||
('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'), | |||
('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'), | |||
('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'), |
@@ -0,0 +1,151 @@ | |||
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() |
@@ -1,5 +1,6 @@ | |||
from Database.database import QuestionDataBase | |||
import random | |||
from Database.database import QuestionDataBase | |||
class Game: |
@@ -1,9 +1,11 @@ | |||
from flask import Flask, jsonify, Response, request | |||
from game import Game | |||
from camera import Camera | |||
app = Flask(__name__) | |||
my_game = Game() | |||
my_camera = Camera() | |||
@app.route('/question', methods=['GET']) | |||
def get_question(): | |||
@@ -20,12 +22,14 @@ def teamsize(): | |||
@app.route('/scoreboard', methods=['GET']) | |||
def scoreboard(): | |||
my_game.set_scoreboard() | |||
print(my_game.scoreboard) | |||
return jsonify(my_game.scoreboard) | |||
@app.route('/check', methods=['GET']) | |||
def check(): | |||
# my_camera.check_correct_field(my_game.field) | |||
my_camera.set_take_picture(True) | |||
return my_game.scoreboard | |||
@app.route('/reset', methods=['GET']) | |||
@@ -46,10 +50,10 @@ def set_winner(): | |||
def main(): | |||
## Router | |||
# app.run(host='192.168.50.79', port=5555, debug=True) | |||
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) | |||