개정판 3148b090
gcloud api test
Change-Id: I74c3a1391baa02b7b7cc77963da591d8710ce85e
DTI_PID/DTI_PID/RecognitionDialog.py | ||
---|---|---|
1171 | 1171 |
#pool.shutdown(wait=True) |
1172 | 1172 |
# up to here |
1173 | 1173 |
|
1174 |
Worker.drawFoundTextsOnCanvas(mainRes, textInfoList) |
|
1175 |
|
|
1174 | 1176 |
# remove text from image |
1175 | 1177 |
textDetector.remove_text_from_image(app_doc_data.imgSrc, [0, 0]) |
1176 | 1178 |
#textDetector.remove_text_from_image(area.img, offset) |
... | ... | |
1235 | 1237 |
#cv2.imwrite(os.path.join(project.getTempPath(), "RECT_" + os.path.basename(mainRes)), |
1236 | 1238 |
# app_doc_data.imgSrc) for debug |
1237 | 1239 |
|
1238 |
Worker.drawFoundSymbolsOnCanvas(mainRes, searchedSymbolList, textInfoList, listWidget) |
|
1240 |
#Worker.drawFoundSymbolsOnCanvas(mainRes, searchedSymbolList, textInfoList, listWidget)
|
|
1239 | 1241 |
|
1240 | 1242 |
# get difference between original and recognized image |
1241 | 1243 |
foundFilePath = os.path.join(project.getTempPath(), "FOUND_" + os.path.basename(mainRes)) |
... | ... | |
3594 | 3596 |
return "LEFT" |
3595 | 3597 |
else: |
3596 | 3598 |
return "NONE" |
3597 |
|
|
3599 |
|
|
3598 | 3600 |
@staticmethod |
3599 | 3601 |
def drawFoundSymbolsOnCanvas(drawingPath, symbols, textInfos, listWidget): |
3600 | 3602 |
"""draw found symbols and texts to image""" |
... | ... | |
3610 | 3612 |
try: |
3611 | 3613 |
project = app_doc_data.getCurrentProject() |
3612 | 3614 |
|
3613 |
for symbol in symbols: |
|
3614 |
Worker.drawFoundSymbols(symbol, listWidget) |
|
3615 |
#for symbol in symbols: |
|
3616 |
# Worker.drawFoundSymbols(symbol, listWidget) |
|
3617 |
|
|
3618 |
for text in textInfos: |
|
3619 |
left = text.getX() |
|
3620 |
top = text.getY() |
|
3621 |
right = text.getX() + text.getW() |
|
3622 |
bottom = text.getY() + text.getH() |
|
3623 |
|
|
3624 |
#canvas[top:bottom, left:right] = app_doc_data.activeDrawing.image_origin[top:bottom, left:right] |
|
3625 |
|
|
3626 |
#cv2.imwrite(os.path.join(project.getTempPath(), "FOUND_" + os.path.basename(drawingPath)), canvas) # for debug |
|
3627 |
except Exception as ex: |
|
3628 |
from App import App |
|
3629 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, |
|
3630 |
sys.exc_info()[-1].tb_lineno) |
|
3631 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
|
3632 |
|
|
3633 |
@staticmethod |
|
3634 |
def drawFoundTextsOnCanvas(drawingPath, textInfos): |
|
3635 |
"""draw found texts to image""" |
|
3636 |
|
|
3637 |
app_doc_data = AppDocData.instance() |
|
3638 |
_canvas = np.zeros(app_doc_data.imgSrc.shape, np.uint8) |
|
3639 |
_canvas[::] = 255 |
|
3640 |
|
|
3641 |
try: |
|
3642 |
project = app_doc_data.getCurrentProject() |
|
3643 |
|
|
3644 |
x_gap = 90 |
|
3645 |
y_gap = 80 |
|
3646 |
y_position = y_gap |
|
3647 |
x_position = x_gap |
|
3648 |
y_max = 0 |
|
3615 | 3649 |
|
3616 | 3650 |
for text in textInfos: |
3651 |
angle = text.getAngle() |
|
3617 | 3652 |
left = text.getX() |
3618 | 3653 |
top = text.getY() |
3619 | 3654 |
right = text.getX() + text.getW() |
3620 | 3655 |
bottom = text.getY() + text.getH() |
3621 | 3656 |
|
3622 |
canvas[top:bottom, left:right] = app_doc_data.imgSrc[top:bottom, left:right] |
|
3657 |
if x_position + text.getW() + 30 >= app_doc_data.imgSrc.shape[1] or x_position + text.getH() + 30 >= app_doc_data.imgSrc.shape[1]: |
|
3658 |
x_position = x_gap |
|
3659 |
y_position = y_position + y_max + y_gap |
|
3660 |
|
|
3661 |
_textImg = app_doc_data.imgSrc[top:bottom, left:right] |
|
3662 |
textImg = _textImg.copy() |
|
3663 |
|
|
3664 |
if angle == 0: |
|
3665 |
_canvas[y_position:y_position + text.getH(), x_position:x_position + text.getW()] = textImg[:] |
|
3666 |
|
|
3667 |
cv2.rectangle(_canvas, (x_position - 30, y_position - 30), (x_position + text.getW() + 60, y_position + text.getH() + 60), 100, 2) |
|
3668 |
|
|
3669 |
x_position = x_position + text.getW() + x_gap |
|
3670 |
if y_max < text.getH(): |
|
3671 |
y_max = text.getH() |
|
3672 |
else: |
|
3673 |
textImg = cv2.rotate(textImg, cv2.ROTATE_90_CLOCKWISE) |
|
3674 |
_canvas[y_position:y_position + text.getW(), x_position:x_position + text.getH()] = textImg[:] |
|
3675 |
|
|
3676 |
cv2.rectangle(_canvas, (x_position - 30, y_position - 30), (x_position + text.getH() + 60, y_position + text.getW() + 60), 100, 2) |
|
3677 |
|
|
3678 |
x_position = x_position + text.getH() + x_gap |
|
3679 |
if y_max < text.getW(): |
|
3680 |
y_max = text.getW() |
|
3623 | 3681 |
|
3624 |
#cv2.imwrite(os.path.join(project.getTempPath(), "FOUND_" + os.path.basename(drawingPath)), canvas) for debug |
|
3682 |
cv2.imwrite(os.path.join(project.getTempPath(), "FROM_TEXT_" + os.path.basename(drawingPath)), app_doc_data.imgSrc) |
|
3683 |
cv2.imwrite(os.path.join(project.getTempPath(), "FOUND_TEXT_" + os.path.basename(drawingPath)), _canvas) # for debug |
|
3625 | 3684 |
except Exception as ex: |
3626 | 3685 |
from App import App |
3627 | 3686 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, |
DTI_PID/DTI_PID/TextDetector.py | ||
---|---|---|
243 | 243 |
sys.exc_info()[-1].tb_lineno) |
244 | 244 |
print(message) |
245 | 245 |
|
246 |
''' |
|
247 |
# for debug text area save |
|
248 |
for res in res_list: |
|
249 |
cv2.rectangle(ocr_image, (res.getX() - offset_x, res.getY() - offset_y), (res.getX() + res.getW() - offset_x, res.getY() + res.getH() - offset_y), 100, 2) |
|
250 |
print((res.getX() - offset_x, res.getY() - offset_y), (res.getX() + res.getW() - offset_x, res.getY() + res.getH() - offset_y), res.getAngle()) |
|
251 |
path = os.path.join(project.getTempPath(), 'OCR_AREA.png') |
|
252 |
#cv2.imwrite(path, ocr_image) |
|
253 |
''' |
|
254 |
|
|
246 | 255 |
return res_list, ocr_image |
247 | 256 |
|
248 | 257 |
def get_text_image_tile(self, img, size=[1300, 1300], overlap=100): |
minorTools/gcloud_test.py | ||
---|---|---|
1 |
def detect_text(path): |
|
2 |
"""Detects text in the file.""" |
|
3 |
from google.cloud import vision |
|
4 |
import cv2 |
|
5 |
import numpy as np |
|
6 |
|
|
7 |
client = vision.ImageAnnotatorClient() |
|
8 |
|
|
9 |
with open(path, "rb") as image_file: |
|
10 |
content = image_file.read() |
|
11 |
|
|
12 |
_image = cv2.imread(path, 1) |
|
13 |
|
|
14 |
image = vision.Image(content=content) |
|
15 |
|
|
16 |
response = client.text_detection(image=image) |
|
17 |
#response = client.document_text_detection(image=image) |
|
18 |
texts = response.text_annotations |
|
19 |
#print("Texts:") |
|
20 |
|
|
21 |
for text in texts[1:]: |
|
22 |
#print(f'\n"{text.description}"') |
|
23 |
|
|
24 |
#vertices = [ f"({vertex.x},{vertex.y})" for vertex in text.bounding_poly.vertices ] |
|
25 |
#print("bounds: {}".format(",".join(vertices))) |
|
26 |
|
|
27 |
_vertices = [(vertex.x,vertex.y) for vertex in text.bounding_poly.vertices ] |
|
28 |
|
|
29 |
cv2.rectangle(_image, _vertices[0], _vertices[2], (100, 100, 100), 2) |
|
30 |
cv2.putText(_image, text.description, (_vertices[0][0], (_vertices[0][1] - 10)), cv2.FONT_HERSHEY_PLAIN, 1, (100, 100, 100), 1) |
|
31 |
|
|
32 |
cv2.imwrite(path.replace('test', 'test_res'), _image) |
|
33 |
|
|
34 |
if response.error.message: |
|
35 |
raise Exception( |
|
36 |
"{}\nFor more info on error messages, check: " |
|
37 |
"https://cloud.google.com/apis/design/errors".format(response.error.message) |
|
38 |
) |
|
39 |
|
|
40 |
|
|
41 |
if __name__ == "__main__": |
|
42 |
import os |
|
43 |
path = 'E:\Projects\DTIPID\minorTools\gcloud' |
|
44 |
path = os.path.join(path, 'test.png') |
|
45 |
detect_text(path) |
|
46 |
|
내보내기 Unified diff