Example #1
0
    def update_preview_state(self, frame, contours):
        """
        Get the average color value for the contour for every X amount of frames
        to prevent flickering and more precise results.
        """
        max_average_rounds = 8
        for index, (x, y, w, h) in enumerate(contours):
            if index in self.average_sticker_colors and len(
                    self.average_sticker_colors[index]) == max_average_rounds:
                sorted_items = {}
                for bgr in self.average_sticker_colors[index]:
                    key = str(bgr)
                    if key in sorted_items:
                        sorted_items[key] += 1
                    else:
                        sorted_items[key] = 1
                most_common_color = max(sorted_items,
                                        key=lambda i: sorted_items[i])
                self.average_sticker_colors[index] = []
                self.preview_state[index] = eval(most_common_color)
                break

            roi = frame[y + 7:y + h - 7, x + 14:x + w - 14]
            avg_bgr = ColorDetector.get_dominant_color(roi)
            closest_color = ColorDetector.get_closest_color(
                avg_bgr)['color_bgr']
            self.preview_state[index] = closest_color
            if index in self.average_sticker_colors:
                self.average_sticker_colors[index].append(closest_color)
            else:
                self.average_sticker_colors[index] = [closest_color]
Example #2
0
    def scan(self, mainNotation):
        """
        Open up the webcam and scans the 9 regions in the center
        and show a preview in the left upper corner.

        After hitting the space bar to confirm, the block below the
        current stickers shows the current state that you have.
        This is show every user can see what the computer toke as input.

        :returns: dictionary
        """

        sides = {}
        preview = [
            'white', 'white', 'white', 'white', 'white', 'white', 'white',
            'white', 'white'
        ]
        state = [0, 0, 0, 0, 0, 0, 0, 0, 0]
        while True:
            _, frame = self.cam.read()
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            key = cv2.waitKey(10) & 0xff

            # init certain stickers.
            self.draw_main_stickers(frame)
            self.draw_preview_stickers(frame, preview)

            for index, (x, y) in enumerate(self.stickers):
                roi = hsv[y:y + 32, x:x + 32]
                avg_hsv = ColorDetector.average_hsv(roi)
                color_name = ColorDetector.get_color_name(avg_hsv)
                state[index] = color_name

                # update when space bar is pressed.
                if key == 32:
                    preview = list(state)
                    self.draw_preview_stickers(frame, state)
                    face = mainNotation[state[4]]
                    notation = [mainNotation[color] for color in state]
                    sides[face] = notation

            # show the new stickers
            self.draw_current_stickers(frame, state)

            # append amount of scanned sides
            text = 'scanned sides: {}/6'.format(len(sides))
            cv2.putText(frame, text, (20, 460), cv2.FONT_HERSHEY_TRIPLEX, 0.5,
                        (255, 255, 255), 1, cv2.LINE_AA)

            # quit on escape.
            if key == 27:
                break

            # show result
            cv2.imshow("default", frame)

        self.cam.release()
        cv2.destroyAllWindows()
        return sides if len(sides) == 6 else False
Example #3
0
File: video.py Project: muts/qbr
    def scan(self):
        """
        Open up the webcam and scans the 9 regions in the center
        and show a preview in the left upper corner.

        After hitting the space bar to confirm, the block below the
        current stickers shows the current state that you have.
        This is show every user can see what the computer toke as input.

        :returns: dictionary
        """

        sides   = {}
        preview = ['white','white','white',
                   'white','white','white',
                   'white','white','white']
        state   = [0,0,0,
                   0,0,0,
                   0,0,0]
        while True:
            _, frame = self.cam.read()
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            key = cv2.waitKey(10) & 0xff

            # init certain stickers.
            self.draw_main_stickers(frame)
            self.draw_preview_stickers(frame, preview)

            for index,(x,y) in enumerate(self.stickers):
                roi          = hsv[y:y+32, x:x+32]
                avg_hsv      = ColorDetector.average_hsv(roi)
                color_name   = ColorDetector.get_color_name(avg_hsv)
                state[index] = color_name

                # update when space bar is pressed.
                if key == 32:
                    preview = list(state)
                    self.draw_preview_stickers(frame, state)
                    face = self.color_to_notation(state[4])
                    notation = [self.color_to_notation(color) for color in state]
                    sides[face] = notation

            # show the new stickers
            self.draw_current_stickers(frame, state)

            # append amount of scanned sides
            text = 'scanned sides: {}/6'.format(len(sides))
            cv2.putText(frame, text, (20, 460), cv2.FONT_HERSHEY_TRIPLEX, 0.5, (255,255,255), 1, cv2.LINE_AA)

            # quit on escape.
            if key == 27:
                break

            # show result
            cv2.imshow("default", frame)

        self.cam.release()
        cv2.destroyAllWindows()
        return sides if len(sides) == 6 else False
Example #4
0
 def __init__(self, image):
     self.image = image
     self.upperleft = [-0.406520238446, 0.314348683295
                       ]  #this is (x,y) from cam and (y,x) from sawyer
     self.lowerRight = [0.289579707007, 0.741919848134]
     # self.upperleft1D = [-0.239140019919,0.650653669801] #this is (x,y) from cam and (y,x) from sawyer
     # self.lowerRight1D = [0.178982502624,.739727213783]
     self.CD = ColorDetector(image)
     self.calibrate_camera()
    def scan(self):
        sides = {}
        preview = [
            'white', 'white', 'white', 'white', 'white', 'white', 'white',
            'white', 'white'
        ]
        state = [0, 0, 0, 0, 0, 0, 0, 0, 0]
        while True:
            _, frame = self.cam.read()
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            key = cv2.waitKey(10) & 0xff

            # init certain stickers.
            self.draw_main_stickers(frame)
            self.draw_preview_stickers(frame, preview)

            for index, (x, y) in enumerate(self.stickers):
                roi = hsv[y:y + 32, x:x + 32]
                avg_hsv = ColorDetector.average_hsv(roi)
                color_name = ColorDetector.get_color_name(avg_hsv)
                state[index] = color_name

                # update when space bar is pressed.
                if key == 32:
                    preview = list(state)
                    self.draw_preview_stickers(frame, state)
                    face = self.color_to_notation(state[4])
                    notation = [
                        self.color_to_notation(color) for color in state
                    ]
                    sides[face] = notation

            # show the new stickers
            self.draw_current_stickers(frame, state)

            # append amount of scanned sides
            text = 'scanned sides: {}/6'.format(len(sides))
            cv2.putText(frame, text, (20, 460), cv2.FONT_HERSHEY_TRIPLEX, 0.5,
                        (255, 255, 255), 1, cv2.LINE_AA)

            # quit on escape.
            if key == 27:
                break

            # show result
            cv2.imshow("default", frame)

        self.cam.release()
        cv2.destroyAllWindows()
        return sides if len(sides) == 6 else False
Example #6
0
def main():
    src = 0
    cap = cv2.VideoCapture(src)
    _, frame = cap.read()
    #find_corner(frame)
    cd = ColorDetector(frame)
    ul, lr = cd.getCorners()
    frame[ul[1]][ul[0]] = [0, 0, 255]
    frame[lr[1]][lr[0]] = [0, 0, 255]
    print(ul, lr)
    cv2.imshow('frame', frame)
    cv2.waitKey(0)
    cv2.destroyAllWindows
    cap.release()
Example #7
0
class FindWorldPoseFromPixels:
    def __init__(self, image):
        self.image = image
        self.upperleft = [-0.406520238446, 0.314348683295
                          ]  #this is (x,y) from cam and (y,x) from sawyer
        self.lowerRight = [0.289579707007, 0.741919848134]
        # self.upperleft1D = [-0.239140019919,0.650653669801] #this is (x,y) from cam and (y,x) from sawyer
        # self.lowerRight1D = [0.178982502624,.739727213783]
        self.CD = ColorDetector(image)
        self.calibrate_camera()

    def calibrate_camera(self):
        self.pixelUl, self.pixelLR = self.CD.getCorners()
        self.fx = (self.lowerRight[0] - self.upperleft[0]) / (self.pixelLR[0] -
                                                              self.pixelUl[0])
        self.fy = (self.lowerRight[1] - self.upperleft[1]) / (self.pixelLR[1] -
                                                              self.pixelUl[1])
        # self.fx1D = (self.lowerRight1D[0]-self.upperleft1D[0])/(pixelLR[0]-pixelUl[0])

    def calc_pose(self, target):
        return [
            ((target[0] - self.pixelUl[0]) * self.fx) + self.upperleft[0] +
            .075,
            ((target[1] - self.pixelUl[1]) * self.fy) + self.upperleft[1] + .03
        ]
Example #8
0
 def update_snapshot_state(self, frame):
     """Update the snapshot state based on the current preview state."""
     self.snapshot_state = list(self.preview_state)
     center_color_name = ColorDetector.get_closest_color(
         self.snapshot_state[4])['color_name']
     self.sides[center_color_name] = self.snapshot_state
     self.draw_snapshot_stickers(frame)
    def scan(self, trigger):

        _, frame = self.cam.read()

        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

        key = cv2.waitKey(10) & 0xff

        self.draw_main_stickers(frame)
        self.draw_preview_stickers(frame, self.preview)

        for index, (x, y) in enumerate(self.stickers):
            roi = hsv[y:y + 32, x:x + 32]
            avg_hsv = ColorDetector.average_hsv(roi)
            color_name = ColorDetector.get_color_name(avg_hsv)
            #print("Color: " + str(color_name) + ", avg_hsv  :  " + str(avg_hsv))
            self.state[index] = color_name

            # update when we recieve camera-command from input-param
            if trigger:
                self.preview = list(self.state)
                self.draw_preview_stickers(frame, self.state)
                face = self.color_to_notation(self.state[4])
                notation = [
                    self.color_to_notation(color) for color in self.state
                ]
                self.sides[face] = notation
                #print(self.sides)

        # show the new stickers
        self.draw_current_stickers(frame, self.state)

        text = 'scanned sides: {}/6'.format(len(self.sides))
        cv2.putText(frame, text, (20, 460), cv2.FONT_HERSHEY_TRIPLEX, 0.5,
                    (255, 255, 255), 1, cv2.LINE_AA)

        cv2.imshow("Rubiks", frame)
Example #10
0
    def draw_stickers(self, frame, stickers, offset_x, offset_y):
        """Draws the given stickers onto the given frame."""
        index = -1
        for row in range(3):
            for col in range(3):
                index += 1
                x1 = (offset_x + STICKER_AREA_TILE_SIZE *
                      col) + STICKER_AREA_TILE_GAP * col
                y1 = (offset_y + STICKER_AREA_TILE_SIZE *
                      row) + STICKER_AREA_TILE_GAP * row
                x2 = x1 + STICKER_AREA_TILE_SIZE
                y2 = y1 + STICKER_AREA_TILE_SIZE

                # shadow
                cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 0), -1)

                # foreground color
                cv2.rectangle(
                    frame, (x1 + 1, y1 + 1), (x2 - 1, y2 - 1),
                    ColorDetector.get_prominent_color(stickers[index]), -1)
