Webcam For 3x3 Rubix - Struct Python
Webcam For 3x3 Rubix - Struct Python
import cv2
import numpy as np
from rubik_solver import Solver
# Các hằng số
COLOR_MAPPING = {
"green": "F",
"blue": "B",
"white": "U",
"yellow": "D",
"orange": "L",
"red": "R"
}
LOWER_BOUND = np.array([0, 100, 100])
UPPER_BOUND = np.array([10, 255, 255])
MIN_CONTOUR_AREA = 100
centers = []
for contour in contours:
area = cv2.contourArea(contour)
if area > MIN_CONTOUR_AREA:
moments = cv2.moments(contour)
if moments["m00"] != 0:
cx = int(moments["m10"] / moments["m00"])
cy = int(moments["m01"] / moments["m00"])
centers.append((cx, cy))
return centers
while True:
ret, frame = cap.read()
centers = detect_colors(frame)
# Nếu nhận diện đủ số lượng mảnh màu, thì giải Rubik's Cube
if len(centers) == 6:
solve_rubiks_cube(["green", "blue", "white", "yellow", "orange", "red"])
break
cap.release()
cv2.destroyAllWindows()