-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jinhailiang
committed
Mar 2, 2022
0 parents
commit 87aeede
Showing
47 changed files
with
8,512 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
*.pyc | ||
.DS_Store | ||
.idea/ | ||
capture/local_*/ | ||
*.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM python:3.6.5 | ||
COPY ./api /vision/api/ | ||
COPY ./service /vision/service/ | ||
COPY ./dbnet_crnn /vision/dbnet_crnn | ||
COPY ./requirements.txt /vision/requirements.txt | ||
COPY ./server.py ./vision/server.py | ||
ARG PIP_MIRROR=https://mirrors.aliyun.com/pypi/simple/ | ||
WORKDIR /vision | ||
RUN mkdir capture\ | ||
&& pip install --upgrade pip -i ${PIP_MIRROR}\ | ||
&& pip install -r requirements.txt -i ${PIP_MIRROR} | ||
CMD ["python3", "server.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 美团点评 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Vision UI | ||
|
||
![GitHub](https://img.shields.io/badge/Python-3.6-blue) | ||
![GitHub](https://img.shields.io/github/license/Meituan-Dianping/vision-diff) | ||
![GitHub](https://img.shields.io/docker/cloud/build/brighthai/vision-ui) | ||
|
||
## 什么是Vision UI | ||
|
||
Vision UI是一组图像处理算法,来源于美团视觉测试工具,提供如视觉对比(增量式对比)、图像融合和文本识别。 | ||
|
||
本项目无需训练模型,基于训练模型的项目在[Vision-ml](https://github.com/Meituan-Dianping/vision) | ||
|
||
## 特性 | ||
|
||
* 超越像素对比-[视觉对比](resources/vision_diff_cn.md) | ||
|
||
* 基于模板匹配-[图像融合](resources/vision_merge.md) | ||
|
||
* 集成模型-[文本识别](resources/vision_text.md) | ||
|
||
|
||
## 效果展示 | ||
### 图像融合 | ||
| 1.png | 2.png | 3.png | merge | | ||
| ------------------------------ | -------------------------------- | -------------------------------- | ------------------------------------- | | ||
| ![](image/1_0.png) | ![](image/1_1.png) | ![](image/1_2.png) | ![](image/1_merge.png) | ||
|
||
### 视觉对比 | ||
|
||
| base | comparison | diff | | ||
| ------------------------------ | -------------------------------- | ------------------------------------- | | ||
| ![](image/base_1.png) | ![](image/comp_1.png) | ![](image/diff_1.png) | | ||
|
||
|
||
|
||
|
||
## License | ||
|
||
This project is licensed under the [MIT](./LICENSE) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from flask import jsonify | ||
from flask import request | ||
from flask import Blueprint | ||
from flask import make_response | ||
from service.image_diff import ImageDiff | ||
from service.image_merge import Stitcher | ||
from service.image_similar import HashSimilar | ||
from service.image_text import get_image_text | ||
from service.image_utils import get_pop_v | ||
|
||
vision = Blueprint('vision', __name__, url_prefix='/vision') | ||
|
||
|
||
@vision.route('/diff', methods=["POST"]) | ||
def vision_diff(): | ||
data = { | ||
"code": 0, | ||
"data": ImageDiff().get_image_score(request.json['image1'], request.json['image2'], | ||
request.json['image_diff_name']) | ||
} | ||
return jsonify(data) | ||
|
||
|
||
@vision.route('/merge', methods=["POST"]) | ||
def vision_merge(): | ||
data = { | ||
"code": 0, | ||
"data": Stitcher(request.json['image_list']).image_merge( | ||
request.json['name'], | ||
without_padding=request.json.get('without_padding') | ||
) | ||
} | ||
return jsonify(data) | ||
|
||
|
||
@vision.route('/similar', methods=["POST"]) | ||
def vision_similar(): | ||
data = { | ||
"code": 0, | ||
"data": HashSimilar().get_hash_similar(request.json['image1'], request.json['image2']) | ||
} | ||
return jsonify(data) | ||
|
||
|
||
@vision.route('/pop', methods=["POST"]) | ||
def vision_pop(): | ||
data = { | ||
"code": 0, | ||
"data": get_pop_v(request.json['image']) | ||
} | ||
return jsonify(data) | ||
|
||
|
||
@vision.route('/text', methods=["POST"]) | ||
def vision_text(): | ||
data = { | ||
"code": 0, | ||
"data": get_image_text(request.json['image']) | ||
} | ||
resp = make_response(jsonify(data)) | ||
resp.headers["Content-Type"] = "application/json;charset=utf-8" | ||
return resp |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import cv2 | ||
import copy | ||
import numpy as np | ||
import dbnet_crnn.tools.utility as utility | ||
from service.image_utils import get_center_pos | ||
import dbnet_crnn.tools.predict_det as predict_det | ||
import dbnet_crnn.tools.predict_rec as predict_rec | ||
|
||
|
||
def sorted_boxes(dt_boxes): | ||
""" | ||
Sort text boxes in order from top to bottom, left to right | ||
args: | ||
dt_boxes(array):detected text boxes with shape [4, 2] | ||
return: | ||
sorted boxes(array) with shape [4, 2] | ||
""" | ||
num_boxes = dt_boxes.shape[0] | ||
sorted_boxes = sorted(dt_boxes, key=lambda x: (x[0][1], x[0][0])) | ||
_boxes = list(sorted_boxes) | ||
|
||
for i in range(num_boxes - 1): | ||
if abs(_boxes[i+1][0][1] - _boxes[i][0][1]) < 10 and (_boxes[i + 1][0][0] < _boxes[i][0][0]): | ||
tmp = _boxes[i] | ||
_boxes[i] = _boxes[i + 1] | ||
_boxes[i + 1] = tmp | ||
return _boxes | ||
|
||
|
||
class ImageText(object): | ||
def __init__(self): | ||
args = utility.parse_args() | ||
self.text_detector = predict_det.TextDetector(args, model_path='dbnet_crnn/modelv1.1/det/') | ||
self.text_recognizer = predict_rec.TextRecognizer(args, model_path='dbnet_crnn/modelv1.1/rec/') | ||
|
||
def get_rotate_crop_image(self, img, points): | ||
''' | ||
img_height, img_width = img.shape[0:2] | ||
left = int(np.min(points[:, 0])) | ||
right = int(np.max(points[:, 0])) | ||
top = int(np.min(points[:, 1])) | ||
bottom = int(np.max(points[:, 1])) | ||
img_crop = img[top:bottom, left:right, :].copy() | ||
points[:, 0] = points[:, 0] - left | ||
points[:, 1] = points[:, 1] - top | ||
''' | ||
img_crop_width = int( | ||
max( | ||
np.linalg.norm(points[0] - points[1]), | ||
np.linalg.norm(points[2] - points[3]))) | ||
img_crop_height = int( | ||
max( | ||
np.linalg.norm(points[0] - points[3]), | ||
np.linalg.norm(points[1] - points[2]))) | ||
pts_std = np.float32([[0, 0], [img_crop_width, 0], | ||
[img_crop_width, img_crop_height], | ||
[0, img_crop_height]]) | ||
M = cv2.getPerspectiveTransform(points, pts_std) | ||
dst_img = cv2.warpPerspective(img, M, (img_crop_width, img_crop_height), | ||
borderMode=cv2.BORDER_REPLICATE, | ||
flags=cv2.INTER_CUBIC) | ||
dst_img_height, dst_img_width = dst_img.shape[0:2] | ||
if dst_img_height * 1.0 / dst_img_width >= 1.5: | ||
dst_img = np.rot90(dst_img) | ||
return dst_img | ||
|
||
def get_ocr(self, img, max_side_len): | ||
ori_im = img.copy() | ||
dt_boxes = self.text_detector(img, max_side_len) | ||
if dt_boxes is None: | ||
return None, None | ||
img_crop_list = [] | ||
dt_boxes = sorted_boxes(dt_boxes) | ||
for bno in range(len(dt_boxes)): | ||
tmp_box = copy.deepcopy(dt_boxes[bno]) | ||
img_crop = self.get_rotate_crop_image(ori_im, tmp_box) | ||
img_crop_list.append(img_crop) | ||
rec_res = self.text_recognizer(img_crop_list) | ||
return dt_boxes, rec_res | ||
|
||
def get_text(self, img, max_side_len, score_thresh=0.6): | ||
result = [] | ||
dt_boxes, rec_res = self.get_ocr(img, max_side_len) | ||
for roi_ocr in list(zip(dt_boxes, rec_res)): | ||
roi_score = roi_ocr[1][1] | ||
if roi_score > score_thresh: | ||
result.append({ | ||
'pos': get_center_pos(roi_ocr[0]), | ||
'text': roi_ocr[1][0], | ||
'score': round(float(roi_score), 2) | ||
}) | ||
return result | ||
|
||
|
||
image_text = ImageText() |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import numpy as np | ||
import cv2 | ||
from shapely.geometry import Polygon | ||
import pyclipper | ||
|
||
|
||
class DBPostProcess(object): | ||
""" | ||
The post process for Differentiable Binarization (DB). | ||
""" | ||
|
||
def __init__(self, params): | ||
self.thresh = params['thresh'] | ||
self.box_thresh = params['box_thresh'] | ||
self.max_candidates = params['max_candidates'] | ||
self.unclip_ratio = params['unclip_ratio'] | ||
self.min_size = 3 | ||
|
||
def boxes_from_bitmap(self, pred, _bitmap, dest_width, dest_height): | ||
''' | ||
_bitmap: single map with shape (1, H, W), | ||
whose values are binarized as {0, 1} | ||
''' | ||
|
||
bitmap = _bitmap | ||
height, width = bitmap.shape | ||
|
||
outs = cv2.findContours((bitmap * 255).astype(np.uint8), cv2.RETR_LIST, | ||
cv2.CHAIN_APPROX_SIMPLE) | ||
if len(outs) == 3: | ||
img, contours, _ = outs[0], outs[1], outs[2] | ||
elif len(outs) == 2: | ||
contours, _ = outs[0], outs[1] | ||
|
||
num_contours = min(len(contours), self.max_candidates) | ||
boxes = np.zeros((num_contours, 4, 2), dtype=np.int16) | ||
scores = np.zeros((num_contours, ), dtype=np.float32) | ||
|
||
for index in range(num_contours): | ||
contour = contours[index] | ||
points, sside = self.get_mini_boxes(contour) | ||
if sside < self.min_size: | ||
continue | ||
points = np.array(points) | ||
score = self.box_score_fast(pred, points.reshape(-1, 2)) | ||
if self.box_thresh > score: | ||
continue | ||
|
||
box = self.unclip(points).reshape(-1, 1, 2) | ||
box, sside = self.get_mini_boxes(box) | ||
if sside < self.min_size + 2: | ||
continue | ||
box = np.array(box) | ||
if not isinstance(dest_width, int): | ||
dest_width = dest_width.item() | ||
dest_height = dest_height.item() | ||
|
||
box[:, 0] = np.clip( | ||
np.round(box[:, 0] / width * dest_width), 0, dest_width) | ||
box[:, 1] = np.clip( | ||
np.round(box[:, 1] / height * dest_height), 0, dest_height) | ||
boxes[index, :, :] = box.astype(np.int16) | ||
scores[index] = score | ||
return boxes, scores | ||
|
||
def unclip(self, box): | ||
unclip_ratio = self.unclip_ratio | ||
poly = Polygon(box) | ||
distance = poly.area * unclip_ratio / poly.length | ||
offset = pyclipper.PyclipperOffset() | ||
offset.AddPath(box, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON) | ||
expanded = np.array(offset.Execute(distance)) | ||
return expanded | ||
|
||
def get_mini_boxes(self, contour): | ||
bounding_box = cv2.minAreaRect(contour) | ||
points = sorted(list(cv2.boxPoints(bounding_box)), key=lambda x: x[0]) | ||
if points[1][1] > points[0][1]: | ||
index_1 = 0 | ||
index_4 = 1 | ||
else: | ||
index_1 = 1 | ||
index_4 = 0 | ||
if points[3][1] > points[2][1]: | ||
index_2 = 2 | ||
index_3 = 3 | ||
else: | ||
index_2 = 3 | ||
index_3 = 2 | ||
|
||
box = [ | ||
points[index_1], points[index_2], points[index_3], points[index_4] | ||
] | ||
return box, min(bounding_box[1]) | ||
|
||
def box_score_fast(self, bitmap, _box): | ||
h, w = bitmap.shape[:2] | ||
box = _box.copy() | ||
xmin = np.clip(np.floor(box[:, 0].min()).astype(np.int), 0, w - 1) | ||
xmax = np.clip(np.ceil(box[:, 0].max()).astype(np.int), 0, w - 1) | ||
ymin = np.clip(np.floor(box[:, 1].min()).astype(np.int), 0, h - 1) | ||
ymax = np.clip(np.ceil(box[:, 1].max()).astype(np.int), 0, h - 1) | ||
|
||
mask = np.zeros((ymax - ymin + 1, xmax - xmin + 1), dtype=np.uint8) | ||
box[:, 0] = box[:, 0] - xmin | ||
box[:, 1] = box[:, 1] - ymin | ||
cv2.fillPoly(mask, box.reshape(1, -1, 2).astype(np.int32), 1) | ||
return cv2.mean(bitmap[ymin:ymax + 1, xmin:xmax + 1], mask)[0] | ||
|
||
def __call__(self, outs_dict, ratio_list): | ||
pred = outs_dict['maps'] | ||
|
||
pred = pred[:, 0, :, :] | ||
segmentation = pred > self.thresh | ||
|
||
boxes_batch = [] | ||
for batch_index in range(pred.shape[0]): | ||
height, width = pred.shape[-2:] | ||
tmp_boxes, tmp_scores = self.boxes_from_bitmap(pred[batch_index], | ||
segmentation[batch_index], | ||
width, height) | ||
boxes = [] | ||
for k in range(len(tmp_boxes)): | ||
if tmp_scores[k] > self.box_thresh: | ||
boxes.append(tmp_boxes[k]) | ||
if len(boxes) > 0: | ||
boxes = np.array(boxes) | ||
|
||
ratio_h, ratio_w = ratio_list[batch_index] | ||
boxes[:, :, 0] = boxes[:, :, 0] / ratio_w | ||
boxes[:, :, 1] = boxes[:, :, 1] / ratio_h | ||
|
||
boxes_batch.append(boxes) | ||
return boxes_batch |
Oops, something went wrong.