コード例 #1
0
def main():

    for i in list(range(3))[::-1]:
        print(i + 1)
        time.sleep(1)

    while (True):

        screen = grab_screen(region=(300, 350, 850, 640))
        screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
        vertices = np.array([[0, 160], [0, 125], [200, 125], [200, 160]],
                            np.int32)
        #screen = take_part(screen, [vertices])

        #screen = cv2.resize(screen, (100,75))
        cv2.imshow('window1', screen)
        keys = key_check()
        out = grab_keys(keys)
        training_data.append([screen, out])

        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

        if len(training_data) % 5000 == 0:
            print(len(training_data))
            np.save(f_name, training_data)
コード例 #2
0
ファイル: jump_time.py プロジェクト: ZeRoyal/t-rex_runner_bot
def main(macro=template, threshold=0.6):
    #jump_time = 0.56s
    #jump_height = 118
    jump = False
    y_prev = None
    y_h = 600
    while (True):
        screen = grab_screen(region=(300, 350, 650, 540))
        screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)

        im_gray = cv2.GaussianBlur(screen, (5, 5), 0)
        _, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)
        _, ctrs, _ = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL,
                                      cv2.CHAIN_APPROX_SIMPLE)
        rects = [cv2.boundingRect(ctr) for ctr in ctrs]
        rects.sort(key=lambda x: x[0])

        if jump:
            try:
                y = rects[0][1]
                if abs(y - y_start) < 5 and y_prev < y:
                    jump_time = time.time() - jump_time
                    print('jump time', jump_time)
                    print('height', y_start - y_h)
                    jump = False
                    y_h = 600
                if y < y_h:
                    y_h = y
                y_prev = y
            except:
                pass

        k = key_check()

        if 'up' in k and not jump:
            jump_time = time.time()
            y_start = rects[0][1]
            y_prev = y_start
            jump = True
            print('tstart ', jump_time, 'y_start', y_start)
        elif 'R' in k:
            jump = False

        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

        cv2.imshow('xxx', screen)
コード例 #3
0
def main():
    last_time = time.time()
    
    for i in list(range(2))[::-1]:
        print(i+1)
        time.sleep(1)
        
    while(True): 
        screen0 = grab_screen(region=(300,450,650,540))
        vertices = np.array([[0,160],[0,120], [500,120], [500,160]], np.int32)

        l_gray = np.array([140,140,140])
        u_gray = np.array([150,150,150])

        screen0 = cv2.inRange(screen0, l_gray, u_gray)

        gray= screen0

        edges = gray
        #edges = cv2.Canny(gray, thr1, thr2)
        #cv2.imshow('img01', edges)
        edges = cv2.dilate(edges, None)
        #cv2.imshow('img02', edges)
        edges = cv2.erode(edges, None)
        #cv2.imshow('img03', edges)
        
        
        contur_bag = []
        _, conturs, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
        for contur in conturs:
            contur_bag.append([contur, cv2.isContourConvex(contur), cv2.contourArea(contur)])
        
        try:
            max_contur = max(contur_bag, key=lambda contur: contur[2])
        except ValueError:
            continue
        
        mask = np.zeros(edges.shape)
        cv2.fillConvexPoly(mask, max_contur[0], (255))
        #cv2.imshow('imgx', mask)  
                
        mask = cv2.dilate(mask, None, iterations=diliter)
        mask = cv2.erode(mask, None, iterations= eroditer)
        mask = cv2.GaussianBlur(mask, (blur, blur), 2) 
        mask = cv2.resize(mask, (100,75))
        
        cv2.imshow('',mask)
        #moves = list(np.around(modelx.predict([screen.reshape(-1,100,75,1)])[0]))
        moves = (np.argmax(np.around(modelx.predict([mask.reshape(-1,100,75,1)])[0])))
        #print(np.argmax(np.around(modelx.predict([screen.reshape(-1,100,75,1)])[0])))
        #moves= moves[0]
        #print(moves)
        print((modelx.predict([mask.reshape(-1,100,75,1)])[0]))

        if moves == 0 and max(modelx.predict([mask.reshape(-1,100,75,1)])[0]) > 0.7:
            forward()

        elif moves == 1 and max(modelx.predict([mask.reshape(-1,100,75,1)])[0]) > 0.75:
            fl()

        elif moves == 2 and max(modelx.predict([mask.reshape(-1,100,75,1)])[0]) > 0.75:
            fr()

        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break       
