|
|
|
|
|
|
|
|
import cv2 |
|
|
import cv2 |
|
|
import numpy as np |
|
|
import numpy as np |
|
|
|
|
|
import pandas as pd |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Camera(): |
|
|
class Camera(): |
|
|
|
|
|
|
|
|
self.upper_blue = np.array([130, 255, 255]) |
|
|
self.upper_blue = np.array([130, 255, 255]) |
|
|
|
|
|
|
|
|
self.video = cv2.VideoCapture(0) |
|
|
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: |
|
|
def get_frame(self) -> np.ndarray: |
|
|
try: |
|
|
try: |
|
|
_, self.image = self.video.read(0) |
|
|
|
|
|
|
|
|
_, self.image = self.video.read(1) |
|
|
except Exception as err: |
|
|
except Exception as err: |
|
|
print("Can not capture the video..\n") |
|
|
print("Can not capture the video..\n") |
|
|
print(err) |
|
|
print(err) |
|
|
return self.image |
|
|
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): |
|
|
def detect_color(self, image): |
|
|
hsv_img = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) |
|
|
hsv_img = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) |
|
|
count_red, count_green, count_blue= 0,0,0 |
|
|
count_red, count_green, count_blue= 0,0,0 |
|
|
results = [] |
|
|
|
|
|
|
|
|
|
|
|
for i, color in enumerate(self.colors): |
|
|
for i, color in enumerate(self.colors): |
|
|
if i == 0: |
|
|
if i == 0: |
|
|
|
|
|
|
|
|
upper = self.upper_blue |
|
|
upper = self.upper_blue |
|
|
|
|
|
|
|
|
mask = cv2.inRange(hsv_img, lower, upper) |
|
|
mask = cv2.inRange(hsv_img, lower, upper) |
|
|
|
|
|
|
|
|
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
|
|
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
|
|
center = None |
|
|
|
|
|
count = 0 |
|
|
|
|
|
|
|
|
|
|
|
for contour in contours: |
|
|
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): |
|
|
def current_score(self, scores: dict): |
|
|
return scores |
|
|
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() |
|
|
|