Browse Source

camera detection for correct field

master
David Engert 1 year ago
parent
commit
ddea6ea320

+ 1
- 1
ip adressen.txt View File

@@ -1,3 +1,3 @@
Bilal IP : 192.168.50.130
Bilal: 192.168.50.130
Youssef: 192.168.50.226
David: 192.168.50.79

+ 0
- 1
src_folder/BackEnd/CameraDetection/Track.py View File

@@ -1,4 +1,3 @@
import time
import numpy as np
import cv2


+ 88
- 100
src_folder/BackEnd/camera.py View File

@@ -1,5 +1,7 @@
import cv2
import numpy as np
import pandas as pd



class Camera():
@@ -17,32 +19,31 @@ class Camera():
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
self.start_process = False
self.correct_field_frame = 0
self.mean_error = []
self.scores = {'score_red': 0,
'score_green': 0,
'score_blue': 0
}

def get_frame(self) -> np.ndarray:
try:
_, self.image = self.video.read(0)
_, self.image = self.video.read(1)
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 take_picture(self):
path = 'src_folder/Game_Images/'
file_name = 'current_score.png'
image_path = path+file_name
cv2.imwrite(image_path, self.image)
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:
@@ -56,96 +57,83 @@ class Camera():
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
if 100 < cv2.contourArea(contour):
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

self.scores['score_red'] = count_red
self.scores['score_green'] = count_green
self.scores['score_blue'] = count_blue

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()

def range_of_interest(self, frame: np.ndarray, correct_field: int):
num_windows_in_y = 1
num_windows_in_x = 3

height, width, _ = frame.shape
roi_height = height/num_windows_in_y
roi_width = width/num_windows_in_x
images = []
for x in range(0,num_windows_in_y):
for y in range(0,num_windows_in_x):
tmp_image=frame[int(x*roi_height):int((x+1)*roi_height), int(y*roi_width):int((y+1)*roi_width)]
images.append(tmp_image)

correct_field_frame = images[correct_field]
return correct_field_frame
def reduce_errors(self, mean_error_list: list):
for team_color in self.current_score.keys():
df = pd.DataFrame(mean_error_list)
total = df[team_color].sum()
number_of_rows = len(df.index)
error = total/number_of_rows
self.current_score[team_color] = round(error)

def process(self):
my_camera = Camera()
square_error = []

while my_camera.start_process:
frame = my_camera.get_frame()
interested_area = my_camera.range_of_interest(frame, my_camera.correct_field_frame)
my_camera.detect_color(interested_area)
cv2.line(img=frame, pt1=(frame.shape[1]//3, 0), pt2=(frame.shape[1]//3, frame.shape[0]), color=(0, 0, 0), thickness=2)
cv2.line(img=frame, pt1=(2 * frame.shape[1]//3, 0), pt2=(2 * frame.shape[1]//3, frame.shape[0]), color=(0, 0, 0), thickness=2)
cv2.imshow("Kamera 1,2 oder 3", frame)

print(my_camera.scores)
square_error.append(my_camera.scores)
if not my_camera.start_process:
pass

if cv2.waitKey(1) & 0xFF == ord('q'):
my_camera.take_picture()
print(my_camera.scores)
break

my_camera.video.release()
cv2.destroyAllWindows()


# nur zum testen: my_camera.start_process auf True setzen und correct_field_frame zwischen 1 und 3 wählen
# if __name__ == "__main__":
# my_camera = Camera()
# my_camera.process()

if __name__ == "__main__":
main()

+ 3
- 8
src_folder/BackEnd/game.py View File

@@ -1,5 +1,5 @@
import random
from Database.database import QuestionDataBase
import random



@@ -10,7 +10,6 @@ class Game:
'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
@@ -20,13 +19,13 @@ class Game:
def set_teamsize(self, teamsize: int):
self.teamsize = teamsize
def add_score(self, current_score: dict):
def add_score(self, current_score: dict) -> dict:
for key in self.scoreboard.keys():
if key in current_score.keys():
self.scoreboard[key] = self.scoreboard[key] + current_score[key]
return self.scoreboard
def set_scoreboard(self, current_scores: dict):
def set_scoreboard(self, current_scores: dict) -> dict:
self.scoreboard = self.add_score(current_scores)
return self.scoreboard
@@ -51,7 +50,6 @@ class Game:
def shuffle_answeroptions(self):
answeroptions = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3']
keys = []
print(self.question)

for answer in answeroptions:
keys.append(self.question[answer])
@@ -74,6 +72,3 @@ class Game:

def final_result(self):
self.final_score = dict(sorted(self.scoreboard.items(), key=lambda x: x[1], reverse=True))




+ 9
- 8
src_folder/BackEnd/router.py View File

@@ -1,6 +1,7 @@
from flask import Flask, jsonify, Response, request
from game import Game
from camera import Camera
from game import Game
import time

app = Flask(__name__)

@@ -27,10 +28,13 @@ def 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
my_camera.start_process = True
my_camera.process()
time.sleep(5)
my_camera.correct_field_frame = my_game.field
my_game.set_scoreboard(my_camera.scores)
my_camera.start_process = False
return jsonify(my_game.scoreboard)

@app.route('/reset', methods=['GET'])
def reset():
@@ -56,8 +60,5 @@ def main():
# app.run(host='127.0.0.1', port=5555, debug=True)


if __name__ == '__main__':
main()



BIN
src_folder/Game_Images/current_score.png View File


Loading…
Cancel
Save