repository to manage all files for 1_2_oder_3 interaction game for Inf2/2 Interaktionen SoSe23 from Engert, Caliskan and Bachiri
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

camera.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import cv2
  2. import numpy as np
  3. import pandas as pd
  4. class Camera():
  5. def __init__(self) -> None:
  6. self.colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0)]
  7. self.color_names = ["Rot", "Gruen", "Blau"]
  8. self.lower_red = np.array([80, 160, 150])
  9. self.upper_red = np.array([255, 255, 255])
  10. self.lower_green = np.array([40, 50, 160])
  11. self.upper_green = np.array([80, 255, 255])
  12. self.lower_blue = np.array([95, 180, 90])
  13. self.upper_blue = np.array([130, 255, 255])
  14. self.video = cv2.VideoCapture(0)
  15. self.start_process = False
  16. self.correct_field_frame = 0
  17. self.mean_error = []
  18. self.scores = {'score_red': 0,
  19. 'score_green': 0,
  20. 'score_blue': 0
  21. }
  22. def get_frame(self) -> np.ndarray:
  23. try:
  24. _, self.image = self.video.read(1)
  25. except Exception as err:
  26. print("Can not capture the video..\n")
  27. print(err)
  28. return self.image
  29. def take_picture(self):
  30. path = 'src_folder/Game_Images/'
  31. file_name = 'current_score.png'
  32. image_path = path+file_name
  33. cv2.imwrite(image_path, self.image)
  34. def detect_color(self, image):
  35. hsv_img = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
  36. count_red, count_green, count_blue= 0,0,0
  37. for i, color in enumerate(self.colors):
  38. if i == 0:
  39. lower = self.lower_red
  40. upper = self.upper_red
  41. elif i == 1:
  42. lower = self.lower_green
  43. upper = self.upper_green
  44. elif i == 2:
  45. lower = self.lower_blue
  46. upper = self.upper_blue
  47. mask = cv2.inRange(hsv_img, lower, upper)
  48. contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  49. for contour in contours:
  50. if 100 < cv2.contourArea(contour):
  51. x, y, w, h = cv2.boundingRect(contour)
  52. cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
  53. cv2.putText(image, self.color_names[i], (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
  54. if i == 0:
  55. count_red += 1
  56. elif i == 1:
  57. count_green += 1
  58. elif i == 2:
  59. count_blue += 1
  60. self.scores['score_red'] = count_red
  61. self.scores['score_green'] = count_green
  62. self.scores['score_blue'] = count_blue
  63. def current_score(self, scores: dict):
  64. return scores
  65. def range_of_interest(self, frame: np.ndarray, correct_field: int):
  66. num_windows_in_y = 1
  67. num_windows_in_x = 3
  68. height, width, _ = frame.shape
  69. roi_height = height/num_windows_in_y
  70. roi_width = width/num_windows_in_x
  71. images = []
  72. for x in range(0,num_windows_in_y):
  73. for y in range(0,num_windows_in_x):
  74. tmp_image=frame[int(x*roi_height):int((x+1)*roi_height), int(y*roi_width):int((y+1)*roi_width)]
  75. images.append(tmp_image)
  76. correct_field_frame = images[correct_field]
  77. return correct_field_frame
  78. def reduce_errors(self, mean_error_list: list):
  79. for team_color in self.current_score.keys():
  80. df = pd.DataFrame(mean_error_list)
  81. total = df[team_color].sum()
  82. number_of_rows = len(df.index)
  83. error = total/number_of_rows
  84. self.current_score[team_color] = round(error)
  85. def process(self):
  86. my_camera = Camera()
  87. square_error = []
  88. while my_camera.start_process:
  89. frame = my_camera.get_frame()
  90. interested_area = my_camera.range_of_interest(frame, my_camera.correct_field_frame)
  91. my_camera.detect_color(interested_area)
  92. cv2.line(img=frame, pt1=(frame.shape[1]//3, 0), pt2=(frame.shape[1]//3, frame.shape[0]), color=(0, 0, 0), thickness=2)
  93. 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)
  94. cv2.imshow("Kamera 1,2 oder 3", frame)
  95. print(my_camera.scores)
  96. square_error.append(my_camera.scores)
  97. if not my_camera.start_process:
  98. pass
  99. if cv2.waitKey(1) & 0xFF == ord('q'):
  100. my_camera.take_picture()
  101. print(my_camera.scores)
  102. break
  103. my_camera.video.release()
  104. cv2.destroyAllWindows()
  105. # nur zum testen: my_camera.start_process auf True setzen und correct_field_frame zwischen 1 und 3 wählen
  106. # if __name__ == "__main__":
  107. # my_camera = Camera()
  108. # my_camera.process()