Created
March 30, 2024 17:22
-
-
Save mirono/91fd044cb449f06f23e6949298052deb to your computer and use it in GitHub Desktop.
Detcting commercials and controlling the TV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tv import TV | |
import time | |
import cv2 | |
import pytesseract | |
import numpy as np | |
from PIL import ImageGrab | |
device_id = "<YOUR TV ID>" | |
access_token = "<YOUR ACCESS TOKEN>" | |
# Area on the screen to look for commercials break text | |
bounding_box = (55, 115, 280, 310) | |
# Commercials related texts | |
commercials_text = ["חוזרים", "בעוד"] | |
# How much time to wait after commercials end - this will prevent false unmutes if text is not recognized momentarily | |
unmute_delay = 5 | |
commercials_frames_needed = 3 | |
tv = TV(access_token, device_id) | |
options = "-l heb --oem 3 --psm 12 tessedit_char_whitelist=01234567890" | |
last_commercials_seen = 0 | |
commercials_seen = 0 | |
while True: | |
cap_scr = np.array(ImageGrab.grab(bounding_box)) | |
cap_scr = cv2.cvtColor(cap_scr, cv2.COLOR_RGB2BGR) | |
# extracted_text = pytesseract.image_to_string(cap_scr, config=options) | |
# print(extracted_text) | |
# Perform bounding box detection using Tesseract's built-in capabilities | |
d = pytesseract.image_to_data(cap_scr, config=options, output_type=pytesseract.Output.DICT) | |
n_boxes = len(d['text']) | |
for i in range(n_boxes): | |
if int(d['conf'][i]) > 50: | |
if d['text'][i] in commercials_text: | |
(x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i]) | |
cap_scr = cv2.rectangle(cap_scr, (x, y), (x + w, y + h), (0, 255, 0), 2) | |
print(f"Found: {d['text'][i]} with conf {d['conf'][i]}") | |
last_commercials_seen = time.time() | |
commercials_seen += 1 | |
if commercials_seen > commercials_frames_needed: | |
tv.mute() | |
if time.time() - last_commercials_seen > unmute_delay and commercials_seen > 0: | |
commercials_seen = 0 | |
tv.mute(False) | |
cv2.imshow("IP Camera Stream", cap_scr) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment