forked from ZennoLab/capmonstercloud-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage2Text.py
More file actions
41 lines (31 loc) · 1.48 KB
/
Copy pathimage2Text.py
File metadata and controls
41 lines (31 loc) · 1.48 KB
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
import os
import time
import asyncio
from capmonstercloudclient import ClientOptions, CapMonsterClient
from capmonstercloudclient.requests import ImageToTextRequest
async def solve_captcha_sync(num_requests):
return [await cap_monster_client.solve_captcha(image_request) for _ in range(num_requests)]
async def solve_captcha_async(num_requests):
tasks = [asyncio.create_task(cap_monster_client.solve_captcha(image_request))
for _ in range(num_requests)]
return await asyncio.gather(*tasks, return_exceptions=True)
if __name__ == '__main__':
key = os.getenv('API_KEY')
client_options = ClientOptions(api_key=key)
cap_monster_client = CapMonsterClient(options=client_options)
image_path = './images/sample.jpg'
with open(image_path, 'rb') as f:
image_bytes = f.read()
image_request = ImageToTextRequest(image_bytes=image_bytes, threshold=50,
module_name='amazon', case=True, numeric=1, math=False)
nums = 3
# Sync test
sync_start = time.time()
sync_responses = asyncio.run(solve_captcha_sync(nums))
print(f'average execution time sync {1/((time.time()-sync_start)/nums):0.2f} ' \
f'resp/sec\nsolution: {sync_responses[0]}')
# Async test
async_start = time.time()
async_responses = asyncio.run(solve_captcha_async(nums))
print(f'average execution time async {1/((time.time()-async_start)/nums):0.2f} ' \
f'resp/sec\nsolution: {async_responses[0]}')