コード例 #4
0
def main(macro=template, threshold=0.6):
    last_time = time.time()
    w1, h1 = template[0].shape[::-1]
    w2, h2 = template[1].shape[::-1]
    w3, h3 = template[2].shape[::-1]
    w4, h4 = template[3].shape[::-1]
    w5, h5 = template[4].shape[::-1]
    w6, h6 = template[5].shape[::-1]

    x_prev = None
    nframes = 0
    x0 = 100
    front = 80

    while (True):
        #screen = grab_screen(region=(300,350,650,540))
        screen = grab_screen(region=(300, 450, 750, 550))
        screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)

        im_gray = cv2.GaussianBlur(screen, (5, 5), 0)
        _, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)
        _, ctrs, _ = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL,
                                      cv2.CHAIN_APPROX_SIMPLE)
        rects = [cv2.boundingRect(ctr) for ctr in ctrs]

        rects = list(filter(lambda x: x[0] > front, rects))
        rects.sort(key=lambda x: x[0])

        res = [
            cv2.matchTemplate(screen, t, cv2.TM_CCOEFF_NORMED)
            for t in template
        ]
        loc = [np.where(r >= threshold) for r in res]

        points = []
        for l in loc[2:]:
            points += zip(*l)

        nframes += 1

        try:
            position = loc[0][1][0]
            i, closest = argmin(points)
            x = rects[0][0]
            if x_prev is None:
                x_prev = x
            else:
                dist = x_prev - x
                if dist < 0:
                    nframes = 0
                    x_prev = x
                elif dist > x0:
                    v = dist / nframes
                    nframes = 0
                    x_prev = x
                    print('current speed', v)

            obj = object_type(i, map(lambda x: x[0], loc[2:]))
            d = closest[1] - position
            if obj == 2 or obj == 3:
                k = 50.0 / 120.0
                r = 130 + (time.time() - last_time) * k
                if d < r:
                    yd = loc[0][0][0]
                    yb = closest[0]
                    h = (yd + h1) - (yb + h5)
                    if h + 5 < h2:
                        PressKey(Up)
                    elif h - 10 < h1:
                        PressKey(Down)
                        time.sleep(0.3)
                        ReleaseKey(Down)
            else:
                k = 50.0 / 120.0
                r = 177 + (time.time() - last_time) * k
                if d < r:
                    PressKey(Up)
        except:
            pass

        if len(rects) > 0:
            rect = merge(rects, 50)
            cv2.rectangle(screen, (rect[0], rect[1]),
                          (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0),
                          3)

        if x_prev is not None:
            cv2.rectangle(screen, (x, 0), (x_prev, 100), (0, 255, 0), 3)

        cv2.imshow('xxx', screen)

        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break
