-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandleGaps.py
58 lines (51 loc) · 1.65 KB
/
handleGaps.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
from __future__ import print_function
from time import time
import cv2 as cv
import numpy as np
import requests
from gaps.genetic_algorithm import GeneticAlgorithm
class Gaps:
def __init__(self, imgUrl):
self.url = imgUrl
self.generations = 20
self.population = 600
self.piece_size = 80
def getRes(self):
resp = requests.get(self.url)
image = np.asarray(bytearray(resp.content), dtype="uint8")
image = cv.imdecode(image, cv.IMREAD_COLOR)
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
print(f"[*] Start Do Gaps {self.url}")
start = time()
algorithm = GeneticAlgorithm(image, self.piece_size, 200, 20)
solution = algorithm.start_evolution(False)
end = time()
print("[*] Done in {0:.3f} s".format(end - start))
solution.to_image()
print('[*] PieceMapping = ', solution.getPieceMapping())
return solution.getPieceMapping()
def getLocation(self):
resMap, total, first = self.getRes(), 0, []
for k, v in resMap.items():
if k == v:
total += 1
continue
if not first:
first = [k, v]
continue
if total == 6 and first:
return first
else:
return []
def run(self):
res = self.getLocation()
if not res:
return res
if res[0] > res[1]:
return [res[1], res[0]]
return res
if __name__ == "__main__":
# 用于测试
url = "https://necaptcha.nosdn.127.net/5c0fac1ddd044630b5daaae2aac85bbc.jpg"
final = Gaps(url).run()
print(final)