From 5b395717dc2ce5cca9b00f96b5efe15cb8e6733a Mon Sep 17 00:00:00 2001 From: locker98 Date: Wed, 18 Sep 2024 16:53:59 -0400 Subject: [PATCH] added code --- ocr.py | 46 ++++++++++++++++++++++++++++++++++ screenshot.py | 58 +++++++++++++++++++++++++++++++++++++++++++ updatedscreenshot.py | 59 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100755 ocr.py create mode 100755 screenshot.py create mode 100755 updatedscreenshot.py diff --git a/ocr.py b/ocr.py new file mode 100755 index 0000000..74339d9 --- /dev/null +++ b/ocr.py @@ -0,0 +1,46 @@ +import os +import cv2 +import easyocr + +def perform_ocr(image_path): + # Read the image using OpenCV + img = cv2.imread(image_path) + + # Initialize the OCR reader + reader = easyocr.Reader(['en']) # Use English language + + # Perform text extraction + result = reader.readtext(img) + + # Extract the text from the result + text = "\n".join([entry[1] for entry in result]) + + return text + +def main(): + screenshot_dir = 'screenshots' + output_dir = 'ocr_results' + + # Create output directory if it doesn't exist + if not os.path.exists(output_dir): + os.makedirs(output_dir) + + # Process all PNG files in the screenshots directory + for filename in os.listdir(screenshot_dir): + if filename.endswith('.png'): + image_path = os.path.join(screenshot_dir, filename) + + # Perform OCR + extracted_text = perform_ocr(image_path) + + # Save the extracted text + output_filename = os.path.splitext(filename)[0] + '.txt' + output_path = os.path.join(output_dir, output_filename) + + with open(output_path, 'w', encoding='utf-8') as f: + f.write(extracted_text) + + print(f"Processed {filename}, saved result to {output_filename}") + +if __name__ == "__main__": + main() diff --git a/screenshot.py b/screenshot.py new file mode 100755 index 0000000..fecb841 --- /dev/null +++ b/screenshot.py @@ -0,0 +1,58 @@ +import time +import numpy as np +from PIL import Image +import mss +import cv2 +import os + + +def take_screenshot(): + with mss.mss() as sct: + monitor = sct.monitors[0] + screenshot = sct.grab(monitor) + img = Image.frombytes("RGB", screenshot.size, screenshot.rgb) + return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) + + +def compare_images(img1, img2): + if img1 is None or img2 is None: + return 100.0 + + # Convert images to grayscale + gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) + gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) + + # Compute the Mean Squared Error between the two images + mse = np.mean((gray1 - gray2) ** 2) + + # Normalize the difference (assuming max pixel value is 255) + diff_percent = (mse / (255.0 ** 2)) * 100 + + return diff_percent + + +def main(): + last_saved_image = None + screenshot_count = 0 + + if not os.path.exists('screenshots'): + os.makedirs('screenshots') + + while True: + current_image = take_screenshot() + diff_percent = compare_images(current_image, last_saved_image) + + if diff_percent > 4.0: + screenshot_count += 1 + filename = f'screenshots/screenshot_{screenshot_count}.png' + cv2.imwrite(filename, current_image) + print(f"Saved {filename} (Difference: {diff_percent:.2f}%)") + last_saved_image = current_image + else: + print(f"Screenshot not saved (Difference: {diff_percent:.2f}%)") + + time.sleep(5) + + +if __name__ == "__main__": + main() diff --git a/updatedscreenshot.py b/updatedscreenshot.py new file mode 100755 index 0000000..57643c1 --- /dev/null +++ b/updatedscreenshot.py @@ -0,0 +1,59 @@ +import time +import numpy as np +from PIL import Image +import mss +import cv2 +import os +from skimage.metrics import structural_similarity as ssim + + +def take_screenshot(): + with mss.mss() as sct: + monitor = sct.monitors[0] + screenshot = sct.grab(monitor) + img = Image.frombytes("RGB", screenshot.size, screenshot.rgb) + return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) + + +def compare_images(img1, img2): + if img1 is None or img2 is None: + return 100.0 + + # Convert images to grayscale + gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) + gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) + + # Compute the Structural Similarity Index (SSIM) between the two images + score, _ = ssim(gray1, gray2, full=True) + + # The SSIM score is a value between -1 and 1, where 1 means identical image + # We'll convert it to a percentage difference + diff_percent = (1 - score) * 100 + + return diff_percent + + +def main(): + last_saved_image = None + screenshot_count = 0 + if not os.path.exists('screenshots'): + os.makedirs('screenshots') + + while True: + current_image = take_screenshot() + diff_percent = compare_images(current_image, last_saved_image) + + if diff_percent > 15.0: # Lowered threshold for increased sensitivity + screenshot_count += 1 + filename = f'screenshots/screenshot_{screenshot_count}.png' + cv2.imwrite(filename, current_image) + print(f"Saved {filename} (Difference: {diff_percent:.2f}%)") + last_saved_image = current_image + else: + print(f"Screenshot not saved (Difference: {diff_percent:.2f}%)") + + time.sleep(5) + + +if __name__ == "__main__": + main()