Beispiel #1
0
  def run(self, image, display_image):
    from arrow import draw_arrow
    image = cv2.resize(image, (n_cols, n_rows))
    self.images.append(image)
    category = None
    if len(self.images) == n_files: 
      try:
        predict = session.run(model_predict, { model_input: [self.images], model_keep_prob: 1.0 } )
        category = ind2cat(predict[0])
        draw_arrow(display_image, category)
      except ValueError:
        category = None
      self.images.popleft()

    return category
Beispiel #2
0
    def paintEvent(self, event):
        self.board_size = min(self.width(), self.height())
        self.tile_size = self.board_size / 8

        painter = QPainter()

        painter.begin(self)
        painter.setRenderHint(QPainter.Antialiasing)

        self.board_svg.render(painter,
                              QRectF(0, 0, self.board_size, self.board_size))

        if self.selected != None:
            painter.setBrush(QBrush(QColor(0, 120, 255, 100), Qt.SolidPattern))
            painter.setPen(QPen(QColor(0, 120, 255, 100), Qt.SolidLine))
            sx = self.selected[0]
            sy = self.selected[1]
            painter.drawRect(sx * self.tile_size, sy * self.tile_size,
                             self.tile_size, self.tile_size)

            for f in range(len(self.files)):
                for r in range(len(self.ranks)):
                    if sx == f and sy == r:
                        continue
                    if self.islegalmove(sx, sy, f, r):
                        x = f * self.tile_size + self.tile_size / 4
                        y = r * self.tile_size + self.tile_size / 4
                        painter.drawEllipse(x, y, self.tile_size / 2,
                                            self.tile_size / 2)

        for f in range(len(self.files)):
            for r in range(len(self.ranks)):
                sn = self.files[f] + self.ranks[r]
                s = chess.parse_square(sn)
                piece = self.board.piece_at(s)
                if piece != None:
                    ps = piece.symbol()
                    color = WHITE if ps.isupper() else BLACK
                    renderer = self.piece_images[color + ps.upper()]
                    bounds = QRectF(f * self.tile_size, r * self.tile_size,
                                    self.tile_size, self.tile_size)
                    renderer.render(painter, bounds)

        for arrow in self.arrows:
            fx = arrow[0] * self.tile_size + self.tile_size / 2
            fy = arrow[1] * self.tile_size + self.tile_size / 2
            tx = arrow[2] * self.tile_size + self.tile_size / 2
            ty = arrow[3] * self.tile_size + self.tile_size / 2

            painter.setBrush(QBrush(QColor(34, 139, 34, 130), Qt.SolidPattern))
            painter.setPen(Qt.NoPen)

            painter.drawPolygon(draw_arrow(fx, fy, tx, ty, 40, 10))

        if self.best_move != None and self.show_best_move:
            bm = self.best_move
            fx, fy = self.coordsofsquare(bm.from_square)
            tx, ty = self.coordsofsquare(bm.to_square)

            fx = fx * self.tile_size + self.tile_size / 2
            fy = fy * self.tile_size + self.tile_size / 2
            tx = tx * self.tile_size + self.tile_size / 2
            ty = ty * self.tile_size + self.tile_size / 2

            painter.setBrush(QBrush(QColor(0, 92, 153, 130), Qt.SolidPattern))
            painter.setPen(Qt.NoPen)

            painter.drawPolygon(draw_arrow(fx, fy, tx, ty, 70, 15))

        painter.end()

        self.update()
def update(table, viewAngleHorz, deltaTime) :
    theta, dist = 0.0, 0.0
    #print "update()"

