import io
import os
import re
from google.cloud import vision_v1
from google.cloud.vision_v1 import types
def detect_text(path, target_strings):
client = vision_v1.ImageAnnotatorClient()
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
# Vision API를 사용하여 이미지에서 텍스트 감지
response = client.text_detection(image=image)
# 감지된 문자 리스트로 변환
texts = response.text_annotations
#######################################################################################3
# 이미지 파일에서 사번 찾기
apm = [0, 0] # 사번이 두 개 있으므로 크기를 2로 설정
i = 0
max_y = 0
for text in response.text_annotations:
if '사번' == text.description:
vertices = text.bounding_poly.vertices
bounds = [(vertex.x, vertex.y) for vertex in vertices]
bounds_str = ','.join('({}, {})'.format(x, y) for x, y in bounds)
print('"{}"'.format(text.description), 'bounds: {}'.format(bounds_str))
apm[i] = bounds[0][0]
i += 1
y = bounds[2][1]
if y > max_y:
max_y = y
#######################################################################################3
# 각 텍스트 영역의 경계 상자 좌표 가져오기
route = ""
text_bounds = [r.bounding_poly.vertices for r in response.text_annotations]
date_pattern = re.compile(r'\d{4}/\d{2}/\d{2}')
# 노선 번호 찾기
for i, text in enumerate(response.text_annotations):
# if text.description in ['6613', '6614', '6615', '6716', '6640A', '6640B']:
if text.description in target_strings :
vertices = response.text_annotations[i].bounding_poly.vertices
bound = [(vertex.x, vertex.y) for vertex in vertices]
bounds_str = ','.join('({}, {})'.format(x, y) for x, y in bound)
route = text.description
if ( route == "66408") : route = "6640B"
# print("Found string above max_y and left of 200: {}".format(route))
# search for date pattern in the current text
match = date_pattern.search(text.description)
vertices = text_bounds[i]
y_values = [vertex.y for vertex in vertices]
if match :
if max(y_values) < max_y :
olist_date = match.group(0)
# print(f"Found date '{olist_date}' at bounding polygon {bounds_str}")
print ( "===== ", route, olist_date, "==================>" )
#######################################################################################3
# 각 문자열의 위치와 문자열을 포함한 dictionary 생성
text_dict = {}
for text in texts:
vertices = [(vertex.x, vertex.y) for vertex in text.bounding_poly.vertices]
text_dict[text.description] = vertices
for j in range(2) :
# 대각선으로 연결된 두 점
if j == 0 :
# x1, y1 = 80, 190
# x2, y2 = 170, 1000
x1, y1 = apm[0]-30, 190
x2, y2 = apm[0]+30, 1000
else :
# x1, y1 = 550, 200
# x2, y2 = 600, 1000
x1, y1 = apm[1]-50, 190
x2, y2 = apm[1], 1000
print ( "apm", apm[j])
# bounding box 계산
left = min(x1, x2)
top = min(y1, y2)
right = max(x1, x2)
bottom = max(y1, y2)
# bounding box 내에 있는 문자열 추출
annotations = response.text_annotations
filtered_annotations = []
# bounding box 내의 숫자만 추출하여 filtered_annotations 리스트에 추가
for annotation in annotations:
vertices = annotation.bounding_poly.vertices
x, y = vertices[0].x, vertices[0].y # bounding box 내의 임의의 한 점 선택
if left <= x <= right and top <= y <= bottom and annotation.description.isdigit():
filtered_annotations.append(annotation)
# y축이 같은 annotation 객체들을 찾아서 출력
sorted_annotations = sorted(filtered_annotations, key=lambda a: a.bounding_poly.vertices[0].y)
current_y = sorted_annotations[0].bounding_poly.vertices[0].y
current_line = []
i = 1
for annotation in sorted_annotations:
if annotation.bounding_poly.vertices[0].y == current_y:
current_line.append(annotation)
else:
# 현재 줄의 annotation 객체들을 출력
text = ''.join([a.description for a in current_line])
bounds = [(vertex.x, vertex.y) for vertex in current_line[0].bounding_poly.vertices]
bounds_str = ','.join('({}, {})'.format(x, y) for x, y in bounds)
print(i, text, 'bounds: {}'.format(bounds_str))
current_line = [annotation]
current_y = annotation.bounding_poly.vertices[0].y
i += 1
# 마지막 줄 출력
if current_line:
text = ''.join([a.description for a in current_line])
bounds = [(vertex.x, vertex.y) for vertex in current_line[0].bounding_poly.vertices]
bounds_str = ','.join('({}, {})'.format(x, y) for x, y in bounds)
print(i, text, 'bounds: {}'.format(bounds_str))
i += 1
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'doc/sbus-407c7-62869bfd7e72.json'
# 폴더 경로
path = "/home/pi/ofile/upload/"
# 폴더 내 파일 이름 리스트
files = os.listdir(path)
# 찾을 문자열
target_strings = ['6613', '6614', '6615', '6640A', '6640B', '6616', '6638', '6716', '6713']
# 파일 이름 출력
for file in files:
print ()
print(file)
file_name = os.path.join(path, file)
# 이미지에서 문자 찾아 출력
detect_text(file_name, target_strings)