Example #11
0
 def getColor(self, face):
     '''
     Get the RGB colors from the face letter
     
     :param face: the face letter
     :returns: RGB color
     '''
     if (face == 'U'):
         return ColorDetector.name_to_rgb('white')
     elif (face == 'F'):
         return ColorDetector.name_to_rgb('blue')
     elif (face == 'R'):
         return ColorDetector.name_to_rgb('orange')
     elif (face == 'D'):
         return ColorDetector.name_to_rgb('yellow')
     elif (face == 'L'):
         return ColorDetector.name_to_rgb('red')
     elif (face == 'B'):
         return ColorDetector.name_to_rgb('green')
Example #12
0
def draw_current_stickers(frame, state):
    """Draws the 9 current stickers in the frame."""
    for index, (x, y) in enumerate(current_stickers):
        cv2.rectangle(frame, (x, y), (x + 32, y + 32),
                      ColorDetector.name_to_rgb(state[index]), -1)
Example #13
0
File: video.py Project: muts/qbr
 def draw_preview_stickers(self, frame, state):
     """Draws the 9 preview stickers in the frame."""
     for index,(x,y) in enumerate(self.preview_stickers):
         cv2.rectangle(frame, (x,y), (x+32, y+32), ColorDetector.name_to_rgb(state[index]), -1)
Example #14
0
def scan():
    """
    Open up the webcam and scans the 9 regions in the center
    and show a preview.

    After hitting the space bar to confirm, the block below the
    current stickers shows the current state that you have.
    This is show every user can see what the computer took as input.

    :returns: dictionary
    """

    sides   = {}                            # collection of scanned sides
    preview = ['white','white','white',     # default starting preview sticker colors
               'white','white','white',
               'white','white','white']
    state   = [0,0,0,                       # current sticker colors
               0,0,0,
               0,0,0]

    defaultcal = {                          # default color calibration
                'white':[[179,30,255],[0,0,0]],
                'green':[[102,255,184],[63,85,39]],
                'red':[[172,255,147],[13,165,86]],
                'orange':[[172,255,255],[7,136,148]],
                'yellow':[[43,172,235],[23,20,52]],
                'blue':[[118,255,194],[89,178,51]]
                }

    colorcal  = {}                          # color calibration dictionary
    color = ['white', 'green', 'red', 'orange', 'yellow', 'blue']  # list of valid colors            
    
    cv2.namedWindow('default',0)
    # create trackbars here
    cv2.createTrackbar('H Upper',"default",defaultcal[color[len(colorcal)]][0][0],179, empty_callback)
    cv2.createTrackbar('H Lower',"default",defaultcal[color[len(colorcal)]][0][1],179, empty_callback)

    # Remember that the range for S and V are not 0 to 179
    # make four more trackbars for ('S Upper', 'S Lower', 'V Upper', 'V Lower')
    # Note you should use these trackbar names to make other parts of the code run properly


    colorcal = defaultcal

    ##################################################
    # Task 1: you can insert out of the loop code here
    ##################################################



    while cameratesting:
        '''
        Here we want to make sure things are working and learn about how to use some openCV functions
        Your code here
        '''
        #task 1.2 preview a camera window
        #task 1.3 draw a rectangle
        #task 1.4 make a slider
        #task 1.5 add text
        #task 1.6 make a mask based on hsv
        #task 1.7 display the masked image


    while not cameratesting:
        _, frame = None # your code here
        hsv = None      # your code here
        key = None      # your code here

        # init certain stickers.
        draw_detector_stickers(frame)
        draw_recorded_stickers(frame, preview)

        for index,(x,y) in enumerate(detector_stickers):
            roi          = hsv[y:y+32, x:x+32]              # extracts hsv values within sticker
            avg_hsv      = ColorDetector.median_hsv(roi)    # filters the hsv values into one hsv
            color_name   = ColorDetector.get_color_name(avg_hsv,colorcal) # extracts the color based on hsv
            state[index] = color_name                       # stores the color 

            # update when space bar is pressed.
            if key == 32:
                preview = list(state)
                draw_recorded_stickers(frame, state)         # draw the saved colors on the preview
                face = color_to_notation(state[4])          # convert the color to notation of the middle sticker and label this as the face
                notation = [color_to_notation(color) for color in state] # convert all colors to notation
                sides[face] = notation                      # update the face in the sides dictionary

        # show the new stickers
        draw_current_stickers(frame, state)                 # draw live sampling of face colors

        # append amount of scanned sides
        text = 'scanned sides: {}/6'.format(len(sides))
        cv2.putText(frame, text, (20, 460), cv2.FONT_HERSHEY_TRIPLEX, 0.5, (255,255,255), 1, cv2.LINE_AA)
        
        # indicate the scanning instruction
        textInstruction = 'scan and rotate the cube with white on the top and green on the front (towards camera)'
        textInstruction2 = 'the color of center brick is used as the side identifier (since the center brick does not move)'
        textInstruction3 = 'you can scan as many times as you want'
        textInstruction4 = 'the program will overwrite the old scan when same side is detected'
        cv2.putText(frame, textInstruction, (20, 600), cv2.FONT_HERSHEY_TRIPLEX, 0.5, (255,255,255), 1, cv2.LINE_AA)
        cv2.putText(frame, textInstruction2, (20, 620), cv2.FONT_HERSHEY_TRIPLEX, 0.5, (255,255,255), 1, cv2.LINE_AA)
        cv2.putText(frame, textInstruction3, (20, 640), cv2.FONT_HERSHEY_TRIPLEX, 0.5, (255,255,255), 1, cv2.LINE_AA)
        cv2.putText(frame, textInstruction4, (20, 660), cv2.FONT_HERSHEY_TRIPLEX, 0.5, (255,255,255), 1, cv2.LINE_AA)

        # quit on escape.
        if key == 27:
            break

        # show result
        cv2.imshow("default", frame)

        if key == 99: 
            colorcal = {}   
            while len(colorcal) < 6:
                _, frame = cam.read()
                
                
                hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
                key = cv2.waitKey(10) & 0xff

                # hue upper lower
                hu = cv2.getTrackbarPos('H Upper','default')
                hl = cv2.getTrackbarPos('H Lower','default')
                # saturation upper lower
                su = None # yourcode here
                sl = None # yourcode here
                # value upper lower
                vu = None # yourcode here
                vl = None # yourcode here

                if color[len(colorcal)] == 'red' or color[len(colorcal)] == 'orange':
                    lower_hsv = np.array([0,sl,vl])
                    upper_hsv = np.array([hl,su,vu])
                    mask1 = cv2.inRange(hsv, lower_hsv, upper_hsv)
                    lower_hsv = np.array([hu,sl,vl])
                    upper_hsv = np.array([179,su,vu])
                    mask2 = cv2.inRange(hsv, lower_hsv, upper_hsv)
                    mask = cv2.bitwise_or(mask1, mask2)
                    res = cv2.bitwise_and(frame,frame, mask= mask)
                    lower_hsv = np.array([hl,sl,vl])
                    upper_hsv = np.array([hu,su,vu])
                else:
                    lower_hsv = np.array([hl,sl,vl])
                    upper_hsv = np.array([hu,su,vu])
                    
                    # Task 3
                    mask = None # your code here
                    res = None # your code here
                
                if key == 32:
                    defaultcal[color[len(colorcal)]] = [upper_hsv,lower_hsv]
                    colorcal[color[len(colorcal)]] = [upper_hsv,lower_hsv]

                    if(len(colorcal) < 6):
                        cv2.setTrackbarPos('H Upper','default',defaultcal[color[len(colorcal)]][0][0])
                        cv2.setTrackbarPos('S Upper','default',defaultcal[color[len(colorcal)]][0][1])
                        cv2.setTrackbarPos('V Upper','default',defaultcal[color[len(colorcal)]][0][2])
                        cv2.setTrackbarPos('H Lower','default',defaultcal[color[len(colorcal)]][1][0])
                        cv2.setTrackbarPos('S Lower','default',defaultcal[color[len(colorcal)]][1][1])
                        cv2.setTrackbarPos('V Lower','default',defaultcal[color[len(colorcal)]][1][2])

                if(len(colorcal) < 6):
                    text = 'calibrating {}'.format(color[len(colorcal)])
                cv2.putText(res, text, (20, 460), cv2.FONT_HERSHEY_TRIPLEX, 0.5, (255,255,255), 1, cv2.LINE_AA)

                cv2.imshow("default", res)
                # quit on escape key.
                if key == 27:
                    break

    cam.release()
    cv2.destroyAllWindows()
    return sides if len(sides) == 6 else False