#    while True :
    ret, img = camera.read() # img.shape 640x480 image
    #print "ret: " + str(ret) + "\n"
    #ret = True
    #img = cv2.imread("c:\Untitled.tiff")
    #if not ret : print "oops!\n"
    #elif
    #    cv2.imshow('input',img)

    # Convert to hsv img
    #hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    h, s, v = rgb_to_hsl_hsv(img, True)
    #cv2.imshow("v", v)
    thresh = 255
    im_bw = cv2.threshold(v, 240, 255, cv2.THRESH_BINARY)[1]
    #cv2.imshow("im_bw",im_bw)

    #
    if False :
        rg = better_way(img)
        cv2.imshow('huh', rg)
    #rg1 = cv2.cvtColor(rg, cv2.COLOR_BGR2HSV)
    #hsv = cv2.cvtColor(rg, cv2.COLOR_BGR2HSV)

    #  Keep only green objects
    # higher s = less white, higher v = less black
    #invent sliders to adjust
    #lowerGreen = np.array([70, 70, 180])
    #upperGreen = np.array([110, 130,255])
    if False :
        lg_h, lg_s, lg_v = 0, 0, 254
        ug_h, ug_s, ug_v = 255, 255, 255
        #lg_h, lg_s, lg_v = 100, 6, 67
        #ug_h, ug_s, ug_v = 158, 98, 87
        lowerGreen = np.array([lg_h, lg_s, lg_v])
        upperGreen = np.array([ug_h, ug_s, ug_v])
        filteredGreen = cv2.inRange(hsv, lowerGreen, upperGreen)
        #cv2.imshow('fgreen',filteredGreen)

    if False :
        filteredGreen = rg
        #cv2.cvtColor(rg, cv2.COLOR_RGB2GRAY, filteredGreen, 1)
        #cv2.cvtColor(filteredGreen, cv2.COLOR_GRAY2RGBA, filteredGreen, 1)
        #rg.convertTo(filteredGreen, CV_8UC1)
        #try as hard as heck to convert to happy format for findContours...
        min_t = np.array((199, 199, 199))
        max_t = np.array((201, 201, 201))
        filteredGreen = cv2.inRange(rg, min_t, max_t)
    filteredGreen = im_bw
    #cv2.imshow('bigfilter',filteredGreen)

    # Filter out small objects
    filteredGreen = cv2.morphologyEx(filteredGreen, cv2.MORPH_OPEN, np.ones((3, 3)))
    #cv2.imshow('fgreens',filteredGreen)

    # Find all contours, counter is vector of points that are connected to make up a shape
    contours, hierarchy = cv2.findContours(filteredGreen, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    # Resetting values
    foundHotTarget = False
    foundVertTarget = False

    # work around subtle bug - see StackOverflow
    # http://stackoverflow.com/questions/13337058/data-type-error-with-drawcontours-unless-i-pickle-unpickle-first
    tmp = cPickle.dumps(contours)
    contours = cPickle.loads(tmp)

    possibleHotTarget = Rectangle(0,0,0,0)
    possibleHotArea = possibleHotTarget.getArea()

    possibleVertTarget1 = Rectangle(0,0,0,0)
    possibleVertTarget2 = Rectangle(0,0,0,0)

    # Parse contours, calculate distance and check if it's the hot target
    for shape in contours:
        #target = Rectangle(*cv2.boundingRect(shape))
        x,y,w,h = cv2.boundingRect(shape)
        target = Rectangle(x,y,w,h)

        # Filter out small contours
        if target.getArea() > 300 and target.getArea() < 30000:

            # Compensate for horizontal view of the target
            #target.width = target.width / math.cos(viewAngle * math.PI / 180)
            #target.width = target.width / math.cos(viewAngle * 3.14159 / 180)

            # MPH improvements: - width / height (23.5/4) +-15% then possibly the hot target
            #  If the width of the target is greater than its height then it's probably the hot target
            hotTargetRatio = 23.5 / 4
            hotTargetMargin = 0.25 # percent error margin
            if (target.width>target.height) and  (target.height * (hotTargetRatio)*(1+hotTargetMargin)) >= target.width >= (target.height * ((hotTargetRatio)*(1-hotTargetMargin))):
                foundHotTarget = True
                if target.getArea() > possibleHotArea :
                    possibleHotTarget = target
                    possibleHotArea = target.getArea()
                #distance = computeDistance(horizTarget.height, target.height)

            # MPH improvements: - height / width (32/4) +-15% then possibly the hot target
            # If the height of the target is greater than its width its probably a vert target
            vertTargetRatio = 32 / 4
            vertTargetMargin = 0.30
            if (target.height>target.width) and (target.width * (vertTargetRatio)*(1+vertTargetMargin)) >= target.height >= (target.width * ((vertTargetRatio)*(1-vertTargetMargin))):
                foundVertTarget = True

                #maintain up to two top-ranked vertical targets
                if possibleVertTarget2.getArea() > possibleVertTarget1.getArea :
                    temp = possibleVertTarget1
                    possibleVertTarget1 = possibleVertTarget2
                    possibleVertTarget2 = temp
                if target.getArea() > possibleVertTarget1.getArea() :
                    possibleVertTarget2 = possibleVertTarget1
                    possibleVertTarget1 = target
                elif target.getArea() > possibleVertTarget1.getArea() :
                    possibleVertTarget2 = target

    #vt1loc = (50,440)
    #vtrloc = (580,440)
    #cv2.putText(img,"Hello World!!!", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
    #img, text, textOrg, fontFace, fontScale, thickness, baseline?
    #cv2.putText
    #cv2.putText(img, "Hi again", vtlloc, cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), thickness=2)
    #lineType= cv2.CV_AA

    if debugMode:
        if possibleHotArea > 0 :
            drawRect(img, possibleHotTarget)
            drawXonTarget(img, possibleHotTarget)
            viewAngle = computeAngle(horizTarget.height, possibleHotTarget.height, 228)
            distanceHot = computeDistance(horizTarget.height, possibleHotTarget.height)

            #print "Distance: ", round(distanceHot), ", Hot Target", viewAngle, viewAngleVert, ", width: ", possibleHotTarget.width, ", height", possibleHotTarget.height
            #print "Distance: ", round(distanceHot / 12), ", Hot Target", viewAngle, viewAngleVert, ", width: ", \
            #    possibleHotTarget.width, ", height", possibleHotTarget.height
    haveHotTarget = False
    if possibleHotArea > 0 :
        haveHotTarget = True
    table.PutBoolean('hotTarget', haveHotTarget)

    if debugMode:
        lString = "L:"
        rString = "R:"
        v1DistStr = ""
        v2DistStr = ""
        if possibleVertTarget1.getArea() > 0 :
            drawRect(img, possibleVertTarget1)
            drawXonTarget(img, possibleVertTarget1)
            viewAngle = computeAngle(vertTarget.height, possibleVertTarget1.height, 228)
            distanceVert1 = computeDistance(vertTarget.height, possibleVertTarget1.height)
            distanceVert1AsFeet = distanceVert1 / 12
            #f(x)= 1.29x -0.09 - per Nathan
            #distanceVert1AsFeet = (1.29 * distanceVert1AsFeet) - 0.09
            v1DistStr = str(round(distanceVert1AsFeet))
            #print "Distance: ", round(distanceVert1 / 12), ", Vert Target 1", viewAngle, viewAngleVert, ", width: ", \
            #    possibleVertTarget1.width, ", height", possibleVertTarget1.height
        if possibleVertTarget2.getArea() > 0 :
            drawRect(img, possibleVertTarget2)
            drawXonTarget(img, possibleVertTarget2)
            viewAngle = computeAngle(vertTarget.height, possibleVertTarget2.height, 228)
            distanceVert2 = computeDistance(vertTarget.height, possibleVertTarget2.height)
            distanceVert2AsFeet = distanceVert2 / 12
            #f(x)= 1.29x -0.09 - per Nathan
            #distanceVert2AsFeet = (1.29 * distanceVert2AsFeet) - 0.09
            v2DistStr = str(round(distanceVert2/12))
            #print "Distance: ", round(distanceVert2 / 12), ", Vert Target 1", viewAngle, viewAngleVert, ", width: ", \
            #    possibleVertTarget2.width, ", height", possibleVertTarget2.height

        if (possibleVertTarget1.getArea()>0) and (possibleVertTarget1.getArea()>0) :
            if (possibleVertTarget1.x+(possibleVertTarget1.width)/2) > (possibleVertTarget2.x+(possibleVertTarget2.width)/2) :
                tmpStr = v2DistStr
                v2DistStr = v1DistStr
                v2DistStr = tmpStr

        dist = 0
        if (possibleVertTarget1.getArea()>0) :
            dist = distanceVert1
        lString = lString + v1DistStr
        rString = rString + v2DistStr
        fpsString = "FPS: "
        fpsString = fpsString + str(round(1/deltaTime))
        cv2.putText(img, lString, (10, 440), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), thickness=2)
        cv2.putText(img, rString, (510, 440), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), thickness=2)
        cv2.putText(img, fpsString, (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), thickness=2)

    if debugMode:
        #arrow.draw_arrow(img, (320, 240), (100, 150), (100, 0, 255), 20);

        throttleArrow = throttleValue(dist)
        if throttleArrow != 0 :
            arrow.draw_arrow(img, (320, 380), (320, int(380 - (50*throttleArrow))), (200, 0, 255), 20);

        cv2.imshow("color", img)
        cv2.waitKey(1)
        #cv2.imshow("filtered", filteredGreen)
        #cv2.waitKey(10)

    if cv2.waitKey(1) == 27:
        exit(0)

    return theta, dist
