Refectored Workspace

This commit is contained in:
Mann Patel
2024-07-20 14:58:49 -06:00
parent 69d0507c3e
commit c9cd98a03a
22 changed files with 1896 additions and 111 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

2
.gitattributes vendored
View File

@@ -1,2 +0,0 @@
# Auto detect text files and perform LF normalization
* text=auto

3
.idea/.gitignore generated vendored
View File

@@ -1,3 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml

10
.idea/Project.iml generated
View File

@@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@@ -1,6 +0,0 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml generated
View File

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.11 (Project)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (Project)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated
View File

@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Project.iml" filepath="$PROJECT_DIR$/.idea/Project.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated
View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

BIN
Assets/Images/Bots.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

1192
Assets/Template.svg Normal file

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 54 KiB

BIN
Computer Vision/.DS_Store vendored Normal file

Binary file not shown.

160
Computer Vision/1.py Normal file
View File

@@ -0,0 +1,160 @@
import cv2
import numpy as np
from scipy.spatial import distance as dist, distance
import main1
class Robot:
def __init__(self, pos=None, team='-no team!-', ID='-no ID!-'):
self.pos = pos if pos is not None else []
self.team = team
self.ID = ID
self.circles = []
def add_marking(self, circle=None):
if circle is None:
circle = [0, 0, [0, 0, 0]]
self.circles.append(circle)
class Ball:
def __init__(self, pos=None):
self.pos = pos if pos is not None else []
# Initialize the ball with default position
ball = Ball()
# Initialize empty lists for robots and ID markings
robotList = []
robotMarks = []
def Color_Detection(blue, green, red):
if blue > 220 and green < 50 and red < 50:
return 'Blue'
if blue < 50 and green > 200 and red > 200:
return 'Yellow'
if blue > 200 and green < 50 and red > 200:
return 'Purple'
if blue < 50 and green > 220 and red < 50:
return 'Green'
if blue < 50 and green < 200 and red > 180:
return 'Orange'
return 'Unidentified'
def IdentifyCircles(img, circle):
global ball
x, y = int(circle[0]), int(circle[1])
blue, green, red = img[y, x, 0], img[y, x, 1], img[y, x, 2]
color = Color_Detection(blue, green, red)
# Debugging statements
print(f"Circle at ({x}, {y}) with BGR ({blue}, {green}, {red}) detected as {color}")
if color == 'Blue' or color == 'Yellow':
robotList.append(Robot([x, y], color))
elif color == 'Green' or color == 'Purple':
robotMarks.append([x, y, color])
print('ROBOT FOUND')
elif color == 'Orange':
ball.pos = [x, y]
print(f"Ball found at ({x}, {y})")
def assignIDmarks():
if robotList is not None:
for idx, robot in enumerate(robotList):
distances = []
for i, mark in enumerate(robotMarks):
mark_dist = distance.euclidean(mark[:2], robot.pos)
distances.append((i, mark_dist))
distances.sort(key=lambda x: x[1])
closest_marks_indices = [i for i, _ in distances[:4]]
robot.circles = [robotMarks[i] for i in closest_marks_indices]
robot.ID = idx + 1
def detect_circles(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (9, 9), 0)
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, minDist=20, param1=50, param2=14, minRadius=15,
maxRadius=50)
return [circles]
def annotate_image(img):
for robot in robotList:
team_color = "B" if robot.team == 'Blue' else "Y"
cv2.putText(img, f'{team_color}', (robot.pos[0] + 20, robot.pos[1] - 40), cv2.FONT_HERSHEY_SIMPLEX, .75,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(img, f'ID{robot.ID}', (robot.pos[0] + 20, robot.pos[1] - 20), cv2.FONT_HERSHEY_SIMPLEX, .75,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(img, f'{robot.pos}', (robot.pos[0] + 20, robot.pos[1]), cv2.FONT_HERSHEY_SIMPLEX, .75,
(255, 255, 255), 2, cv2.LINE_AA)
if ball.pos:
cv2.putText(img, f'Ball {ball.pos}', (ball.pos[0] + 20, ball.pos[1] + 20), cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.circle(img, (ball.pos[0], ball.pos[1]), 10, (0, 165, 255), -1) # Orange color for the ball
# Main function
def main():
global robotList, robotMarks
global ball
while True:
# Initialize globals
robotList = []
robotMarks = []
ball = Ball() # Ensure ball is always an instance of Ball
# Load and process the image
imgpath = "Assets/Images/BotsAndBall.png"
img = cv2.imread(imgpath)
if img is None:
print(f"Failed to load image at path: {imgpath}")
return
cv2.imshow("Original Image", img)
# Detect circles in the image
circles = detect_circles(img)
if circles is not None:
circles = np.uint8(np.around(circles))
for circle in circles[0, :]:
IdentifyCircles(img, circle)
cv2.circle(img, (circle[0], circle[1]), circle[2], (0, 255, 0), 2)
cv2.circle(img, (circle[0], circle[1]), 2, (0, 0, 255), 3)
assignIDmarks()
for robot in robotList:
print(f'There is a {robot.team} robot with these ID circles:')
for mark in robot.circles:
print(mark)
if ball.pos:
print(f'Ball found at {ball.pos}')
for robot in robotList:
if robot.pos:
cv2.circle(img, (robot.pos[0], robot.pos[1]), 10, (0, 0, 0), 5)
for mark in robot.circles:
cv2.circle(img, (mark[0], mark[1]), 10, (0, 0, 0), 5)
else:
print("No circles detected")
annotate_image(img)
cv2.imshow("Annotated Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
main()

163
Computer Vision/2.py Normal file
View File

@@ -0,0 +1,163 @@
import cv2
import numpy as np
from scipy.spatial import distance as dist, distance
class Robot:
def __init__(self, pos=None, team='-no team!-', ID='-no ID!-'):
self.pos = pos if pos is not None else []
self.team = team
self.ID = ID
self.circles = []
def add_marking(self, circle=None):
if circle is None:
circle = [0, 0, [0, 0, 0]]
self.circles.append(circle)
class Ball:
def __init__(self, pos=None):
self.pos = pos if pos is not None else []
# Initialize the ball with default position
ball = Ball()
# Initialize empty lists for robots and ID markings
robotList = []
robotMarks = []
def Color_Detection(blue, green, red):
if blue > 220 and green < 50 and red < 50:
return 'Blue'
if blue < 50 and green > 200 and red > 200:
return 'Yellow'
if blue > 200 and green < 50 and red > 200:
return 'Purple'
if blue < 50 and green > 220 and red < 50:
return 'Green'
if blue <= 50 and green <= 200 and red >= 180:
return 'Orange'
return 'Unidentified'
def IdentifyCircles(img, circle):
global ball
x, y = int(circle[0]), int(circle[1])
blue, green, red = img[y, x, 0], img[y, x, 1], img[y, x, 2]
color = Color_Detection(blue, green, red)
# Debugging statements
print(f"Circle at ({x}, {y}) with BGR ({blue}, {green}, {red}) detected as {color}")
if color == 'Blue' or color == 'Yellow':
robotList.append(Robot([x, y], color))
elif color == 'Green' or color == 'Purple':
robotMarks.append([x, y, color])
print('ROBOT FOUND')
elif color == 'Orange':
ball.pos = [x, y]
print(f"Ball found at ({x}, {y})")
def assignIDmarks():
if robotList is not None:
for idx, robot in enumerate(robotList):
distances = []
for i, mark in enumerate(robotMarks):
mark_dist = distance.euclidean(mark[:2], robot.pos)
distances.append((i, mark_dist))
distances.sort(key=lambda x: x[1])
closest_marks_indices = [i for i, _ in distances[:4]]
robot.circles = [robotMarks[i] for i in closest_marks_indices]
robot.ID = idx + 1
def detect_circles(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (9, 9), 0)
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, minDist=20, param1=50, param2=14, minRadius=15,
maxRadius=50)
return circles
def annotate_image(img):
for robot in robotList:
team_color = "B" if robot.team == 'Blue' else "Y"
cv2.putText(img, f'{team_color}', (robot.pos[0] + 20, robot.pos[1] - 40), cv2.FONT_HERSHEY_SIMPLEX, .75,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(img, f'ID{robot.ID}', (robot.pos[0] + 20, robot.pos[1] - 20), cv2.FONT_HERSHEY_SIMPLEX, .75,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(img, f'{robot.pos}', (robot.pos[0] + 20, robot.pos[1]), cv2.FONT_HERSHEY_SIMPLEX, .75,
(255, 255, 255), 2, cv2.LINE_AA)
if ball.pos:
cv2.putText(img, f'Ball {ball.pos}', (ball.pos[0] + 20, ball.pos[1] + 20), cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.circle(img, (ball.pos[0], ball.pos[1]), 10, (0, 165, 255), -1) # Orange color for the ball
# Main function
def main():
global robotList, robotMarks
global ball
# Initialize globals
robotList = []
robotMarks = []
ball = Ball() # Ensure ball is always an instance of Ball
# Load and process the image
imgpath = "Assets/Images/BotsAndBall.png"
img = cv2.imread(imgpath)
if img is None:
print(f"Failed to load image at path: {imgpath}")
return
cv2.imshow("Original Image", img)
# Detect circles in the image
circles = detect_circles(img)
if circles is not None:
circles = np.uint16(np.around(circles))
for circle in circles[0, :]:
IdentifyCircles(img, circle)
cv2.circle(img, (circle[0], circle[1]), circle[2], (0, 255, 0), 2)
cv2.circle(img, (circle[0], circle[1]), 2, (0, 0, 255), 3)
assignIDmarks()
for robot in robotList:
print(f'There is a {robot.team} robot with these ID circles:')
for mark in robot.circles:
print(mark)
if ball.pos:
print(f'Ball found at {ball.pos}')
for robot in robotList:
if robot.pos:
cv2.circle(img, (robot.pos[0], robot.pos[1]), 10, (0, 0, 0), 5)
for mark in robot.circles:
cv2.circle(img, (mark[0], mark[1]), 10, (0, 0, 0), 5)
else:
print("No circles detected")
annotate_image(img)
cv2.imshow("Annotated Image", img)
# Use cv2.waitKey() to display the window until a key is pressed
while True:
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
cv2.destroyAllWindows()
if __name__ == "__main__":
main()

160
Computer Vision/3.py Normal file
View File

@@ -0,0 +1,160 @@
import cv2
import numpy as np
from scipy.spatial import distance as dist, distance
class Robot:
def __init__(self, pos=None, team='-no team!-', ID='-no ID!-'):
self.pos = pos if pos is not None else []
self.team = team
self.ID = ID
self.circles = []
def add_marking(self, circle=None):
if circle is None:
circle = [0, 0, [0, 0, 0]]
self.circles.append(circle)
class Ball:
def __init__(self, pos=None):
self.pos = pos if pos is not None else []
ball = Ball()
robotList = []
robotMarks = []
def Color_Detection(blue, green, red):
if blue > 220 and green < 50 and red < 50:
return 'Blue'
if blue < 50 and green > 200 and red > 200:
return 'Yellow'
if blue > 200 and green < 50 and red > 200:
return 'Purple'
if blue < 50 and green > 220 and red < 50:
return 'Green'
if blue < 50 and green < 200 and red > 180:
return 'Orange'
return 'Unidentified'
def IdentifyCircles(img, circle):
global ball
x, y = int(circle[0]), int(circle[1])
blue, green, red = img[y, x, 0], img[y, x, 0], img[y, x, 0]
color = Color_Detection(blue, green, red)
if color == 'Blue' or color == 'Yellow':
robotList.append(Robot([x, y], color))
elif color == 'Green' or color == 'Purple':
robotMarks.append([x, y, color])
print('ROBOT FOUND')
elif color == 'Orange':
ball.pos = [x, y]
print(f"Ball found at ({x}, {y})")
def assignIDmarks():
if robotList is not None:
for idx, robot in enumerate(robotList):
distances = []
for i, mark in enumerate(robotMarks):
mark_dist = distance.euclidean(mark[:2], robot.pos)
distances.append((i, mark_dist))
distances.sort(key=lambda x: x[1])
closest_marks_indices = [i for i, _ in distances[:4]]
robot.circles = [robotMarks[i] for i in closest_marks_indices]
robot.ID = idx + 1
def detect_circles(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (9, 9), 0)
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, minDist=20, param1=50, param2=14, minRadius=1,
maxRadius=99)
return circles
def annotate_image(img):
for robot in robotList:
team_color = "B" if robot.team == 'Blue' else "Y"
cv2.putText(img, f'{team_color}', (robot.pos[0] + 20, robot.pos[1] - 40), cv2.FONT_HERSHEY_SIMPLEX, .75,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(img, f'ID{robot.ID}', (robot.pos[0] + 20, robot.pos[1] - 20), cv2.FONT_HERSHEY_SIMPLEX, .75,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(img, f'{robot.pos}', (robot.pos[0] + 20, robot.pos[1]), cv2.FONT_HERSHEY_SIMPLEX, .75,
(255, 255, 255), 2, cv2.LINE_AA)
if ball.pos:
cv2.putText(img, f'Ball {ball.pos}', (ball.pos[0] + 20, ball.pos[1] + 20), cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.circle(img, (ball.pos[0], ball.pos[1]), 10, (0, 165, 255), -1) # Orange color for the ball
# Main function
def main():
global robotList, robotMarks
global ball
# Open the video file
video_path = "Assets/Images/BotsAndBall.png"
cap = cv2.VideoCapture(1)
if not cap.isOpened():
print(f"Failed to open video file: {video_path}")
return
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Initialize globals for each frame
robotList = []
robotMarks = []
ball = Ball() # Ensure ball is always an instance of Ball
# Detect circles in the frame
circles = detect_circles(frame)
if circles is not None:
circles = np.uint16(np.around(circles))
for circle in circles[0, :]:
IdentifyCircles(frame, circle)
cv2.circle(frame, (circle[0], circle[1]), circle[2], (0, 255, 0), 2)
cv2.circle(frame, (circle[0], circle[1]), 2, (0, 0, 255), 3)
assignIDmarks()
for robot in robotList:
print(f'There is a {robot.team} robot with these ID circles:')
for mark in robot.circles:
print(mark)
if ball.pos:
print(f'Ball found at {ball.pos}')
for robot in robotList:
if robot.pos:
cv2.circle(frame, (robot.pos[0], robot.pos[1]), 10, (0, 0, 0), 5)
for mark in robot.circles:
cv2.circle(frame, (mark[0], mark[1]), 10, (0, 0, 0), 5)
else:
print("No circles detected")
annotate_image(frame)
cv2.imshow("Annotated Frame", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 355 KiB

Binary file not shown.

View File

@@ -28,15 +28,15 @@ ball = Ball()
def Color_Detection(blue, green, red):
if blue > 220 and green < 50 and red < 50:
return 'Blue'
if blue < 50 and green > 200 and red > 200:
elif blue < 50 and green > 200 and red > 200:
return 'Yellow'
if blue > 200 and green < 50 and red > 200:
elif blue > 200 and green < 50 and red > 200:
return 'Purple'
if blue < 50 and green > 220 and red < 50:
elif blue < 50 and green > 220 and red < 50:
return 'Green'
if blue < 50 and green < 200 and red > 220:
elif blue < 50 and green < 200 and red > 220:
return 'Orange'
return 'Unidentified'
return f'Unidentified Color R:{red}, G:{green}, B:{blue}'
def IdentifyCircles(img, circle):
global ball
@@ -49,7 +49,6 @@ def IdentifyCircles(img, circle):
robotList.append(Robot([x, y], color))
elif color == 'Green' or color == 'Purple':
robotMarks.append([x, y, color])
print('ROBOT FOUND')
elif color == 'Orange':
ball = Ball([x, y])
@@ -76,8 +75,8 @@ def annotate_image(img):
for robot in robotList:
team_color = "B" if robot.team == 'Blue' else "Y"
cv2.putText(img, f'{team_color}', (robot.pos[0] + 20, robot.pos[1] - 40), cv2.FONT_HERSHEY_SIMPLEX, .75, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(img, f'ID{robot.ID}', (robot.pos[0] + 20, robot.pos[1] - 20), cv2.FONT_HERSHEY_SIMPLEX, .75, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(img, f'{robot.pos}', (robot.pos[0] + 20, robot.pos[1]), cv2.FONT_HERSHEY_SIMPLEX, .75, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(img, f'\nID{robot.ID}', (robot.pos[0] + 20, robot.pos[1] - 20), cv2.FONT_HERSHEY_SIMPLEX, .75, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(img, f'{robot.pos }', (robot.pos[0] + 20, robot.pos[1]), cv2.FONT_HERSHEY_SIMPLEX, .75, (255, 255, 255), 2, cv2.LINE_AA)
if ball:
cv2.putText(img, f'Ball {ball.pos}', (ball.pos[0] + 20, ball.pos[1] + 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
@@ -92,7 +91,7 @@ def main():
ball = None
# Load and process the video
video_path = "/Users/mannpatel/Desktop/Project/Computer Vision/Test2.mp4"
video_path = "/Users/mannpatel/Desktop/Robocup/Assets/Video/Test2.mp4"
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
@@ -116,10 +115,10 @@ def main():
assignIDmarks()
for robot in robotList:
print(f'There is a {robot.team} robot with these ID circles:')
for mark in robot.circles:
print(mark)
# for robot in robotList:
# print(f'There is a {robot.team} robot with these ID circles:')
# for mark in robot.circles:
# print(mark)
if ball:
print(f'Ball found at {ball.pos}')

View File

@@ -1,50 +1,202 @@
# import cv2
# import numpy as np
# from scipy.spatial import distance as dist, distance
#
# class Robot:
# def __init__(self, pos=None, team='-no team!-', ID='-no ID!-'):
# self.pos = pos if pos is not None else []
# self.team = team
# self.ID = ID
# self.circles = []
#
# def add_marking(self, circle=None):
# if circle is None:
# circle = [0, 0, [0, 0, 0]]
# self.circles.append(circle)
#
# class Ball:
# def __init__(self, pos=None):
# self.pos = pos if pos is not None else []
#
# ball = Ball()
# robotList = []
# robotMarks = []
#
# def Color_Detection(blue, green, red):
# if blue >= 220 and green <= 50 and red <= 50:
# return 'Blue'
# if blue <= 50 and green >= 200 and red >= 200:
# return 'Yellow'
# if blue >= 200 and green <= 50 and red >= 200:
# return 'Purple'
# if blue <= 50 and green >= 220 and red <= 50:
# return 'Green'
# if blue <= 50 and green <= 200 and red >= 180:
# return 'Orange'
# return 'Unidentified'
#
#
# def IdentifyCircles(img, circle):
# global ball
#
# x, y = int(circle[0]), int(circle[1])
# blue, green, red = img[y, x, 0], img[y, x, 1], img[y, x, 2]
# color = Color_Detection(blue, green, red)
#
# if color == 'Blue' or color == 'Yellow':
# robotList.append(Robot([x, y], color))
# elif color == 'Green' or color == 'Purple':
# robotMarks.append([x, y, color])
# print('ROBOT FOUND')
# elif color == 'Orange':
# ball.pos = [x, y]
# print(f"Ball found at ({x}, {y})")
#
#
# def assignIDmarks():
# if robotList is not None:
# for idx, robot in enumerate(robotList):
# distances = []
#
# for i, mark in enumerate(robotMarks):
# mark_dist = distance.euclidean(mark[:2], robot.pos)
# distances.append((i, mark_dist))
# distances.sort(key=lambda x: x[1])
# closest_marks_indices = [i for i, _ in distances[:4]]
# robot.circles = [robotMarks[i] for i in closest_marks_indices]
# robot.ID = idx + 1
#
#
# def detect_circles(image):
# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# blurred = cv2.GaussianBlur(gray, (9, 9), 0)
# circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, minDist=20, param1=50, param2=14, minRadius=15,
# maxRadius=50)
# return circles
#
#
# def annotate_image(img):
# for robot in robotList:
# team_color = "B" if robot.team == 'Blue' else "Y"
# sting = f'Team: {team_color} | ID: {robot.ID} | POS: {robot.pos}'
# cv2.putText(img, sting, (robot.pos[0] + 20, robot.pos[1] - 40), cv2.FONT_HERSHEY_SIMPLEX, .75,
# (255, 255, 255), 2, cv2.LINE_AA)
#
# if ball.pos:
# cv2.putText(img, f'Ball {ball.pos}', (ball.pos[0] + 20, ball.pos[1] + 20), cv2.FONT_HERSHEY_SIMPLEX, 1,
# (255, 255, 255), 2, cv2.LINE_AA)
#
# # Main function
# def main():
# global robotList, robotMarks
# global ball
#
# while True:
# # Initialize globals
# robotList = []
# robotMarks = []
# ball = Ball() # Ensure ball is always an instance of Ball
#
# # Load and process the image
# imgpath = "/Users/mannpatel/Desktop/Project/Computer Vision/Test2.jpeg"
# img = cv2.imread(imgpath)
# if img is None:
# print(f"Failed to load image at path: {imgpath}")
# return
#
# cv2.imshow("Original Image", img)
#
# # Detect circles in the image
# circles = detect_circles(img)
#
# if circles is not None:
# circles = np.uint16(np.around(circles))
# for circle in circles[0, :]:
# IdentifyCircles(img, circle)
# cv2.circle(img, (circle[0], circle[1]), circle[2], (0, 255, 0), 2)
# cv2.circle(img, (circle[0], circle[1]), 2, (0, 0, 255), 3)
# assignIDmarks()
#
# for robot in robotList:
# print(f'There is a {robot.team} robot with these ID circles:')
# for mark in robot.circles:
# print(mark)
#
# if ball.pos:
# print(f'Ball found at {ball.pos}')
#
# for robot in robotList:
# if robot.pos:
# cv2.circle(img, (robot.pos[0], robot.pos[1]), 10, (0, 0, 0), 5)
# for mark in robot.circles:
# cv2.circle(img, (mark[0], mark[1]), 10, (0, 0, 0), 5)
#
# else:
# print("No circles detected")
#
# annotate_image(img)
# cv2.imshow("Annotated Image", img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
#
#
# if __name__ == "__main__":
# main()
import cv2
import numpy as np
from scipy.spatial import distance as dist, distance
import copy
# Initialize empty lists for robots and ID markings
robotList = []
robotMarks = []
class Robot:
def __init__(self, pos=None, team='-no team!-', ID='-no ID!-'):
# Initialize with default empty list if pos is not provided
self.pos = pos if pos is not None else []
self.team = team
self.ID = ID
self.circles = [] # ID markings [x, y, color]
self.circles = []
def add_marking(self, circle=None):
# Initialize with default circle if none is provided
if circle is None:
circle = [0, 0, [0, 0, 0]]
self.circles.append(circle)
class Ball:
def __init__(self, pos=None):
# Initialize with default empty list if pos is not provided
self.pos = pos if pos is not None else []
# Initialize the ball with default position
ball = Ball()
# Initialize empty lists for robots and ID markings
robotList = []
robotMarks = []
def Color_Detection(blue, green, red):
if blue > 220 and green < 50 and red < 50:
if blue >= 220 and green <= 50 and red <= 50:
return 'Blue'
if blue < 50 and green > 200 and red > 200:
if blue <= 50 and green >= 200 and red >= 200:
return 'Yellow'
if blue > 200 and green < 50 and red > 200:
if blue >= 200 and green <= 50 and red >= 200:
return 'Purple'
if blue < 50 and green > 220 and red < 50:
if blue <= 50 and green >= 220 and red <= 50:
return 'Green'
if blue < 50 and green < 200 and red > 220:
if blue <= 50 and green <= 200 and red >= 220:
return 'Orange'
return 'Unidentified'
# def Color_Detection(blue, green, red):
# if blue == 246 and green == 0 and red == 0:
# return 'Blue'
# if blue <= 0 and green >= 250 and red > 350:
# return 'Yellow'
# if blue == 247 and green == 51 and red == 235:
# return 'Purple'
# if blue == 77 and green == 252 and red == 118:
# return 'Green'
# if blue == 50 and green == 113 and red == 228:
# return 'Orange'
# return 'Unidentified'
def IdentifyCircles(img, circle):
global ball
@@ -53,13 +205,17 @@ def IdentifyCircles(img, circle):
blue, green, red = img[y, x, 0], img[y, x, 1], img[y, x, 2]
color = Color_Detection(blue, green, red)
# Debugging statements
print(f"Circle at ({x}, {y}) with BGR ({blue}, {green}, {red}) detected as {color}")
if color == 'Blue' or color == 'Yellow':
robotList.append(Robot([x, y], color))
elif color == 'Green' or color == 'Purple':
robotMarks.append([x, y, color])
print('ROBOT FOUND')
elif color == 'Orange':
ball = Ball([x, y])
ball.pos = [x, y]
print(f"Ball found at ({x}, {y})")
def assignIDmarks():
@@ -79,32 +235,44 @@ def assignIDmarks():
def detect_circles(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (9, 9), 0)
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, minDist=20, param1=50, param2=14, minRadius=15, maxRadius=25)
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, minDist=20, param1=50, param2=14, minRadius=15,
maxRadius=50)
return circles
def annotate_image(img):
for robot in robotList:
team_color = "B" if robot.team == 'Blue' else "Y"
cv2.putText(img, f'{team_color}', (robot.pos[0] + 20, robot.pos[1] - 40), cv2.FONT_HERSHEY_SIMPLEX, .75, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(img, f'ID{robot.ID}', (robot.pos[0] + 20, robot.pos[1] - 20), cv2.FONT_HERSHEY_SIMPLEX, .75, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(img, f'{robot.pos}', (robot.pos[0] + 20, robot.pos[1]), cv2.FONT_HERSHEY_SIMPLEX, .75, (255, 255, 255), 2, cv2.LINE_AA)
# Assuming orientation and other details are part of robot attributes
cv2.putText(img, f'{team_color}', (robot.pos[0] + 20, robot.pos[1] - 40), cv2.FONT_HERSHEY_SIMPLEX, .75,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(img, f'ID{robot.ID}', (robot.pos[0] + 20, robot.pos[1] - 20), cv2.FONT_HERSHEY_SIMPLEX, .75,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(img, f'{robot.pos}', (robot.pos[0] + 20, robot.pos[1]), cv2.FONT_HERSHEY_SIMPLEX, .75,
(255, 255, 255), 2, cv2.LINE_AA)
if ball.pos:
cv2.putText(img, f'Ball {ball.pos}', (ball.pos[0] + 20, ball.pos[1] + 20), cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.circle(img, (ball.pos[0], ball.pos[1]), 10, (0, 165, 255), -1) # Orange color for the ball
if ball:
cv2.putText(img, f'Ball {ball.pos}', (ball.pos[0] + 20, ball.pos[1] + 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
# Main function
def main():
global robotList, robotMarks, ball
global robotList, robotMarks
global ball
# Initialize globals
robotList = []
robotMarks = []
ball = None
ball = Ball() # Ensure ball is always an instance of Ball
# Load and process the image
imgpath = "/Users/mannpatel/Desktop/Project/Template.jpg"
imgpath = "/Users/mannpatel/Desktop/Robocup/Computer Vision/Template1.png"
img = cv2.imread(imgpath)
if img is None:
print(f"Failed to load image at path: {imgpath}")
return
cv2.imshow("Original Image", img)
# Detect circles in the image
@@ -120,24 +288,31 @@ def main():
assignIDmarks()
for robot in robotList:
print(f'There is a {robot.team} robot with these ID circles:')
print(f'There is a {robot.team} robot with these ID {robot.ID}')
for mark in robot.circles:
print(mark)
if ball:
if ball.pos:
print(f'Ball found at {ball.pos}')
for robot in robotList:
cv2.circle(img, (robot.pos[0], robot.pos[1]), 10, (0, 0, 0), 5)
for mark in robot.circles:
cv2.circle(img, (mark[0], mark[1]), 10, (0, 0, 0), 5)
if robot.pos:
cv2.circle(img, (robot.pos[0], robot.pos[1]), 10, (0, 0, 0), 5)
for mark in robot.circles:
cv2.circle(img, (mark[0], mark[1]), 10, (0, 0, 0), 5)
else:
print("No circles detected")
annotate_image(img)
cv2.imshow("Annotated Image", img)
cv2.waitKey(0)
# Use cv2.waitKey() to display the window until a key is pressed
while True:
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
cv2.destroyAllWindows()

View File

@@ -1,22 +0,0 @@
import dearpygui.dearpygui as dpg
dpg.create_context()
with dpg.window(label="Example Window"):
dpg.add_text("Hello, world")
dpg.add_button(label="Save")
dpg.add_input_text(label="string", default_value="Quick brown fox")
dpg.add_slider_float(label="float", default_value=0.273, max_value=1)
dpg.create_viewport(title='Custom Title', width=600, height=200)
dpg.setup_dearpygui()
dpg.show_viewport()
# below replaces, start_dearpygui()
while dpg.is_dearpygui_running():
# insert here any code you would like to run in the render loop
# you can manually stop by using stop_dearpygui()
print("this will run every frame")
dpg.render_dearpygui_frame()
dpg.destroy_context()