示例#1
0
def control(ball, paddle):
    if ball in range(paddle[0], paddle[1]):
        pyKey.releaseKey("UP")
        pyKey.releaseKey("DOWN")
    elif ball > paddle[1]:
        pyKey.releaseKey("UP")
        pyKey.pressKey("DOWN")
    elif ball < paddle[0]:
        pyKey.releaseKey("DOWN")
        pyKey.pressKey("UP")
示例#2
0
def walk_loop():
    while True:
        pressKey('D')
        sleep(10)
        releaseKey('D')
        sleep(0.5)
        pressKey('A')
        sleep(10)
        releaseKey('A')
        sleep(0.5)
示例#3
0
def leer_mensaje_nuevo(client, userdata, msg):
    mensaje = msg.payload.decode('UTF-8')
    # print(msg.topic + " " + str(msg.payload))
    print(msg.topic + " " + mensaje)
    comando = CMD.get(mensaje, 'nanai de la china')
    print('tecla -> {}'.format(comando))
    # Pulsacion de teclado
    #press(comando, 1)
    pressKey(comando)
    sleep(0.125)
    releaseKey(comando)
示例#4
0
def main():

    graph, sess = load_graph(pre_trained_model_path)
    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    mp = _mp.get_context("spawn")
    v = mp.Value('i', 0)
    lock = mp.Lock()

    while True:
        key = cv2.waitKey(10)
        if key == ord("q"):
            break
        _, frame = cap.read()
        frame = cv2.flip(frame, 1)
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        boxes, scores, classes = detect_hands(frame, graph, sess)
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
        results = predict(boxes, scores, classes, threshold, width, height)

        if len(results) == 1:
            x_min, x_max, y_min, y_max, category = results[0]
            x = int((x_min + x_max) / 2)
            y = int((y_min + y_max) / 2)
            cv2.circle(frame, (x, y), 5, RED, -1)

            if category == "Closed":
                action = 0  # Do nothing
                text = "Run"

            elif category == "Open" and y < height / 2:
                action = 1  # Jump
                pressKey('UP')
                text = "Jump"

            elif category == "Open" and y > height / 2:
                action = 2
                pressKey('DOWN')
                text = "Duck"

            with lock:
                v.value = action
            cv2.putText(frame, "{}".format(text), (x_min, y_min - 5),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, GREEN, 2)

        overlay = frame.copy()
        cv2.rectangle(overlay, (0, 0), (width, int(height / 2)), ORANGE, -1)
        cv2.addWeighted(overlay, alpha, frame, 1 - alpha, 0, frame)
        cv2.imshow('Detection', frame)

    cap.release()
    cv2.destroyAllWindows()
def start_capturing(white_pixels, wait_interval, wait_frames, opencv_wait_ms):
    frame_counter = 0

    with mss.mss() as sct:
        monitor = {"top": 0, "left": 0, "width": 200, "height": 50}
        while True:
            img = numpy.array(sct.grab(monitor))
            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

            # print(img[40][184], img[40][177], img[40][172], img[45][177], img[45][172], img[45][165])
            # print(img[40])

            for pixel in white_pixels:
                x, y = pixel[0], pixel[1]
                if(img[x][y] < 250):
                    frame_counter = 0
                    break
            else:
                frame_counter += 1
            

            if(frame_counter > wait_frames):
                print("trigger")
                time.sleep(wait_interval)
                print("keypress")
                pressKey(key="s")
                releaseKey(key="s")
                sys.exit()


            # print pixels
            for pixel in white_pixels:
                x, y = pixel[0], pixel[1]
                print(img[x][y], end="-")
            print("")


            # Display the picture
            if(opencv_wait_ms):
                cv2.imshow("OpenCV/Numpy normal", img)

                # Press "q" to quit
                cv2.waitKey(opencv_wait_ms)
示例#6
0
from pyKey import pressKey, releaseKey, press, sendSequence, showKeys
from time import *
sleep(4)

#if True:
while True:
    pressKey("UP")
    sleep(1)
    pressKey("DEL")
    sleep(3)
    releaseKey("DEL")
    sleep(2)
    pressKey("RIGHT")
    sleep(0.4)
    releaseKey("RIGHT")
    sleep(1)
    pressKey("LEFT")
    sleep(0.1)
    releaseKey("LEFT")
    sleep(1)
    pressKey("LEFT")
    sleep(0.9)
    releaseKey("LEFT")
    sleep(1.5)
    pressKey("RIGHT")
    sleep(0.5)
    releaseKey("RIGHT")
    sleep(3)
    sleep(1.1)
    pressKey("LEFT")
    sleep(2)
def main():
    
    graph, sess = load_graph(pre_trained_model_path)
    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    mp = _mp.get_context("spawn")
    v = mp.Value('i', 0)
    lock = mp.Lock()
    
    while True:
        key = cv2.waitKey(10)
        if key == ord("q"):
            break
        _, frame = cap.read()
        frame = cv2.flip(frame, 1)
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        boxes, scores, classes = detect_hands(frame, graph, sess)
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
        results = predict(boxes, scores, classes, threshold, width, height)

        if len(results) == 1:
            x_min, x_max, y_min, y_max, category = results[0]
            x = int((x_min + x_max) / 2)
            y = int((y_min + y_max) / 2)
            cv2.circle(frame, (x, y), 5, RED, -1)

            if category == "Open" and x <= width / 3:
                action = 7  # Left move
                text = "Left move"
                releaseKey("LEFT")
                press('LEFT', 0.15)
                pressKey("UP")
            
            elif category == "Open" and width / 3 < x <= 2 * width / 3:
                action = 5  # Straight
                releaseKey('LEFT')
                releaseKey("RIGHT")
                pressKey("UP")
                text = "Straight"
            
            elif category == "Open" and x > 2 * width / 3:
                action = 2  # Right move
                text = "Right move"
                releaseKey("RIGHT")
                press("RIGHT", 0.15)
                pressKey('UP')
                
            else:
                action = 0
                text = "Go"
            
            with lock:
                v.value = action
            cv2.putText(frame, "{}".format(text), (x_min, y_min - 5),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, GREEN, 2)
            
        overlay = frame.copy()
        cv2.rectangle(overlay, (0, 0), (int(width / 3), height), ORANGE, -1)
        cv2.rectangle(overlay, (int(2 * width / 3), 0), (width, height), ORANGE, -1)
        cv2.addWeighted(overlay, alpha, frame, 1 - alpha, 0, frame)
        cv2.imshow('Detection', frame)
        
    cap.release()
    cv2.destroyAllWindows()