diff --git a/camera-node-ocr/ocr.py b/camera-node-ocr/ocr.py index becf194113c75cd8b6bbcebafda19e06ebeb6816..de850dd3ed253bb2214e3ce610ba2a61485b2586 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()