forked from VedankPurohit/LiveRecall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaptureStart.py
141 lines (117 loc) · 4.61 KB
/
CaptureStart.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from PIL import ImageGrab
import numpy as np
import cv2
from skimage.metrics import structural_similarity as Ssim
from Components.Memory import AddToMemory, GetMemory, JsonData, ClipMode, RetriveMemoryMax
from Components.Crypt import EncryptDecryptImage
import time
import os
import threading
global Threshold
Threshold = 0.85 # prev 95 Bigger val easy to start save
global ThresholdSave
ThresholdSave = 0.75 # smaller value easy to actualy save - prev 85
global ScreenshotInterval
ScreenshotInterval = 2
SaveDirectory = 'CapturedData'
# Ensure the save directory exists
if not os.path.exists(SaveDirectory):
os.makedirs(SaveDirectory)
def GrabScreen():
"""Captures the current screen."""
return ImageGrab.grab()
def ImageToArray(image):
"""Converts an image to a numpy array."""
return np.array(image)
def CalculateSsim(img1, img2):
"""Calculates the Structural Similarity Index (SSIM) between two images."""
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
score, _ = Ssim(gray1, gray2, full=True)
return score
def GetNextFilename(extension='.png'):
existing_files = os.listdir(SaveDirectory)
screenshot_numbers = [int(f.split('_')[1].split('.')[0]) for f in existing_files if f.startswith('screenshot_') and f.split('_')[1].split('.')[0].isdigit()]
if not screenshot_numbers:
return f'screenshot_1{extension}'
next_number = max(screenshot_numbers) + 1
return f'screenshot_{next_number}{extension}'
def SaveScreenshot(image,SaveDirectory = 'CapturedData'):
filename = GetNextFilename()
filepath = os.path.join(SaveDirectory, filename)
image.save(filepath)
return filename
def SaveScreenshotJpg(image, SaveDirectory='CapturedData', quality=90):
if not os.path.exists(SaveDirectory):
os.makedirs(SaveDirectory)
filename = GetNextFilename('.jpg')
filepath = os.path.join(SaveDirectory, filename)
if image.mode == 'RGBA':
image = image.convert('RGB')
image.save(filepath, 'JPEG', quality=quality)
return filename
def Main(previous_screenshot, key):
#global previous_screenshot
global last_capture_time
time.sleep(ScreenshotInterval)
current_screenshot = GrabScreen()
current_array = ImageToArray(current_screenshot)
similarity_score = CalculateSsim(previous_screenshot, current_array)
print("Waiting for change")
if similarity_score < Threshold:
print("Detected a change -")
previous_screenshot = current_array
stable_change_period = 3
while stable_change_period:
stable_change_period -= 1
time.sleep(ScreenshotInterval * 0.3)
current_screenshot = GrabScreen()
current_array = ImageToArray(current_screenshot)
similarity_score = CalculateSsim(previous_screenshot, current_array)
if similarity_score <= ThresholdSave:
print("Screen Changed")
break
print("Screen Didn't change")
else:
print("Stable for long")
saved_filename = SaveScreenshotJpg(current_screenshot)
print(f'Screenshot saved: {saved_filename}')
AddToMemory(os.path.join(SaveDirectory, saved_filename))
print("Added To memory")
previous_screenshot = current_array
previous_screenshot = current_array
time.sleep(ScreenshotInterval/2)
EncryptDecryptImage(os.path.join(SaveDirectory, saved_filename), key)
return previous_screenshot
global Start
Start = False
global Key
Key = ""
def ThreadMain():
try:
previous_screenshot = ImageToArray(GrabScreen())
last_capture_time = time.time()
count = 0
print("Started")
while True:
while Start:
count += 1
previous_screenshot = Main(previous_screenshot, key=Key)
if count > 10:
count = 1
Memory , Names,TimeLine = GetMemory()
JsonData.SaveJson("Data.json",Names,Memory, TimeLine)
print("Saved")
if Start == False and count > 0:
count = 0
Memory , Names, TimeLine = GetMemory()
JsonData.SaveJson("Data.json",Names,Memory, TimeLine)
print("Saved")
except Exception as e:
print(e)
time.sleep(5)
ThreadMain()
Threaded = threading.Thread(target=ThreadMain)
#Threaded.start()
if __name__ == "__main__":
ThreadMain()