コード例 #5
0
ファイル: main.py プロジェクト: ZeRoyal/t-rex_runner_bot
def main(macro = template, threshold= 0.6):
    
    last_time = time.time()
    w1, h1 = template[0].shape[::-1]
    w2, h2 = template[1].shape[::-1]
    w3, h3 = template[2].shape[::-1] 
    w4, h4 = template[3].shape[::-1] 
    w5, h5 = template[4].shape[::-1]    
    w6, h6 = template[5].shape[::-1]
    
    for i in list(range(2))[::-1]:
        print(i+1)
        time.sleep(1)
        
    while(True):

        screen = grab_screen(region=(300,350,650,540))
        screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)


        im_gray = cv2.GaussianBlur(screen, (5, 5), 0)
        ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)
        

        im, ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        rects = [cv2.boundingRect(ctr) for ctr in ctrs]        
        



        res = [cv2.matchTemplate(screen,t,cv2.TM_CCOEFF_NORMED) for t in template]
        loc = [np.where(r >= threshold) for r in res]
        
        points = []
        for l in loc[2:]:
            points += zip(*l)
        #for i in range(len(template[2:])):
        #    points += zip(*loc[i])
        
        try:
            position = loc[0][1][0]
            i, closest = argmin(points)
            obj = object_type(i, map(lambda x: x[0], loc[2:]))
            #print(obj)
            d = closest[1] - position
            if obj == 2 or obj == 3:
                #print('bird', d)
                if d < 130:
                    yd = loc[0][0][0]
                    yb = closest[0]
                    h = (yd + h1) - (yb + h5)
                    #print('bird height', h, h2)
                    if h+5 < h2:
                        PressKey(Up)
                    elif h-10 < h1:
                        PressKey(Down)
                        time.sleep(0.3)
                        ReleaseKey(Down)
            else:
                if d < 180 and time.time()-last_time < 30:
                    PressKey(Up)
                    
                elif d < 185 and time.time()-last_time < 60 and time.time()-last_time > 30:
                    PressKey(Up)

                elif d < 190 and time.time()-last_time > 60:
                    PressKey(Up)                    

        except:
            pass
        
        for t, l in zip(template,loc):
            w, h = t.shape[::-1]
            for pt in zip(*l[::-1]):
                cv2.rectangle(screen, pt, (pt[0] + w, pt[1] + h), (0,0,255), 1)
        


        for rect in rects:

            cv2.rectangle(screen, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3) 

            leng = int(rect[3] * 1.6)
            pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
            pt2 = int(rect[0] + rect[2] // 2 - leng // 2)
            roi = im_th[pt1:pt1+leng, pt2:pt2+leng]
            roi = cv2.resize(roi, (28, 28))

            nbr = list(np.around(modelx.predict([roi.reshape(-1,28,28,1)])[0]))
            print(nbr)

        




        cv2.imshow('xxx',screen)
        #moves = list(np.around(modelx.predict([screen.reshape(1,100,75,1)])[0]))

        

    

        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break       
コード例 #6
0
def main():
    for i in list(range(4))[::-1]:
        print(i + 1)
        time.sleep(1)

    while (True):
        screen0 = grab_screen(region=(300, 450, 650, 540))
        #screen = cv2.cvtColor(screen0, cv2.COLOR_BGR2GRAY)
        #vertices = np.array([[0,160],[0,120], [500,120], [500,160]], np.int32)

        l_gray = np.array([140, 140, 140])
        u_gray = np.array([150, 150, 150])
        #screen0 = take_part(screen0, [vertices])
        screen0 = cv2.inRange(screen0, l_gray, u_gray)
        #cv2.imshow('img000', screen0)
        gray = screen0
        #gray = cv2.cvtColor(screen0,cv2.COLOR_BGR2GRAY)
        #cv2.imshow('img001', gray)
        #gray = cv2.inRange(gray, 110, 160)
        #cv2.imshow('img01', gray)

        edges = gray
        #edges = cv2.Canny(gray, thr1, thr2)
        #cv2.imshow('img01', edges)
        edges = cv2.dilate(edges, None)
        #cv2.imshow('img02', edges)
        edges = cv2.erode(edges, None)
        #cv2.imshow('img03', edges)

        contur_bag = []
        _, conturs, _ = cv2.findContours(edges, cv2.RETR_LIST,
                                         cv2.CHAIN_APPROX_NONE)
        for contur in conturs:
            contur_bag.append(
                [contur,
                 cv2.isContourConvex(contur),
                 cv2.contourArea(contur)])

        try:
            max_contur = max(contur_bag, key=lambda contur: contur[2])
        except ValueError:
            continue

        mask = np.zeros(edges.shape)
        cv2.fillConvexPoly(mask, max_contur[0], (255))
        #cv2.imshow('imgx', mask)

        mask = cv2.dilate(mask, None, iterations=diliter)
        mask = cv2.erode(mask, None, iterations=eroditer)
        mask = cv2.GaussianBlur(mask, (blur, blur), 2)
        cv2.imshow('imgx2', mask)
        '''
        mask_stack_bgr = np.dstack([mask]*3)
        mask_stack_bgr  = mask_stack_bgr.astype('float32') / 255.0           
        img = screen0.astype('float32') / 255.0                 
        
        masked = (mask_stack_bgr * img) + ((1-mask_stack_bgr) * color) 
        masked = (masked * 255).astype('uint8')  
        cv2.imshow('img', masked) 
        '''
        mask = cv2.resize(mask, (100, 75))
        #screen=cv2.Canny(screen,200,300)
        #cv2.imshow('window1', screen)

        try:
            keys = key_check()
            out = grab_keys(keys)
            training_data.append([mask, out])
        except KeyError:
            pass

        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

        if len(training_data) % 200 == 0:
            #print(len(training_data))
            np.save(f_name, training_data)