From cfc048873801e3df22e8f9ead5f2c8800cd6ae76 Mon Sep 17 00:00:00 2001
From: Han Donghyeon <hando1220@ajou.ac.kr>
Date: Wed, 4 Dec 2024 21:21:22 +0900
Subject: [PATCH] =?UTF-8?q?feat(ocr):=20=EC=B0=A8=EB=9F=89=20=EB=A1=9C?=
 =?UTF-8?q?=EA=B7=B8=20=EC=A1=B0=ED=9A=8C=20=EC=9B=B9=20API=20=EC=B6=94?=
 =?UTF-8?q?=EA=B0=80?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 camera-node-ocr/ocr.py | 54 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/camera-node-ocr/ocr.py b/camera-node-ocr/ocr.py
index becf194..de850dd 100644
--- a/camera-node-ocr/ocr.py
+++ b/camera-node-ocr/ocr.py
@@ -3,6 +3,11 @@ import re
 import time
 import socket
 from paddleocr import PaddleOCR
+import http.server
+import json
+import os
+import threading
+from urllib.parse import urlparse
 
 paddle_ocr = PaddleOCR(lang='korean', show_log=False, use_angle_cls=False)
 print("모델 로딩이 완료되었습니다.")
@@ -61,5 +66,54 @@ def start_server(host='127.0.0.1', port=3000):
             print(f"클라이언트 연결 종료: {addr}")
             client_socket.close()
 
+def handle_http_request(handler):
+    path = urlparse(handler.path).path
+    
+    if path == '/api/images':
+        send_file_list(handler, '../images')
+    elif path == '/api/plates':
+        send_file_list(handler, '../plates')
+    elif path.startswith('/images/') or path.startswith('/plates/'):
+        send_image(handler, '../' + path.lstrip('/'))
+    else:
+        handler.send_error(404, "File not found")
+
+def send_file_list(handler, dir_path):
+    try:
+        files = [file for file in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, file))]
+        handler.send_response(200)
+        handler.send_header('Content-Type', 'application/json')
+        handler.send_header('Access-Control-Allow-Origin', '*')
+        handler.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS')
+        handler.end_headers()
+        handler.wfile.write(json.dumps(files).encode())
+    except Exception as e:
+        handler.send_error(500, str(e))
+
+def send_image(handler, file_path):
+    if os.path.exists(file_path):
+        with open(file_path, 'rb') as f:
+            handler.send_response(200)
+            handler.send_header('Content-Type', 'image/jpeg')
+            handler.send_header('Access-Control-Allow-Origin', '*')
+            handler.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS')
+            handler.end_headers()
+            handler.wfile.write(f.read())
+    else:
+        handler.send_error(404, "File not found")
+
+def start_http_server(port=8000):
+    class RequestHandler(http.server.SimpleHTTPRequestHandler):
+        def do_GET(self):
+            handle_http_request(self)
+            
+    server = http.server.HTTPServer(('', port), RequestHandler)
+    print(f"HTTP 서버가 포트 {port}에서 시작되었습니다.")
+    server.serve_forever()
+
 if __name__ == "__main__":
+    http_thread = threading.Thread(target=start_http_server)
+    http_thread.daemon = True
+    http_thread.start()
+
     start_server()
-- 
GitLab