Example #15
0
 def update_preview(self, frame):
     self.preview = list(self.state)
     center_color_name = ColorDetector.get_closest_color(
         self.preview[4])['color_name']
     self.sides[center_color_name] = self.preview
     self.draw_stickers(self.preview_stickers, frame, self.preview)
Example #16
0
    def scan(self):
        """
        Open up the webcam and scans the 9 regions in the center
        and show a preview in the left upper corner.

        After hitting the space bar to confirm, the block below the
        current stickers shows the current state that you have.
        This is show every user can see what the computer toke as input.

        :returns: dictionary
        """
        while True:
            key = cv2.waitKey(10) & 0xff

            # Quit on escape.
            if key == 27:
                break

            _, frame = self.cam.read()
            grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            # denoisedFrame = cv2.fastNlMeansDenoising(grayFrame, None, 10, 7, 7)
            blurredFrame = cv2.blur(grayFrame, (5, 5))
            cannyFrame = cv2.Canny(blurredFrame, 30, 60, 3)

            kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 9))
            dilatedFrame = cv2.dilate(cannyFrame, kernel)

            contours = self.find_contours(dilatedFrame)
            if len(contours) == 9:
                self.draw_contours(frame, contours)
                self.update_state(frame, contours)

            # Update the snapshot preview when space bar is pressed.
            if key == 32:
                self.update_preview(frame)

            self.draw_stickers(self.current_stickers, frame, self.state)
            self.draw_stickers(self.preview_stickers, frame, self.preview)

            # Dislay amount of scanned sides.
            text = 'scanned sides: {}/6'.format(len(self.sides.keys()))
            cv2.putText(frame, text, (20, self.height - 20),
                        cv2.FONT_HERSHEY_TRIPLEX, 0.5, (255, 255, 255), 1,
                        cv2.LINE_AA)

            # Show the result.
            cv2.imshow('default', frame)

        self.cam.release()
        cv2.destroyAllWindows()

        if len(self.sides.keys()) != 6:
            return False

        if not self.scanned_successfully():
            return False

        # Convert all the sides and their BGR colors to cube notation.
        notation = dict(self.sides)
        for side, state in notation.items():
            for sticker_index, bgr in enumerate(state):
                notation[side][
                    sticker_index] = ColorDetector.convert_bgr_to_notation(bgr)

        # Join all the sides together into one single string.
        # Order must be URFDLB (white, red, green, yellow, orange, blue)
        combined = ''
        for side in ['white', 'red', 'green', 'yellow', 'orange', 'blue']:
            combined += ''.join(notation[side])
        return combined
