Skip to content

Commit

Permalink
fix: get filepath
Browse files Browse the repository at this point in the history
  • Loading branch information
garden-jun committed Feb 24, 2024
1 parent c33805c commit dd0cc06
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 26 deletions.
73 changes: 48 additions & 25 deletions OCR/googleOCR.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def detect_text_uri(uri):



def detect_text_dir(file_dir):
def detect_text_dir(file_dir, x, y):
from google.cloud import vision # 구글 클라우드 비전 API를 사용하기 위한 라이브러리 import


Expand Down Expand Up @@ -371,36 +371,59 @@ def detect_text_dir(file_dir):
"""

print('Texts:')
for text in texts:
print(text.description) # 텍스트 출력
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in text.bounding_poly.vertices])
print('bounds: {}'.format(','.join(vertices))) # 바운딩 박스 좌표 출력


# TODO: 이부분이 내가 그리는게 맞는지 확인 필요
# 바운딩 박스 그리기
draw.polygon([
(text.bounding_poly.vertices)[0].x, (text.bounding_poly.vertices)[0].y,
(text.bounding_poly.vertices)[1].x, (text.bounding_poly.vertices)[1].y,
(text.bounding_poly.vertices)[2].x, (text.bounding_poly.vertices)[2].y,
(text.bounding_poly.vertices)[3].x, (text.bounding_poly.vertices)[3].y],
None,
outline='green',
width=3)
# 손가락 x, y 좌표에 해당하는 단어 배열
words = []

# 바운딩 박스가 그려진 이미지 저장
img.save('./images/image' + '_bounding_box.jpg') # TODO: 파일 경로 수정 필요
for i, text in enumerate(texts):
# 전체 텍스트는 제외, 텍스트 각각의 조각들만 필요하기 때문!
if i == 0 :
continue

# 바운딩 박스가 그려진 이미지 보여주기
"""
img.show()
"""
word = text.description
print(word)

x_set = set()
y_set = set()

# 총 문자 개수 출력
print("Total Texts: ", len(texts))
for vertex in text.bounding_poly.vertices :
x_set.add(vertex.x)
y_set.add(vertex.y)


min_x = min(x_set)
max_x = max(x_set)

min_y = min(y_set)
max_y = max(y_set)

mid_x = min_x + (max_x - min_x) // 2
mid_y = min_y + (max_y - min_y) // 2

print('min_x : ', min_x)
print('min_y : ', max_x)

print('max_x : ', min_y)
print('max_y : ', max_y)

print('mid_x : ', mid_x)
print('mid_y : ', mid_y)

if min_x <= x <= max_x and max_y <= y :
print("거리 : math.sqrt((abs(x - mid_x) ** 2) + (abs(y - mid_y) ** 2))" )
words.append((word, math.sqrt((abs(x - mid_x) ** 2) + (abs(y - mid_y) ** 2)))) # (단어, 단어의 가운데 좌표 값과 손 좌표 간 거리)


words.sort(key=lambda x: x[1]) # 손과 가장 가까운 단어를 반환
print(words)
# 가장 긴 문자열 찾기
if words:
result_string = words[0][0] # 제일 가까운 단어 반환하도록 세팅
else:
result_string = "해당위치에 문자열이 없습니다."
#print(finalResponse.boundingPoly[0].vertices[0].x)
# print(finalResponse.boundingPoly[0])
return result_string

#image_uri = "https://storage.googleapis.com/gdcs-sc-beadyeyes-bucket/140d792f-7fe6-4ef9-b062-0583aa39c05e"
#image_uri = os.environ.get("IMAGE_URI", "https://cloud.google.com/static/vision/docs/images/sign_small.jpg")
Expand Down
64 changes: 63 additions & 1 deletion OCR/handLandmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def get_finger_coordinate(uri):



def text_pointer(uri):
def text_pointer_uri(uri):
# 이미지에서 텍스트를 추출

x, y = get_finger_coordinate(uri)
Expand All @@ -114,3 +114,65 @@ def text_pointer(uri):



###################################################################################################################
# here the code for file upload



def get_finger_coordinate_file(imagefile):
base_options = python.BaseOptions(model_asset_path='hand_landmarker.task')
options = vision.HandLandmarkerOptions(base_options=base_options,
num_hands=1)
detector = vision.HandLandmarker.create_from_options(options)

# STEP 3: Load the input image.

image = mp.Image.create_from_file(imagefile)


# uri로부터 이미지를 읽어옴
# image = read_image_from_uri(uri)
# image = mp.Image(image_format=mp.ImageFormat.SRGB, data=image)

image_shape = image.numpy_view().shape

# STEP 4: Detect hand landmarks from the input image.
detection_result = detector.detect(image)

right_hand_x_coordinate = 0
right_hand_y_coordinate = 0



# cv2.imshow("Annotated Image", image.numpy_view())
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# print(image_shape)
# print(detection_result.hand_landmarks[0][8])
try:
right_hand_x_coordinate = int(detection_result.hand_landmarks[0][8].x * image_shape[1])
right_hand_y_coordinate = int(detection_result.hand_landmarks[0][8].y * image_shape[0])


except:
# 손가락 인식 실패
return -1, -1

return right_hand_x_coordinate, right_hand_y_coordinate


def text_pointer_file(imagefile):
# 이미지에서 텍스트를 추출
x, y = get_finger_coordinate(imagefile)

if x == -1 and y == -1:
return "손가락 인식에 실패했습니다."

text = googleOCR.text_pointer(imagefile, x, y)

return text




0 comments on commit dd0cc06

Please sign in to comment.