Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Module_10_Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ştefan, R.P. (Răzvan, Student B-TCS)
Module_10_Project
Commits
4e4908e7
Commit
4e4908e7
authored
4 months ago
by
Rainer Lourens
Browse files
Options
Downloads
Patches
Plain Diff
cube drawing finalized
parent
34406627
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
__pycache__/cubeplot.cpython-310.pyc
+0
-0
0 additions, 0 deletions
__pycache__/cubeplot.cpython-310.pyc
cubeplot.py
+30
-13
30 additions, 13 deletions
cubeplot.py
main.py
+16
-109
16 additions, 109 deletions
main.py
with
46 additions
and
122 deletions
__pycache__/cubeplot.cpython-310.pyc
0 → 100644
+
0
−
0
View file @
4e4908e7
File added
This diff is collapsed.
Click to expand it.
tes
t.py
→
cubeplo
t.py
+
30
−
13
View file @
4e4908e7
...
...
@@ -15,11 +15,12 @@ def draw_face(ax, origin, normal, colors):
for
i
in
range
(
rows
):
for
j
in
range
(
cols
):
x_offset
=
j
*
step
y_offset
=
i
*
step
color
=
colors
[
i
][
j
]
x_offset
=
(
cols
-
1
-
j
)
*
step
y_offset
=
(
rows
-
1
-
i
)
*
step
if
normal
==
[
0
,
0
,
1
]:
# XY plane (Top)
x_offset
=
(
cols
-
1
-
j
)
*
step
# Standard column order
y_offset
=
i
*
step
vertices
=
[
[
origin
[
0
]
+
x_offset
,
origin
[
1
]
+
y_offset
,
origin
[
2
]],
[
origin
[
0
]
+
x_offset
+
step
,
origin
[
1
]
+
y_offset
,
origin
[
2
]],
...
...
@@ -34,6 +35,8 @@ def draw_face(ax, origin, normal, colors):
[
origin
[
0
]
+
x_offset
,
origin
[
1
]
+
y_offset
+
step
,
origin
[
2
]]
]
elif
normal
==
[
0
,
1
,
0
]:
# XZ plane (Front)
x_offset
=
(
cols
-
1
-
j
)
*
step
# Reverse column order
y_offset
=
(
rows
-
1
-
i
)
*
step
# Reverse row order
vertices
=
[
[
origin
[
0
]
+
x_offset
,
origin
[
1
],
origin
[
2
]
+
y_offset
],
[
origin
[
0
]
+
x_offset
+
step
,
origin
[
1
],
origin
[
2
]
+
y_offset
],
...
...
@@ -41,6 +44,7 @@ def draw_face(ax, origin, normal, colors):
[
origin
[
0
]
+
x_offset
,
origin
[
1
],
origin
[
2
]
+
y_offset
+
step
]
]
elif
normal
==
[
0
,
-
1
,
0
]:
# XZ plane (Back)
x_offset
=
j
*
step
vertices
=
[
[
origin
[
0
]
+
x_offset
,
origin
[
1
],
origin
[
2
]
+
y_offset
],
[
origin
[
0
]
+
x_offset
+
step
,
origin
[
1
],
origin
[
2
]
+
y_offset
],
...
...
@@ -48,6 +52,7 @@ def draw_face(ax, origin, normal, colors):
[
origin
[
0
]
+
x_offset
,
origin
[
1
],
origin
[
2
]
+
y_offset
+
step
]
]
elif
normal
==
[
1
,
0
,
0
]:
# YZ plane (Right)
x_offset
=
j
*
step
vertices
=
[
[
origin
[
0
],
origin
[
1
]
+
x_offset
,
origin
[
2
]
+
y_offset
],
[
origin
[
0
],
origin
[
1
]
+
x_offset
+
step
,
origin
[
2
]
+
y_offset
],
...
...
@@ -86,6 +91,20 @@ def draw_cube(state):
for
face
,
(
origin
,
normal
)
in
face_definitions
.
items
():
draw_face
(
ax
,
origin
,
normal
,
state
[
face
])
# Add text at the center of each face
if
normal
==
[
0
,
0
,
1
]:
# Top
ax
.
text
(
0
,
0
,
0.7
,
face
,
color
=
'
black
'
,
fontsize
=
12
,
ha
=
'
center
'
,
va
=
'
center
'
)
elif
normal
==
[
0
,
0
,
-
1
]:
# Bottom
ax
.
text
(
0
,
0
,
-
0.7
,
face
,
color
=
'
black
'
,
fontsize
=
12
,
ha
=
'
center
'
,
va
=
'
center
'
)
elif
normal
==
[
0
,
1
,
0
]:
# Front
ax
.
text
(
0
,
0.7
,
0
,
face
,
color
=
'
black
'
,
fontsize
=
12
,
ha
=
'
center
'
,
va
=
'
center
'
)
elif
normal
==
[
0
,
-
1
,
0
]:
# Back
ax
.
text
(
0
,
-
0.7
,
0
,
face
,
color
=
'
black
'
,
fontsize
=
12
,
ha
=
'
center
'
,
va
=
'
center
'
)
elif
normal
==
[
1
,
0
,
0
]:
# Right
ax
.
text
(
0.7
,
0
,
0
,
face
,
color
=
'
black
'
,
fontsize
=
12
,
ha
=
'
center
'
,
va
=
'
center
'
)
elif
normal
==
[
-
1
,
0
,
0
]:
# Left
ax
.
text
(
-
0.7
,
0
,
0
,
face
,
color
=
'
black
'
,
fontsize
=
12
,
ha
=
'
center
'
,
va
=
'
center
'
)
# Set the aspect of the plot to be equal
ax
.
set_box_aspect
([
1
,
1
,
1
])
# Aspect ratio is 1:1:1
...
...
@@ -101,13 +120,11 @@ def draw_cube(state):
# Display the cube
plt
.
show
()
if
__name__
==
"
__main__
"
:
cube_state
=
{
'
Front
'
:
[[
'
red
'
,
'
blue
'
,
'
blue
'
],
[
'
white
'
,
'
white
'
,
'
white
'
],
[
'
white
'
,
'
red
'
,
'
blue
'
]],
'
Top
'
:
[[
'
red
'
,
'
blue
'
,
'
blue
'
],
[
'
white
'
,
'
white
'
,
'
white
'
],
[
'
white
'
,
'
red
'
,
'
blue
'
]],
'
Bottom
'
:
[[
'
white
'
,
'
blue
'
,
'
white
'
],
[
'
white
'
,
'
white
'
,
'
blue
'
],
[
'
white
'
,
'
red
'
,
'
blue
'
]],
'
Left
'
:
[[
'
red
'
,
'
blue
'
,
'
blue
'
],
[
'
white
'
,
'
blue
'
,
'
white
'
],
[
'
white
'
,
'
red
'
,
'
blue
'
]],
'
Right
'
:
[[
'
red
'
,
'
blue
'
,
'
blue
'
],
[
'
white
'
,
'
blue
'
,
'
white
'
],
[
'
white
'
,
'
red
'
,
'
blue
'
]],
'
Back
'
:
[[
'
red
'
,
'
blue
'
,
'
blue
'
],
[
'
white
'
,
'
blue
'
,
'
white
'
],
[
'
white
'
,
'
red
'
,
'
blue
'
]]
}
draw_cube
(
cube_state
)
# cube = {'Front': [['red', 'red', 'white'], ['white', 'white', 'red'], ['red', 'red', 'red']],
# 'Top': [['white', 'blue', 'white'], ['white', 'white', 'red'], ['white', 'white', 'red']],
# 'Bottom': [['white', 'blue', 'white'], ['red', 'blue', 'red'], ['red', 'white', 'white']],
# 'Left': [['white', 'white', 'red'], ['white', 'red', 'white'], ['white', 'white', 'red']],
# 'Right': [['white', 'red', 'white'], ['red', 'red', 'blue'], ['blue', 'blue', 'blue']],
# 'Back': [['white', 'yellow', 'red'], ['white', 'yellow', 'blue'], ['blue', 'white', 'blue']]}
# draw_cube(cube)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
main.py
+
16
−
109
View file @
4e4908e7
import
matplotlib.pyplot
as
plt
from
mpl_toolkits.mplot3d.art3d
import
Poly3DCollection
from
cubeplot
import
draw_cube
,
draw_face
import
numpy
as
np
from
ultralytics
import
YOLO
import
cv2
...
...
@@ -28,105 +26,6 @@ color_map = {
'
orange
'
:
'
#FFA500
'
}
def
draw_face
(
ax
,
origin
,
normal
,
colors
):
"""
Draw a single face of the Rubik
'
s cube with 3x3 sub-squares.
:param ax: Matplotlib 3D axis
:param origin: Bottom-left corner of the face
:param normal: Normal vector of the face (to determine orientation)
:param colors: 3x3 grid of colors for the face
"""
rows
,
cols
=
len
(
colors
),
len
(
colors
[
0
])
step
=
1
/
rows
for
i
in
range
(
rows
):
for
j
in
range
(
cols
):
x_offset
=
j
*
step
y_offset
=
i
*
step
color
=
colors
[
i
][
j
]
if
normal
==
[
0
,
0
,
1
]:
# XY plane (Top)
vertices
=
[
[
origin
[
0
]
+
x_offset
,
origin
[
1
]
+
y_offset
,
origin
[
2
]],
[
origin
[
0
]
+
x_offset
+
step
,
origin
[
1
]
+
y_offset
,
origin
[
2
]],
[
origin
[
0
]
+
x_offset
+
step
,
origin
[
1
]
+
y_offset
+
step
,
origin
[
2
]],
[
origin
[
0
]
+
x_offset
,
origin
[
1
]
+
y_offset
+
step
,
origin
[
2
]]
]
elif
normal
==
[
0
,
0
,
-
1
]:
# XY plane (Bottom)
vertices
=
[
[
origin
[
0
]
+
x_offset
,
origin
[
1
]
+
y_offset
,
origin
[
2
]],
[
origin
[
0
]
+
x_offset
+
step
,
origin
[
1
]
+
y_offset
,
origin
[
2
]],
[
origin
[
0
]
+
x_offset
+
step
,
origin
[
1
]
+
y_offset
+
step
,
origin
[
2
]],
[
origin
[
0
]
+
x_offset
,
origin
[
1
]
+
y_offset
+
step
,
origin
[
2
]]
]
elif
normal
==
[
0
,
1
,
0
]:
# XZ plane (Front)
vertices
=
[
[
origin
[
0
]
+
x_offset
,
origin
[
1
],
origin
[
2
]
+
y_offset
],
[
origin
[
0
]
+
x_offset
+
step
,
origin
[
1
],
origin
[
2
]
+
y_offset
],
[
origin
[
0
]
+
x_offset
+
step
,
origin
[
1
],
origin
[
2
]
+
y_offset
+
step
],
[
origin
[
0
]
+
x_offset
,
origin
[
1
],
origin
[
2
]
+
y_offset
+
step
]
]
elif
normal
==
[
0
,
-
1
,
0
]:
# XZ plane (Back)
vertices
=
[
[
origin
[
0
]
+
x_offset
,
origin
[
1
],
origin
[
2
]
+
y_offset
],
[
origin
[
0
]
+
x_offset
+
step
,
origin
[
1
],
origin
[
2
]
+
y_offset
],
[
origin
[
0
]
+
x_offset
+
step
,
origin
[
1
],
origin
[
2
]
+
y_offset
+
step
],
[
origin
[
0
]
+
x_offset
,
origin
[
1
],
origin
[
2
]
+
y_offset
+
step
]
]
elif
normal
==
[
1
,
0
,
0
]:
# YZ plane (Right)
vertices
=
[
[
origin
[
0
],
origin
[
1
]
+
x_offset
,
origin
[
2
]
+
y_offset
],
[
origin
[
0
],
origin
[
1
]
+
x_offset
+
step
,
origin
[
2
]
+
y_offset
],
[
origin
[
0
],
origin
[
1
]
+
x_offset
+
step
,
origin
[
2
]
+
y_offset
+
step
],
[
origin
[
0
],
origin
[
1
]
+
x_offset
,
origin
[
2
]
+
y_offset
+
step
]
]
elif
normal
==
[
-
1
,
0
,
0
]:
# YZ plane (Left)
vertices
=
[
[
origin
[
0
],
origin
[
1
]
+
x_offset
,
origin
[
2
]
+
y_offset
],
[
origin
[
0
],
origin
[
1
]
+
x_offset
+
step
,
origin
[
2
]
+
y_offset
],
[
origin
[
0
],
origin
[
1
]
+
x_offset
+
step
,
origin
[
2
]
+
y_offset
+
step
],
[
origin
[
0
],
origin
[
1
]
+
x_offset
,
origin
[
2
]
+
y_offset
+
step
]
]
else
:
continue
face
=
Poly3DCollection
([
vertices
],
color
=
color
,
edgecolor
=
'
black
'
)
ax
.
add_collection3d
(
face
)
def
draw_cube
(
state
):
"""
Draw a 3D Rubik
'
s cube based on the input state.
"""
fig
=
plt
.
figure
(
figsize
=
(
8
,
8
))
ax
=
fig
.
add_subplot
(
111
,
projection
=
'
3d
'
)
# Define face origins and normals
face_definitions
=
{
"
Top
"
:
([
-
0.5
,
-
0.5
,
0.5
],
[
0
,
0
,
1
]),
"
Bottom
"
:
([
-
0.5
,
-
0.5
,
-
0.5
],
[
0
,
0
,
-
1
]),
"
Front
"
:
([
-
0.5
,
0.5
,
-
0.5
],
[
0
,
1
,
0
]),
"
Back
"
:
([
-
0.5
,
-
0.5
,
-
0.5
],
[
0
,
-
1
,
0
]),
"
Left
"
:
([
-
0.5
,
-
0.5
,
-
0.5
],
[
-
1
,
0
,
0
]),
"
Right
"
:
([
0.5
,
-
0.5
,
-
0.5
],
[
1
,
0
,
0
]),
}
# Draw each face
for
face
,
(
origin
,
normal
)
in
face_definitions
.
items
():
draw_face
(
ax
,
origin
,
normal
,
state
[
face
])
# Set the aspect of the plot to be equal
ax
.
set_box_aspect
([
1
,
1
,
1
])
# Aspect ratio is 1:1:1
# Set limits and labels
ax
.
set_xlim
([
-
0.75
,
0.75
])
ax
.
set_ylim
([
-
0.75
,
0.75
])
ax
.
set_zlim
([
-
0.75
,
0.75
])
ax
.
set_xlabel
(
"
X
"
)
ax
.
set_ylabel
(
"
Y
"
)
ax
.
set_zlabel
(
"
Z
"
)
# Display the cube
plt
.
show
()
# Load the YOLO model
model
=
YOLO
(
r
"
Cube_detection/runs/detect/best/weights/best.pt
"
)
...
...
@@ -145,19 +44,23 @@ sequence = ['Front', 'Top', 'Bottom', 'Left', 'Right', 'Back']
current_step
=
0
# Initialize matplotlib figure
fig
=
plt
.
figure
()
ax
=
fig
.
add_subplot
(
111
,
projection
=
'
3d
'
)
draw_cube
(
cube_state
)
def
save_colors
(
face
,
colors
):
"""
Save the detected colors for the given face.
"""
cube_state
[
face
]
=
np
.
array
(
colors
).
reshape
(
3
,
3
).
tolist
()
draw_cube
(
ax
,
cube_state
)
# Update visualization
print
(
f
"
Colors for
{
face
}
saved:
"
,
colors
)
cube_state
=
{
'
Front
'
:
[[
'
red
'
,
'
blue
'
,
'
blue
'
],
[
'
white
'
,
'
white
'
,
'
white
'
],
[
'
white
'
,
'
red
'
,
'
blue
'
]],
'
Top
'
:
[[
'
red
'
,
'
blue
'
,
'
blue
'
],
[
'
white
'
,
'
white
'
,
'
white
'
],
[
'
white
'
,
'
red
'
,
'
blue
'
]],
'
Bottom
'
:
[[
'
white
'
,
'
blue
'
,
'
white
'
],
[
'
white
'
,
'
white
'
,
'
blue
'
],
[
'
white
'
,
'
red
'
,
'
blue
'
]],
'
Left
'
:
[[
'
red
'
,
'
blue
'
,
'
blue
'
],
[
'
white
'
,
'
blue
'
,
'
white
'
],
[
'
white
'
,
'
red
'
,
'
blue
'
]],
'
Right
'
:
[[
'
red
'
,
'
blue
'
,
'
blue
'
],
[
'
white
'
,
'
blue
'
,
'
white
'
],
[
'
white
'
,
'
red
'
,
'
blue
'
]],
'
Back
'
:
[[
'
red
'
,
'
blue
'
,
'
blue
'
],
[
'
white
'
,
'
blue
'
,
'
white
'
],
[
'
white
'
,
'
red
'
,
'
blue
'
]]}
draw_cube
(
cube_state
)
# cube_state = {'Front': [['red', 'red', 'white'], ['white', 'white', 'red'], ['red', 'red', 'red']],
# 'Top': [['white', 'blue', 'white'], ['white', 'white', 'red'], ['white', 'white', 'red']],
# 'Bottom': [['white', 'blue', 'white'], ['red', 'blue', 'red'], ['red', 'white', 'white']],
# 'Left': [['white', 'white', 'red'], ['white', 'red', 'white'], ['white', 'white', 'red']],
# 'Right': [['white', 'red', 'white'], ['red', 'red', 'blue'], ['blue', 'blue', 'blue']],
# 'Back': [['white', 'yellow', 'red'], ['white', 'yellow', 'blue'], ['blue', 'white', 'blue']]}
# draw_cube(cube_state)
if
cube_state
==
None
:
if
cube_state
==
None
or
True
:
while
True
:
ret
,
frame
=
cap
.
read
()
if
not
ret
:
...
...
@@ -194,11 +97,15 @@ if cube_state ==None:
predicted_colors
=
raspberryPi
.
predict_colors
(
model_color
,
cropped_frame
)
colors
=
raspberryPi
.
indices_to_colors
(
predicted_colors
)
save_colors
(
sequence
[
current_step
],
colors
)
print
(
colors
)
current_step
+=
1
if
current_step
>=
len
(
sequence
):
print
(
"
All sides scanned successfully!
"
)
print
(
"
Final Cube Colors:
"
,
cube_state
)
fig
=
plt
.
figure
()
ax
=
fig
.
add_subplot
(
111
,
projection
=
'
3d
'
)
draw_cube
(
cube_state
)
# Update visualization
break
elif
key_confirm
==
ord
(
'
r
'
):
print
(
"
Retaking the picture...
"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment