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