Skip to content

Commit

Permalink
更新Readme文档
Browse files Browse the repository at this point in the history
  • Loading branch information
sml2h3 committed Jan 25, 2022
1 parent fc6ebc1 commit 0e4ac18
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
26 changes: 17 additions & 9 deletions ddddocr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1641,27 +1641,35 @@ def get_target(self, img_bytes: bytes = None):
starttx = x
if end_y != 0:
end_x = x
return image.crop([starttx, startty, end_x, end_y]), startty
return image.crop([starttx, startty, end_x, end_y]), starttx, startty

def slide_match(self, target_bytes: bytes = None, background_bytes: bytes = None, simple_target: bool=False):
if not simple_target:
target, target_y = self.get_target(target_bytes)
target = cv2.cvtColor(np.asarray(target), cv2.COLOR_RGBA2GRAY)
target, target_x, target_y = self.get_target(target_bytes)
target = cv2.cvtColor(np.asarray(target), cv2.IMREAD_ANYCOLOR)
else:
target = cv2.imdecode(np.frombuffer(target_bytes, np.uint8), cv2.IMREAD_GRAYSCALE)
target = cv2.imdecode(np.frombuffer(target_bytes, np.uint8), cv2.IMREAD_ANYCOLOR)
target_y = 0
target_x = 0

background = cv2.imdecode(np.frombuffer(background_bytes, np.uint8), cv2.IMREAD_GRAYSCALE)
res = cv2.matchTemplate(background, target, cv2.TM_CCOEFF)
background = cv2.imdecode(np.frombuffer(background_bytes, np.uint8), cv2.IMREAD_ANYCOLOR)

background = cv2.Canny(background, 100, 200)
target = cv2.Canny(target, 100, 200)

background = cv2.cvtColor(background, cv2.COLOR_GRAY2RGB)
target = cv2.cvtColor(target, cv2.COLOR_GRAY2RGB)

res = cv2.matchTemplate(background, target, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
w, h = target.shape[::-1]
h, w = target.shape[:2]
bottom_right = (max_loc[0] + w, max_loc[1] + h)
return {"target_y": target_y,
"target": [int(max_loc[0]), int(max_loc[1]), int(bottom_right[0]), int(bottom_right[1])]}

def slide_comparison(self, target_bytes: bytes = None, background_bytes: bytes = None):
target = Image.open(io.BytesIO(target_bytes))
background = Image.open(io.BytesIO(background_bytes))
target = Image.open(io.BytesIO(target_bytes)).convert("RGB")
background = Image.open(io.BytesIO(background_bytes)).convert("RGB")
image = ImageChops.difference(background, target)
background.close()
target.close()
Expand Down
11 changes: 6 additions & 5 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@
# 滑块模板匹配方式
import cv2

im = cv2.imread("b.jpg")
im = cv2.imread("b.png")
det = ddddocr.DdddOcr(det=False, ocr=False)

with open('a.jpg', 'rb') as f:
with open('t.png', 'rb') as f:
target_bytes = f.read()

with open('b.jpg', 'rb') as f:
with open('b.png', 'rb') as f:
background_bytes = f.read()

res = det.slide_match(target_bytes, background_bytes, simple_target=True)
res = det.slide_match(target_bytes, background_bytes)
print(res)
x = res["target"]
print(x)
im = cv2.line(im, (int(x[0]), 0), (int(x[0]), 300), color=(0, 0, 255), thickness=2)
# im = cv2.line(im, (int(x[0]), 0), (int(x[0]), 200), color=(0, 0, 255), thickness=2)
im = cv2.rectangle(im, (int(x[0]), int(x[1])), (int(x[2]), int(x[3])), color=(0, 0, 255), thickness=2)
cv2.imwrite("res.jpg", im)

# det = ddddocr.DdddOcr(det=False, ocr=False)
Expand Down

0 comments on commit 0e4ac18

Please sign in to comment.