From cebe2dedfa7ed193e1a6da03030844a971110277 Mon Sep 17 00:00:00 2001
From: Han Donghyeon <hando1220@ajou.ac.kr>
Date: Wed, 4 Dec 2024 10:04:31 +0000
Subject: [PATCH] =?UTF-8?q?feat(ocr):=20Paddle=20OCR=20=EA=B8=B0=EB=B0=98?=
 =?UTF-8?q?=20=EB=B2=88=ED=98=B8=ED=8C=90=20OCR?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitignore             |  5 +++-
 camera-node-ocr/ocr.py | 65 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 camera-node-ocr/ocr.py

diff --git a/.gitignore b/.gitignore
index 746a733..091199b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,4 +12,7 @@ plates/
 
 # 시스템 파일
 .DS_Store
-.directory
\ No newline at end of file
+.directory
+
+# 파이썬
+venv/
\ No newline at end of file
diff --git a/camera-node-ocr/ocr.py b/camera-node-ocr/ocr.py
new file mode 100644
index 0000000..becf194
--- /dev/null
+++ b/camera-node-ocr/ocr.py
@@ -0,0 +1,65 @@
+import cv2
+import re
+import time
+import socket
+from paddleocr import PaddleOCR
+
+paddle_ocr = PaddleOCR(lang='korean', show_log=False, use_angle_cls=False)
+print("모델 로딩이 완료되었습니다.")
+
+def ocr(img_path):
+    plate_img = cv2.imread(img_path)
+    
+    start_time = time.time()
+    result = paddle_ocr.ocr(plate_img, cls=True)
+    
+    if not result or not result[0]:
+        return "No text detected"
+
+    # 모든 텍스트를 x 좌표 기준으로 정렬하여 처리
+    all_words = []
+    for line in result:
+        for word_info in line:
+            x_coord = word_info[0][0][0]
+            text = word_info[1][0]
+            all_words.append((x_coord, text))
+    
+    all_words.sort(key=lambda x: x[0])
+    full_text = ''.join(word[1] for word in all_words)
+    full_text = re.sub(r'[^0-9가-힣]', '', full_text)
+    
+    process_time = time.time() - start_time
+    print(f"처리 시간: {process_time:.2f}초")
+    
+    # 번호판 형식 검증
+    if re.search(r'\d{2,3}[가-힣]\d{4}', full_text):
+        print(f"인식된 번호판: {full_text}")
+        return full_text
+    else:
+        return "Invalid plate format"
+
+def start_server(host='127.0.0.1', port=3000):
+    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+    server_socket.bind((host, port))
+    server_socket.listen(1)
+    print("OCR 서버가 시작되었습니다.")
+    
+    while True:
+        client_socket, addr = server_socket.accept()
+        print(f"클라이언트 연결됨: {addr}")
+
+        try:
+            while True:
+                data = client_socket.recv(1024).decode()
+                ocr_text = ocr(data)
+                client_socket.send(ocr_text.encode())
+                
+        except Exception as e:
+            print(f"오류 발생: {e}")
+        finally:
+            print(f"클라이언트 연결 종료: {addr}")
+            client_socket.close()
+
+if __name__ == "__main__":
+    start_server()
-- 
GitLab