Each time a number (health) decreased, execute a code [closed]

So basically I’m working on a python script to read health in a game. I use the following libraries.

import numpy as np
import pytesseract
import cv2
from PIL import ImageGrab

I have succeeded to read a health number in real-time screen and change it to int in python. However, I still cannot come out with an idea on how to execute a code whenever my health in the game decreasing or in another term each time my health drop.

conf = r'--oem 3 --psm 6 outputbase digits'
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR\tesseract.exe'

def screenToData():
    while (True):
        screen = np.array(ImageGrab.grab(bbox=(350, 150, 550, 300)))
        cv2.imshow("window", cv2.cvtColor(screen, cv2.COLOR_BGR2RGB))
        data = pytesseract.image_to_data(cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY), lang='eng', config=conf)
        # print(data)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

        for x, box in enumerate(data.splitlines()):
            if x != 0:
                box = box.split()
                # print(box)
                if len(box) == 12:
                    if int(box[11]) == 100:
                        print("Full health")
                    elif int(box[11]) <= 100:
                        print("Nope")


screenToData()

Answer

First step, in your code you should have a variable called health, this meakes the code more readable.

To determine when your health is dropping, each time you loop you just need to compare your current health to the last reading, so maybe add a variable new_health to compare to health.

health = 100

while True:
    new_health = read_health()
    if new_health < health:
        # execute code
    health = new_health