diff --git a/.gitignore b/.gitignore index 746a733e2f021bd540f35e4d3b87f8f6b0e7dfd5..091199be7e02cef35413fdd26eed073bf3fb35a3 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 0000000000000000000000000000000000000000..becf194113c75cd8b6bbcebafda19e06ebeb6816 --- /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()