Python Forum
how come i can't stop program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how come i can't stop program
#1
see below for edited python code.

when i press the q key nothing happens but i want to stop the program. help please.
Reply
#2
i found (control + c) workd
Reply
#3
I have another question when the program runs it presses the w key on the keyboard constantly, but I want it to only run for a few seconds then pause then run again until I press ctrl+c to stop the program.
Reply
#4
I have my new code but still i do not know how to stop the program when its running by pressing the q button?

import cv2
import numpy as np
from pynput.keyboard import Key, Controller

keyboard = Controller()

up = 0
down = 0
left = 0
right = 0

number_one = []
number_two = []
number_three = []

def increment_up_down(up, down):
    if(up == 0 and down == 0):
        down = 40
        return up, down
    else:
        up = up + 40
        down = down + 40
        return up, down
    

def increment_left_right(left, right):
    if(left == 0 and right == 0):
        right = 40
        return left, right
    else:
        left = left + 40
        right = right + 40
        return left, right
    

def find_the_average_color_of_an_image(region_of_interest):
    #
    #  Find the average color in an image
    #

    height, width, _ = np.shape(region_of_interest)

    # calculate the average color of each row of our image
    avg_color_per_row = np.average(region_of_interest, axis=0)

    # calculate the averages of our rows
    avg_colors = np.average(avg_color_per_row, axis=0)

    # avg_color is a tuple in BGR order of the average colors
    # but as float values
    #print(f'avg_colors: {avg_colors}')

    # so, convert that array to integers
    one, two, three = np.array(avg_colors, dtype=np.uint8)
    int_averages = np.array(avg_colors, dtype=np.uint8)
    #print(f'int_averages: {int_averages}')

    return one, two, three

cap = cv2.VideoCapture(1)
cap.set(3, 1920) # width is ID number 3
cap.set(4, 1080) # height is ID number 4

while True:
    success, frame = cap.read()
    # If the frame was not read successfully, break the loop
    if not success:
        break
  
    # Crop a region of interest (ROI) from the frame
    for i in range(27):
        up, down = increment_up_down(up, down)   
    
        counter = 1
        for j in range(48):
            left, right = increment_left_right(left, right)
            # Crop a region of interest (ROI) from the frame
            region_of_interest = frame[up:down, left:right] 
            one = 0
            two = 0
            three = 0
            one, two, three = find_the_average_color_of_an_image(region_of_interest)
            number_one.append(one)
            number_two.append(two)
            number_three.append(three)
        left = 0
        right = 0
    up = 0
    down = 0
    
    number_one_minValue = min(number_one)
    number_one_maxValue = max(number_one)
    
    number_two_minValue = min(number_two)
    number_two_maxValue = max(number_two)
    
    number_three_minValue = min(number_three)
    number_three_maxValue = max(number_three)
        
    if ((number_one_maxValue - number_one_minValue) < 100) and ((number_two_maxValue - number_two_minValue) < 100) and ((number_three_maxValue - number_three_minValue) < 100):
        keyboard.release('w')
    else:
        keyboard.press('w')
        
    number_one.clear()
    number_two.clear()
    number_three.clear()
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
Reply
#5
Did you look at the example for monitoring the keyboard?

https://pynput.readthedocs.io/en/latest/keyboard.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to programmatically stop a program in Jupyter Notebook? Mark17 11 37,247 Feb-12-2023, 01:41 PM
Last Post: jp21in
  How do I stop this fast factorization program from printing beyond the 3rd output? Pleiades 6 3,878 Dec-07-2019, 08:43 PM
Last Post: Pleiades

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020