Example #17
0
    def run(self):
        """
        Open up the webcam and present the user with the Qbr user interface.

        Returns a string of the scanned state in rubik's cube notation.
        """
        while True:
            _, frame = self.cam.read()
            key = cv2.waitKey(10) & 0xff

            # Quit on escape.
            if key == 27:
                break

            # Update the snapshot when space bar is pressed.
            if key == 32 and not self.calibrate_mode:
                self.update_snapshot_state(frame)

            # Toggle calibrate mode.
            if key == ord(CALIBRATE_MODE_KEY):
                self.reset_calibrate_mode()
                self.calibrate_mode = not self.calibrate_mode

            grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            blurredFrame = cv2.blur(grayFrame, (5, 5))
            cannyFrame = cv2.Canny(blurredFrame, 30, 60, 3)
            kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 9))
            dilatedFrame = cv2.dilate(cannyFrame, kernel)

            contours = self.find_contours(dilatedFrame)
            if len(contours) == 9:
                self.draw_contours(frame, contours)
                if not self.calibrate_mode:
                    self.update_preview_state(frame, contours)
                elif key == 32 and self.done_calibrating == False:
                    current_color = self.cube_sides[
                        self.current_color_to_calibrate_index]
                    (x, y, w, h) = contours[4]
                    roi = frame[y + 7:y + h - 7, x + 14:x + w - 14]
                    avg_bgr = ColorDetector.get_dominant_color(roi)
                    self.calibrated_colors[current_color] = avg_bgr
                    self.current_color_to_calibrate_index += 1
                    self.done_calibrating = self.current_color_to_calibrate_index == len(
                        self.cube_sides)
                    if self.done_calibrating:
                        ColorDetector.set_cube_color_pallete(
                            self.calibrated_colors)

            if self.calibrate_mode:
                self.display_current_color_to_calibrate(frame)
                self.display_calibrated_colors(frame)
            else:
                self.draw_preview_stickers(frame)
                self.draw_snapshot_stickers(frame)
                self.display_scanned_sides(frame)

            cv2.imshow('default', frame)

        self.cam.release()
        cv2.destroyAllWindows()

        if len(self.sides.keys()) != 6:
            return False

        if not self.scanned_successfully():
            return False

        # Convert all the sides and their BGR colors to cube notation.
        notation = dict(self.sides)
        for side, preview in notation.items():
            for sticker_index, bgr in enumerate(preview):
                notation[side][
                    sticker_index] = ColorDetector.convert_bgr_to_notation(bgr)

        # Join all the sides together into one single string.
        # Order must be URFDLB (white, red, green, yellow, orange, blue)
        combined = ''
        for side in ['white', 'red', 'green', 'yellow', 'orange', 'blue']:
            combined += ''.join(notation[side])
        return combined
Example #18
0
def draw_recorded_stickers(frame, state):
    """Draws the 9 preview stickers in the frame."""
    # for index,(x,y) in (recorded_stickers):
    for index, (x, y) in enumerate(recorded_stickers):
        cv2.rectangle(frame, (x, y), (x + 32, y + 32),
                      ColorDetector.name_to_rgb(state[index]), -1)