def update(table, viewAngleHorz, deltaTime):
    theta, dist = 0.0, 0.0
    #print "update()"

    #    while True :
    ret, img = camera.read()  # img.shape 640x480 image
    #print "ret: " + str(ret) + "\n"
    #ret = True
    #img = cv2.imread("c:\Untitled.tiff")
    #if not ret : print "oops!\n"
    #elif
    #    cv2.imshow('input',img)

    # Convert to hsv img
    #hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    h, s, v = rgb_to_hsl_hsv(img, True)
    #cv2.imshow("v", v)
    thresh = 255
    im_bw = cv2.threshold(v, 240, 255, cv2.THRESH_BINARY)[1]
    #cv2.imshow("im_bw",im_bw)

    #
    if False:
        rg = better_way(img)
        cv2.imshow('huh', rg)
    #rg1 = cv2.cvtColor(rg, cv2.COLOR_BGR2HSV)
    #hsv = cv2.cvtColor(rg, cv2.COLOR_BGR2HSV)

    #  Keep only green objects
    # higher s = less white, higher v = less black
    #invent sliders to adjust
    #lowerGreen = np.array([70, 70, 180])
    #upperGreen = np.array([110, 130,255])
    if False:
        lg_h, lg_s, lg_v = 0, 0, 254
        ug_h, ug_s, ug_v = 255, 255, 255
        #lg_h, lg_s, lg_v = 100, 6, 67
        #ug_h, ug_s, ug_v = 158, 98, 87
        lowerGreen = np.array([lg_h, lg_s, lg_v])
        upperGreen = np.array([ug_h, ug_s, ug_v])
        filteredGreen = cv2.inRange(hsv, lowerGreen, upperGreen)
        #cv2.imshow('fgreen',filteredGreen)

    if False:
        filteredGreen = rg
        #cv2.cvtColor(rg, cv2.COLOR_RGB2GRAY, filteredGreen, 1)
        #cv2.cvtColor(filteredGreen, cv2.COLOR_GRAY2RGBA, filteredGreen, 1)
        #rg.convertTo(filteredGreen, CV_8UC1)
        #try as hard as heck to convert to happy format for findContours...
        min_t = np.array((199, 199, 199))
        max_t = np.array((201, 201, 201))
        filteredGreen = cv2.inRange(rg, min_t, max_t)
    filteredGreen = im_bw
    #cv2.imshow('bigfilter',filteredGreen)

    # Filter out small objects
    filteredGreen = cv2.morphologyEx(filteredGreen, cv2.MORPH_OPEN,
                                     np.ones((3, 3)))
    #cv2.imshow('fgreens',filteredGreen)

    # Find all contours, counter is vector of points that are connected to make up a shape
    contours, hierarchy = cv2.findContours(filteredGreen, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)

    # Resetting values
    foundHotTarget = False
    foundVertTarget = False

    # work around subtle bug - see StackOverflow
    # http://stackoverflow.com/questions/13337058/data-type-error-with-drawcontours-unless-i-pickle-unpickle-first
    tmp = cPickle.dumps(contours)
    contours = cPickle.loads(tmp)

    possibleHotTarget = Rectangle(0, 0, 0, 0)
    possibleHotArea = possibleHotTarget.getArea()

    possibleVertTarget1 = Rectangle(0, 0, 0, 0)
    possibleVertTarget2 = Rectangle(0, 0, 0, 0)

    # Parse contours, calculate distance and check if it's the hot target
    for shape in contours:
        #target = Rectangle(*cv2.boundingRect(shape))
        x, y, w, h = cv2.boundingRect(shape)
        target = Rectangle(x, y, w, h)

        # Filter out small contours
        if target.getArea() > 300 and target.getArea() < 30000:

            # Compensate for horizontal view of the target
            #target.width = target.width / math.cos(viewAngle * math.PI / 180)
            #target.width = target.width / math.cos(viewAngle * 3.14159 / 180)

            # MPH improvements: - width / height (23.5/4) +-15% then possibly the hot target
            #  If the width of the target is greater than its height then it's probably the hot target
            hotTargetRatio = 23.5 / 4
            hotTargetMargin = 0.25  # percent error margin
            if (target.width > target.height) and (
                    target.height * (hotTargetRatio) *
                (1 + hotTargetMargin)) >= target.width >= (target.height * (
                    (hotTargetRatio) * (1 - hotTargetMargin))):
                foundHotTarget = True
                if target.getArea() > possibleHotArea:
                    possibleHotTarget = target
                    possibleHotArea = target.getArea()
                #distance = computeDistance(horizTarget.height, target.height)

            # MPH improvements: - height / width (32/4) +-15% then possibly the hot target
            # If the height of the target is greater than its width its probably a vert target
            vertTargetRatio = 32 / 4
            vertTargetMargin = 0.30
            if (target.height > target.width) and (
                    target.width * (vertTargetRatio) *
                (1 + vertTargetMargin)) >= target.height >= (target.width * (
                    (vertTargetRatio) * (1 - vertTargetMargin))):
                foundVertTarget = True

                #maintain up to two top-ranked vertical targets
                if possibleVertTarget2.getArea() > possibleVertTarget1.getArea:
                    temp = possibleVertTarget1
                    possibleVertTarget1 = possibleVertTarget2
                    possibleVertTarget2 = temp
                if target.getArea() > possibleVertTarget1.getArea():
                    possibleVertTarget2 = possibleVertTarget1
                    possibleVertTarget1 = target
                elif target.getArea() > possibleVertTarget1.getArea():
                    possibleVertTarget2 = target

    #vt1loc = (50,440)
    #vtrloc = (580,440)
    #cv2.putText(img,"Hello World!!!", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
    #img, text, textOrg, fontFace, fontScale, thickness, baseline?
    #cv2.putText
    #cv2.putText(img, "Hi again", vtlloc, cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), thickness=2)
    #lineType= cv2.CV_AA

    if debugMode:
        if possibleHotArea > 0:
            drawRect(img, possibleHotTarget)
            drawXonTarget(img, possibleHotTarget)
            viewAngle = computeAngle(horizTarget.height,
                                     possibleHotTarget.height, 228)
            distanceHot = computeDistance(horizTarget.height,
                                          possibleHotTarget.height)

            #print "Distance: ", round(distanceHot), ", Hot Target", viewAngle, viewAngleVert, ", width: ", possibleHotTarget.width, ", height", possibleHotTarget.height
            #print "Distance: ", round(distanceHot / 12), ", Hot Target", viewAngle, viewAngleVert, ", width: ", \
            #    possibleHotTarget.width, ", height", possibleHotTarget.height
    haveHotTarget = False
    if possibleHotArea > 0:
        haveHotTarget = True
    table.PutBoolean('hotTarget', haveHotTarget)

    if debugMode:
        lString = "L:"
        rString = "R:"
        v1DistStr = ""
        v2DistStr = ""
        if possibleVertTarget1.getArea() > 0:
            drawRect(img, possibleVertTarget1)
            drawXonTarget(img, possibleVertTarget1)
            viewAngle = computeAngle(vertTarget.height,
                                     possibleVertTarget1.height, 228)
            distanceVert1 = computeDistance(vertTarget.height,
                                            possibleVertTarget1.height)
            distanceVert1AsFeet = distanceVert1 / 12
            #f(x)= 1.29x -0.09 - per Nathan
            #distanceVert1AsFeet = (1.29 * distanceVert1AsFeet) - 0.09
            v1DistStr = str(round(distanceVert1AsFeet))
            #print "Distance: ", round(distanceVert1 / 12), ", Vert Target 1", viewAngle, viewAngleVert, ", width: ", \
            #    possibleVertTarget1.width, ", height", possibleVertTarget1.height
        if possibleVertTarget2.getArea() > 0:
            drawRect(img, possibleVertTarget2)
            drawXonTarget(img, possibleVertTarget2)
            viewAngle = computeAngle(vertTarget.height,
                                     possibleVertTarget2.height, 228)
            distanceVert2 = computeDistance(vertTarget.height,
                                            possibleVertTarget2.height)
            distanceVert2AsFeet = distanceVert2 / 12
            #f(x)= 1.29x -0.09 - per Nathan
            #distanceVert2AsFeet = (1.29 * distanceVert2AsFeet) - 0.09
            v2DistStr = str(round(distanceVert2 / 12))
            #print "Distance: ", round(distanceVert2 / 12), ", Vert Target 1", viewAngle, viewAngleVert, ", width: ", \
            #    possibleVertTarget2.width, ", height", possibleVertTarget2.height

        if (possibleVertTarget1.getArea() >
                0) and (possibleVertTarget1.getArea() > 0):
            if (possibleVertTarget1.x + (possibleVertTarget1.width) / 2) > (
                    possibleVertTarget2.x + (possibleVertTarget2.width) / 2):
                tmpStr = v2DistStr
                v2DistStr = v1DistStr
                v2DistStr = tmpStr

        dist = 0
        if (possibleVertTarget1.getArea() > 0):
            dist = distanceVert1
        lString = lString + v1DistStr
        rString = rString + v2DistStr
        fpsString = "FPS: "
        fpsString = fpsString + str(round(1 / deltaTime))
        cv2.putText(img,
                    lString, (10, 440),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    1, (0, 0, 255),
                    thickness=2)
        cv2.putText(img,
                    rString, (510, 440),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    1, (0, 0, 255),
                    thickness=2)
        cv2.putText(img,
                    fpsString, (10, 40),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    1, (0, 0, 255),
                    thickness=2)

    if debugMode:
        #arrow.draw_arrow(img, (320, 240), (100, 150), (100, 0, 255), 20);

        throttleArrow = throttleValue(dist)
        if throttleArrow != 0:
            arrow.draw_arrow(img, (320, 380),
                             (320, int(380 - (50 * throttleArrow))),
                             (200, 0, 255), 20)

        cv2.imshow("color", img)
        cv2.waitKey(1)
        #cv2.imshow("filtered", filteredGreen)
        #cv2.waitKey(10)

    if cv2.waitKey(1) == 27:
        exit(0)

    return theta, dist