-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckFrigate_noloop.py
69 lines (59 loc) · 2.25 KB
/
checkFrigate_noloop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import requests
import time
from PIL import Image
import imagehash
import io
import os
# Configuration
URL = "http://frigate:5000/api/entree/latest.webp?h=344&cache=1734254308389"
REFERENCE_IMAGE_PATH = "/root/check-frigate/latest.webp" # Local uploaded image
RETRY_DELAY = 5 # Retry delay in seconds
RETRY_COUNT = 5 # Number of additional retries if match is found
# Function: Perform the action if condition is met
def perform_action():
print("[ACTION TRIGGERED] Images match continuously. Performing the action...")
# Example: Replace with actual code for the action
os.system("docker restart frigate")
# Function: Download image from a URL
def download_image(url):
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
return Image.open(io.BytesIO(response.content))
except requests.RequestException as e:
print(f"[ERROR] Failed to fetch image: {e}")
return None
# Function: Compare two images using image hashing
def images_match(image1, image2):
hash1 = imagehash.average_hash(image1)
hash2 = imagehash.average_hash(image2)
return hash1 == hash2
# Main function to check images
def check_images():
print("[STARTING] Checking images...")
reference_image = Image.open(REFERENCE_IMAGE_PATH)
print("[INFO] Checking first image...")
first_image = download_image(URL)
if first_image is None:
print("[WARNING] Could not fetch the first image. Exiting...")
return
# Compare with reference image
if images_match(reference_image, first_image):
print("[MATCH FOUND] First image matches reference. Retrying...")
match = True
# Retry to confirm persistent match
for i in range(RETRY_COUNT):
time.sleep(RETRY_DELAY)
retry_image = download_image(URL)
if retry_image is None or not images_match(reference_image, retry_image):
print(f"[RETRY {i+1}] No match. Condition reset.")
match = False
break
print(f"[RETRY {i+1}] Match confirmed.")
# If all retries match, perform the action
if match:
perform_action()
else:
print("[INFO] No match found.")
if __name__ == "__main__":
check_images()