Example #19
0
def scan():

    sides = {}  # collection of scanned sides
    preview = [
        'white',
        'white',  # default starting preview sticker colors
        'white',
        'white',
    ]
    state = [
        0,
        0,  # current sticker colors
        0,
        0
    ]

    defaultcal = {  # default color calibration
        'white': [[64, 173, 255], [3, 0, 0]],
        'green': [[104, 255, 255], [31, 85, 78]],
        'red': [[172, 255, 255], [7, 136, 148]],
        'orange': [[45, 255, 255], [17, 71, 186]],
        'yellow': [[48, 255, 242], [27, 178, 51]],
        'blue': [[172, 255, 255], [68, 0, 0]]
    }

    colorcal = {}  # color calibration dictionary
    color = ['white', 'green', 'red', 'orange', 'yellow',
             'blue']  # list of valid colors

    cv2.namedWindow('default', 0)
    # create trackbars here
    cv2.createTrackbar('H Upper', 'default',
                       defaultcal[color[len(colorcal)]][0][0], 179,
                       empty_callback)
    cv2.createTrackbar('S Upper', 'default',
                       defaultcal[color[len(colorcal)]][0][1], 255,
                       empty_callback)
    cv2.createTrackbar('V Upper', 'default',
                       defaultcal[color[len(colorcal)]][0][2], 255,
                       empty_callback)
    cv2.createTrackbar('H Lower', 'default',
                       defaultcal[color[len(colorcal)]][1][0], 179,
                       empty_callback)
    cv2.createTrackbar('S Lower', 'default',
                       defaultcal[color[len(colorcal)]][1][1], 255,
                       empty_callback)
    cv2.createTrackbar('V Lower', 'default',
                       defaultcal[color[len(colorcal)]][1][2], 255,
                       empty_callback)

    colorcal = defaultcal

    while cameratesting:
        global i
        _, frame = cam.read()
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        key = cv2.waitKey(300)
        draw_detector_stickers(frame)
        # Bounds for HSV values we are interested in (Blue)
        # lower_hsv = np.array([89,178,51])     #hmin,smin,vmin
        # upper_hsv = np.array([118,255,194])   #hmax,smax,vmax
        # mask = cv2.inRange(hsv, lower_hsv, upper_hsv)
        # frame = cv2.bitwise_and(frame,frame, mask= mask)

        # cv2.rectangle(frame, (200,200), (250, 250), (255,0,0), 2)
        # # -1 borderwidth is a fill
        # cv2.rectangle(frame, (300,200), (350, 250), (0,0,255), -1)

        #preview a camera window
        # cv2.imshow('my_window_name', frame)
        # value = cv2.getTrackbarPos('My track bar','my_window_name')

        # print(value)

        # init certain stickers.

        for index, (x, y) in enumerate(detector_stickers):
            # cv2.rectangle(frame, (x,y), (x+30, y+30), (255,255,255), 2)
            roi = hsv[y:y + 50, x:x + 50]  # extracts hsv values within sticker
            avg_hsv = ColorDetector.average_hsv(
                roi)  # filters the hsv values into one hsv
            color_name = ColorDetector.get_color_name(
                avg_hsv, colorcal)  # extracts the color based on hsv
            state[index] = color_name  # stores the color

            # update when space bar is pressed.

            if key == 32:
                i += 0.25
                preview = list(state)
                draw_recorded_stickers(
                    frame, state)  # draw the saved colors on the preview
                face = numb_to_notation(
                    math.ceil(i)
                )  # convert the color to notation of the middle sticker and label this as the face
                # time.sleep(0.5)
                notation = [color_to_notation(color) for color in state
                            ]  # convert all colors to notation
                sides[
                    face] = notation  # update the face in the sides dictionary

        # show the new stickers
        draw_current_stickers(frame,
                              state)  # draw live sampling of face colors

        # append amount of scanned sides
        text = 'scanned sides: {}/6'.format(len(sides))
        cv2.putText(frame, text, (20, 460), cv2.FONT_HERSHEY_TRIPLEX, 0.5,
                    (255, 255, 255), 1, cv2.LINE_AA)

        # quit on escape.
        if key == 27:
            break

        # show result
        cv2.imshow("default", frame)

        # show color calibration when pressing button c
        if key == 99:
            colorcal = {}
            while len(colorcal) < 6:
                _, frame = cam.read()

                hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
                key = cv2.waitKey(10) & 0xff

                # hue upper lower
                # hue upper lower
                hu = cv2.getTrackbarPos('H Upper', 'default')
                hl = cv2.getTrackbarPos('H Lower', 'default')
                # saturation upper lower
                su = cv2.getTrackbarPos('S Upper', 'default')
                sl = cv2.getTrackbarPos('S Lower', 'default')
                # value upper lower
                vu = cv2.getTrackbarPos('V Upper', 'default')
                vl = cv2.getTrackbarPos('V Lower', 'default')

                if color[len(colorcal)] == 'red' or color[len(
                        colorcal)] == 'orange':
                    lower_hsv = np.array([0, sl, vl])
                    upper_hsv = np.array([hl, su, vu])
                    mask1 = cv2.inRange(hsv, lower_hsv, upper_hsv)
                    lower_hsv = np.array([hu, sl, vl])
                    upper_hsv = np.array([179, su, vu])
                    mask2 = cv2.inRange(hsv, lower_hsv, upper_hsv)
                    mask = cv2.bitwise_or(mask1, mask2)
                    res = cv2.bitwise_and(frame, frame, mask=mask)
                    lower_hsv = np.array([hl, sl, vl])
                    upper_hsv = np.array([hu, su, vu])
                else:
                    lower_hsv = np.array([hl, sl, vl])
                    upper_hsv = np.array([hu, su, vu])

                    mask = cv2.inRange(hsv, lower_hsv, upper_hsv)
                    res = cv2.bitwise_and(frame, frame, mask=mask)

                if key == 32:
                    defaultcal[color[len(colorcal)]] = [upper_hsv, lower_hsv]
                    colorcal[color[len(colorcal)]] = [upper_hsv, lower_hsv]
                    print([upper_hsv, lower_hsv])

                    if (len(colorcal) < 6):
                        cv2.setTrackbarPos(
                            'H Upper', 'default',
                            defaultcal[color[len(colorcal)]][0][0])
                        cv2.setTrackbarPos(
                            'S Upper', 'default',
                            defaultcal[color[len(colorcal)]][0][1])
                        cv2.setTrackbarPos(
                            'V Upper', 'default',
                            defaultcal[color[len(colorcal)]][0][2])
                        cv2.setTrackbarPos(
                            'H Lower', 'default',
                            defaultcal[color[len(colorcal)]][1][0])
                        cv2.setTrackbarPos(
                            'S Lower', 'default',
                            defaultcal[color[len(colorcal)]][1][1])
                        cv2.setTrackbarPos(
                            'V Lower', 'default',
                            defaultcal[color[len(colorcal)]][1][2])

                if (len(colorcal) < 6):
                    text = 'calibrating {}'.format(color[len(colorcal)])
                cv2.putText(res, text, (20, 460), cv2.FONT_HERSHEY_TRIPLEX,
                            0.5, (255, 255, 255), 1, cv2.LINE_AA)

                cv2.imshow("default", res)
                # quit on escape key.
                if key == 27:
                    break

    cam.release()
    cv2.destroyAllWindows()
    return sides if len(sides) == 6 else False
 def draw_preview_stickers(self, frame, state):
     """Draws the 9 preview stickers in the frame."""
     for index, (x, y) in enumerate(self.preview_stickers):
         cv2.rectangle(frame, (x, y), (x + 32, y + 32),
                       ColorDetector.name_to_rgb(state[index]), -1)
Example #21
0
    def visualization(self, edgeIndex, cornerIndex, edgeBuffer, cornerBuffer,
                      parity, mainNotation):
        '''
        Draw cubes for edge and corner sequence, determine the currnet buffer with its colors 
        and the target of the cubie, the pink color on the target represents the swapped face
        the arrow keys use to flip and display the sequence
        
        :param edgeIndex: the edge index list from RubikBlindfolded package
        :param cornerIndex: the corner index list from RubikBlindfolded package
        :param edgeBuffer: the currnet buffer list from RubikBlindfolded package
        :param cornerBuffer: the currnet buffer list from RubikBlindfolded package
        '''

        edgeCounter = 0
        cornerCounter = 0
        current = 'edge'
        notation = {}

        while True:
            _, frame = self.cap.read()

            cv2.putText(frame, 'Edge Sequence', (20, 40),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (50, 50, 50), 2,
                        cv2.LINE_AA)
            cv2.putText(frame, 'Corner Sequence', (20, 280),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (50, 50, 50), 2,
                        cv2.LINE_AA)

            if (parity == 0):
                cv2.putText(frame, 'Even Parity', (320, 40),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (50, 50, 50), 2,
                            cv2.LINE_AA)
            elif (parity == 1):
                cv2.putText(frame, 'Odd Parity, apply parity algorithm',
                            (320, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                            (50, 50, 50), 2, cv2.LINE_AA)
            #(B,G,R)

            whiteF = mainNotation['white']
            blueF = mainNotation['blue']
            orangeF = mainNotation['orange']
            yellowF = mainNotation['yellow']
            redF = mainNotation['red']
            greenF = mainNotation['green']

            notation[whiteF] = 'white'
            notation[blueF] = 'blue'
            notation[orangeF] = 'orange'
            notation[yellowF] = 'yellow'
            notation[redF] = 'red'
            notation[greenF] = 'green'

            #draw cube lines
            scale = 240
            for z in range(2):
                for m in range(6):
                    if (m == 0):
                        #U
                        scale1 = 20
                        scale2 = 6
                        scale3 = -20
                        scale4 = 6
                        xp = [100, 120, 140, 120]
                        yp = [66, 72, 66, 60]

                    elif (m == 1):
                        #F
                        scale1 = 20
                        scale2 = 6
                        scale3 = 0
                        scale4 = 22
                        xp = [60, 60, 80, 80]
                        yp = [78, 100, 106, 84]

                    elif (m == 2):
                        #R
                        scale1 = 20
                        scale2 = -6
                        scale3 = 0
                        scale4 = 22
                        xp = [120, 120, 140, 140]
                        yp = [96, 118, 112, 90]

                    elif (m == 3):
                        #D
                        scale1 = 20
                        scale2 = 6
                        scale3 = 20
                        scale4 = -6
                        xp = [230, 210, 230, 250]
                        yp = [138, 144, 150, 144]

                    elif (m == 4):
                        #L
                        scale1 = -20
                        scale2 = 6
                        scale3 = 0
                        scale4 = 22
                        xp = [270, 250, 250, 270]
                        yp = [82, 88, 66, 60]

                    elif (m == 5):
                        #B
                        scale1 = -20
                        scale2 = -6
                        scale3 = 0
                        scale4 = 22
                        xp = [310, 330, 330, 310]
                        yp = [94, 100, 78, 72]

                    # x rows y columns
                    for x in range(3):
                        for y in range(3):
                            a1 = np.array(
                                [[[
                                    xp[0] + (scale1 * y) +
                                    (scale3 * x), yp[0] + (scale2 * y) +
                                    (scale4 * x) + (scale * z)
                                ],
                                  [
                                      xp[1] + (scale1 * y) +
                                      (scale3 * x), yp[1] + (scale2 * y) +
                                      (scale4 * x) + (scale * z)
                                  ],
                                  [
                                      xp[2] + (scale1 * y) +
                                      (scale3 * x), yp[2] + (scale2 * y) +
                                      (scale4 * x) + (scale * z)
                                  ],
                                  [
                                      xp[3] + (scale1 * y) +
                                      (scale3 * x), yp[3] + (scale2 * y) +
                                      (scale4 * x) + (scale * z)
                                  ]]],
                                dtype=np.int32)
                            if (x == 1 and y == 1):
                                if (m == 0):
                                    cv2.fillPoly(
                                        frame, a1,
                                        ColorDetector.name_to_rgb(
                                            notation['U']))
                                elif (m == 1):
                                    cv2.fillPoly(
                                        frame, a1,
                                        ColorDetector.name_to_rgb(
                                            notation['F']))
                                elif (m == 2):
                                    cv2.fillPoly(
                                        frame, a1,
                                        ColorDetector.name_to_rgb(
                                            notation['R']))
                                elif (m == 3):
                                    cv2.fillPoly(
                                        frame, a1,
                                        ColorDetector.name_to_rgb(
                                            notation['D']))
                                elif (m == 4):
                                    cv2.fillPoly(
                                        frame, a1,
                                        ColorDetector.name_to_rgb(
                                            notation['L']))
                                elif (m == 5):
                                    cv2.fillPoly(
                                        frame, a1,
                                        ColorDetector.name_to_rgb(
                                            notation['B']))
                            cv2.polylines(frame, a1, True, (50, 50, 50), 1)

            #draw edge sequence
            #counter
            if current == 'edge':
                numColor = (80, 80, 80)
            elif current == 'corner':
                numColor = (50, 50, 50)
            cv2.putText(frame, str(edgeCounter + 1), (20, 105),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, numColor, 2, cv2.LINE_AA)

            #current buffer
            points = self.getPoints('edge', 'U', 5)  # to find points
            a1 = np.array([[[points[0], points[1]], [points[2], points[3]],
                            [points[4], points[5]], [points[6], points[7]]]],
                          dtype=np.int32)
            cv2.fillPoly(frame, a1, self.getColor(edgeBuffer[edgeCounter][0]))
            cv2.polylines(frame, a1, True, (50, 50, 50), 1)

            points = self.getPoints('edge', 'R', 1)  # to find points
            a1 = np.array([[[points[0], points[1]], [points[2], points[3]],
                            [points[4], points[5]], [points[6], points[7]]]],
                          dtype=np.int32)
            cv2.fillPoly(frame, a1, self.getColor(edgeBuffer[edgeCounter][1]))
            cv2.polylines(frame, a1, True, (50, 50, 50), 1)

            #target
            points = self.getPoints('edge', edgeIndex[edgeCounter][0],
                                    int(edgeIndex[edgeCounter][1]))
            a1 = np.array([[[points[0], points[1]], [points[2], points[3]],
                            [points[4], points[5]], [points[6], points[7]]]],
                          dtype=np.int32)
            cv2.fillPoly(frame, a1, (255, 0, 255))
            cv2.polylines(frame, a1, True, (50, 50, 50), 1)

            points = self.getPoints('edge', edgeIndex[edgeCounter][2],
                                    int(edgeIndex[edgeCounter][3]))
            a1 = np.array([[[points[0], points[1]], [points[2], points[3]],
                            [points[4], points[5]], [points[6], points[7]]]],
                          dtype=np.int32)
            cv2.fillPoly(frame, a1, (50, 50, 50))
            cv2.polylines(frame, a1, True, (50, 50, 50), 1)

            #draw corner sequence
            #counter
            if current == 'corner':
                numColor = (80, 80, 80)
            elif current == 'edge':
                numColor = (50, 50, 50)
            cv2.putText(frame, str(cornerCounter + 1), (20, 105 + 240),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, numColor, 2, cv2.LINE_AA)

            #current buffer
            points = self.getPoints('corner', 'L', 0)  # to find points
            a1 = np.array([[[points[0], points[1]], [points[2], points[3]],
                            [points[4], points[5]], [points[6], points[7]]]],
                          dtype=np.int32)
            cv2.fillPoly(frame, a1,
                         self.getColor(cornerBuffer[cornerCounter][0]))
            cv2.polylines(frame, a1, True, (50, 50, 50), 1)

            points = self.getPoints('corner', 'U', 0)  # to find points
            a1 = np.array([[[points[0], points[1]], [points[2], points[3]],
                            [points[4], points[5]], [points[6], points[7]]]],
                          dtype=np.int32)
            cv2.fillPoly(frame, a1,
                         self.getColor(cornerBuffer[cornerCounter][1]))
            cv2.polylines(frame, a1, True, (50, 50, 50), 1)

            points = self.getPoints('corner', 'B', 2)  # to find points
            a1 = np.array([[[points[0], points[1]], [points[2], points[3]],
                            [points[4], points[5]], [points[6], points[7]]]],
                          dtype=np.int32)
            cv2.fillPoly(frame, a1,
                         self.getColor(cornerBuffer[cornerCounter][2]))
            cv2.polylines(frame, a1, True, (50, 50, 50), 1)

            #target
            points = self.getPoints('corner', cornerIndex[cornerCounter][0],
                                    int(cornerIndex[cornerCounter][1]))
            a1 = np.array([[[points[0], points[1]], [points[2], points[3]],
                            [points[4], points[5]], [points[6], points[7]]]],
                          dtype=np.int32)
            cv2.fillPoly(frame, a1, (255, 0, 255))
            cv2.polylines(frame, a1, True, (50, 50, 50), 1)

            points = self.getPoints('corner', cornerIndex[cornerCounter][2],
                                    int(cornerIndex[cornerCounter][3]))
            a1 = np.array([[[points[0], points[1]], [points[2], points[3]],
                            [points[4], points[5]], [points[6], points[7]]]],
                          dtype=np.int32)
            cv2.fillPoly(frame, a1, (50, 50, 50))
            cv2.polylines(frame, a1, True, (50, 50, 50), 1)

            points = self.getPoints('corner', cornerIndex[cornerCounter][4],
                                    int(cornerIndex[cornerCounter][5]))
            a1 = np.array([[[points[0], points[1]], [points[2], points[3]],
                            [points[4], points[5]], [points[6], points[7]]]],
                          dtype=np.int32)
            cv2.fillPoly(frame, a1, (50, 50, 50))
            cv2.polylines(frame, a1, True, (50, 50, 50), 1)

            cv2.imshow("Result", frame)

            key = cv2.waitKey(1)
            if key == 27:  #esc key
                break
            if key == 81:  #left arrow key
                if current == 'edge':
                    if (edgeCounter == len(edgeIndex) - 1):
                        edgeCounter = 0
                    else:
                        edgeCounter = edgeCounter + 1
                elif current == 'corner':
                    if (cornerCounter == len(cornerIndex) - 1):
                        cornerCounter = 0
                    else:
                        cornerCounter = cornerCounter + 1
            if key == 83:  #right arrow key
                if current == 'edge':
                    if (edgeCounter == 0):
                        edgeCounter = len(edgeIndex) - 1
                    else:
                        edgeCounter = edgeCounter - 1
                elif current == 'corner':
                    if (cornerCounter == 0):
                        cornerCounter = len(cornerIndex) - 1
                    else:
                        cornerCounter = cornerCounter - 1
            if key == 82:  #up arrow key
                current = 'edge'
            if key == 84:  #down arrow key
                current = 'corner'

        self.cap.release()
        cv2.destroyAllWindows()