added code

This commit is contained in:
locker98 2024-09-18 16:53:59 -04:00
parent 3c197c7fd1
commit 5b395717dc
3 changed files with 163 additions and 0 deletions

46
ocr.py Executable file
View File

@ -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()

58
screenshot.py Executable file
View File

@ -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()

59
updatedscreenshot.py Executable file
View File

@ -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()