Spaces:
Sleeping
Sleeping
amaye15
commited on
Commit
·
1cdd472
1
Parent(s):
ef98c8b
Max limit
Browse files- run_google_lens.py +40 -3
run_google_lens.py
CHANGED
@@ -31,6 +31,7 @@ class GoogleLensPipeline:
|
|
31 |
accept_button_xpath=None,
|
32 |
file_input_xpath=None,
|
33 |
root_directory=None,
|
|
|
34 |
):
|
35 |
self.link = link or "https://lens.google.com/"
|
36 |
self.accept_button_xpath = accept_button_xpath or '//span[text()="Accept all"]'
|
@@ -41,6 +42,7 @@ class GoogleLensPipeline:
|
|
41 |
self.tmp_directory_two = os.path.join(self.root_directory, "tmp_two")
|
42 |
self.final_directory = os.path.join(self.root_directory, "images")
|
43 |
|
|
|
44 |
self.setup_directories()
|
45 |
|
46 |
def start_driver(self):
|
@@ -87,7 +89,7 @@ class GoogleLensPipeline:
|
|
87 |
except Exception as e:
|
88 |
logger.error(f"Failed to open page: {str(e)}")
|
89 |
|
90 |
-
def accept_terms(self, timeout=
|
91 |
try:
|
92 |
wait = WebDriverWait(self.driver, timeout)
|
93 |
accept_button = wait.until(
|
@@ -98,7 +100,7 @@ class GoogleLensPipeline:
|
|
98 |
except Exception as e:
|
99 |
logger.error(f"Failed to accept terms: {str(e)}")
|
100 |
|
101 |
-
def upload_image(self, image_path, timeout=
|
102 |
try:
|
103 |
wait = WebDriverWait(self.driver, timeout)
|
104 |
file_input = wait.until(
|
@@ -164,6 +166,14 @@ class GoogleLensPipeline:
|
|
164 |
try:
|
165 |
await self.run_pipeline(image_path, save_path=self.tmp_directory_one)
|
166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
for val in os.listdir(self.tmp_directory_one):
|
168 |
try:
|
169 |
await self.run_pipeline(
|
@@ -174,6 +184,15 @@ class GoogleLensPipeline:
|
|
174 |
os.path.join(self.tmp_directory_one, val),
|
175 |
os.path.join(self.final_directory, val),
|
176 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
except Exception as e:
|
178 |
logger.error(
|
179 |
f"Error processing file {val} in tmp_directory_one: {str(e)}"
|
@@ -189,6 +208,15 @@ class GoogleLensPipeline:
|
|
189 |
os.path.join(self.tmp_directory_two, val),
|
190 |
os.path.join(self.final_directory, val),
|
191 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
except Exception as e:
|
193 |
logger.error(
|
194 |
f"Error processing file {val} in tmp_directory_two: {str(e)}"
|
@@ -220,6 +248,15 @@ if __name__ == "__main__":
|
|
220 |
|
221 |
path = "/home/user/app/images/"
|
222 |
|
|
|
|
|
|
|
|
|
223 |
for image_path in get_image_paths(path):
|
224 |
-
|
|
|
|
|
|
|
|
|
|
|
225 |
asyncio.run(pipeline.loop_run_pipeline(image_path))
|
|
|
31 |
accept_button_xpath=None,
|
32 |
file_input_xpath=None,
|
33 |
root_directory=None,
|
34 |
+
max_images=100000, # Added max_images parameter
|
35 |
):
|
36 |
self.link = link or "https://lens.google.com/"
|
37 |
self.accept_button_xpath = accept_button_xpath or '//span[text()="Accept all"]'
|
|
|
42 |
self.tmp_directory_two = os.path.join(self.root_directory, "tmp_two")
|
43 |
self.final_directory = os.path.join(self.root_directory, "images")
|
44 |
|
45 |
+
self.max_images = max_images # Initialize max_images
|
46 |
self.setup_directories()
|
47 |
|
48 |
def start_driver(self):
|
|
|
89 |
except Exception as e:
|
90 |
logger.error(f"Failed to open page: {str(e)}")
|
91 |
|
92 |
+
def accept_terms(self, timeout=20):
|
93 |
try:
|
94 |
wait = WebDriverWait(self.driver, timeout)
|
95 |
accept_button = wait.until(
|
|
|
100 |
except Exception as e:
|
101 |
logger.error(f"Failed to accept terms: {str(e)}")
|
102 |
|
103 |
+
def upload_image(self, image_path, timeout=20, sleep=10):
|
104 |
try:
|
105 |
wait = WebDriverWait(self.driver, timeout)
|
106 |
file_input = wait.until(
|
|
|
166 |
try:
|
167 |
await self.run_pipeline(image_path, save_path=self.tmp_directory_one)
|
168 |
|
169 |
+
# Check if the maximum number of images has been reached
|
170 |
+
total_images = len(os.listdir(self.final_directory))
|
171 |
+
if total_images >= self.max_images:
|
172 |
+
logger.info(
|
173 |
+
f"Reached maximum number of images ({self.max_images}). Stopping."
|
174 |
+
)
|
175 |
+
return
|
176 |
+
|
177 |
for val in os.listdir(self.tmp_directory_one):
|
178 |
try:
|
179 |
await self.run_pipeline(
|
|
|
184 |
os.path.join(self.tmp_directory_one, val),
|
185 |
os.path.join(self.final_directory, val),
|
186 |
)
|
187 |
+
|
188 |
+
# Check after each image
|
189 |
+
total_images = len(os.listdir(self.final_directory))
|
190 |
+
if total_images >= self.max_images:
|
191 |
+
logger.info(
|
192 |
+
f"Reached maximum number of images ({self.max_images}). Stopping."
|
193 |
+
)
|
194 |
+
return
|
195 |
+
|
196 |
except Exception as e:
|
197 |
logger.error(
|
198 |
f"Error processing file {val} in tmp_directory_one: {str(e)}"
|
|
|
208 |
os.path.join(self.tmp_directory_two, val),
|
209 |
os.path.join(self.final_directory, val),
|
210 |
)
|
211 |
+
|
212 |
+
# Check after each image
|
213 |
+
total_images = len(os.listdir(self.final_directory))
|
214 |
+
if total_images >= self.max_images:
|
215 |
+
logger.info(
|
216 |
+
f"Reached maximum number of images ({self.max_images}). Stopping."
|
217 |
+
)
|
218 |
+
return
|
219 |
+
|
220 |
except Exception as e:
|
221 |
logger.error(
|
222 |
f"Error processing file {val} in tmp_directory_two: {str(e)}"
|
|
|
248 |
|
249 |
path = "/home/user/app/images/"
|
250 |
|
251 |
+
pipeline = GoogleLensPipeline(
|
252 |
+
root_directory=path, max_images=100000
|
253 |
+
) # Set max_images
|
254 |
+
|
255 |
for image_path in get_image_paths(path):
|
256 |
+
total_images = len(os.listdir(pipeline.final_directory))
|
257 |
+
if total_images >= pipeline.max_images:
|
258 |
+
logger.info(
|
259 |
+
f"Reached maximum number of images ({pipeline.max_images}). Stopping."
|
260 |
+
)
|
261 |
+
break
|
262 |
asyncio.run(pipeline.loop_run_pipeline(image_path))
|