Skip to content

Commit

Permalink
add logic with tcp & http server
Browse files Browse the repository at this point in the history
  • Loading branch information
goolat committed Aug 22, 2023
1 parent c71d28d commit 513e5ec
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 52 deletions.
107 changes: 56 additions & 51 deletions control/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import sys
sys.path.insert(1,'../model/')

from joolpr.infer import LPRModel
from joodetection.detector import get_detector
sys.path.insert(1, '../model/joodetection/')
import threading
import time
import base64

import cv2
import threading
import serial


block = 1
from joolpr.infer import LPRModel
from joodetection.detector import get_detector
from tcp_function import *

ser = None
def free_serial():
Expand All @@ -32,53 +33,57 @@ def activate_barrier():

def deactivate_barrier():
global ser
import time
time.sleep(10)
ser.write(bytes(bytearray([100])))

def dummy_serial():
while barrier_running:
pass


def tcp_connection():
print('tcp connected')

def free_tcp_socket():
print('free tcp socket')

def dummy_tcp():
while tcp_running:
pass

def send_log_data(id:int, frame, block):
print(f'send log to server : {id} {block} time:time!')


subjects = ['5721']
subjects = []
polling_running = True
polling_thread = None
def polling_subject_number():
global polling_running
global subjects
while polling_running:
try:
str_list = get_car_num_list()
subjects = [s.strip() for s in str_list.replace('[','').replace(']','').replace('\n', '').replace('\"','').split(',')]
time.sleep(1)
except:
pass
polling_thread = threading.Thread(target=polling_subject_number)
polling_thread.start()
def compare_with_subjects(number : str):
global subjects
if number in subjects:
return 1
else:
return 0
id = 0
barrier_thread = None
barrier_running = True
tcp_thread = None
data_buffer = []
tcp_running = True
def send_data_subroutine():
global data_buffer
global tcp_client
while tcp_running:
if len(data_buffer) > 0:
try:
id, frame, number = data_buffer.pop()
send_log_data(tcp_client, id, frame, number)
except:
pass
finally:
data_buffer.clear()
pass
else:
time.sleep(0.1)
pass

serial_connection()
tcp_connection()
id = 0

barrier_thread = threading.Thread(target=dummy_serial)
tcp_thread = threading.Thread(target=dummy_tcp)
serial_connection()
tcp_client = tcp_connection()

barrier_thread.start()
tcp_thread = threading.Thread(target=send_data_subroutine)
tcp_thread.start()


sys.path.insert(1, '../model/joodetection/')
detector = get_detector()
lpr_model = LPRModel()
cap = cv2.VideoCapture(2)
Expand All @@ -98,26 +103,26 @@ def compare_with_subjects(number : str):
if is_subject:
cv2.rectangle(frame, tl, br, (0,0,255), thickness=3)
activate_barrier()
send_log_data(id, frame, block)
success, buffer = cv2.imencode('.jpg', frame)
if success:
print(type(buffer))
encoded_img = base64.b64encode(buffer).decode(encoding='utf-8')
data_buffer.append((id, encoded_img, number))
cv2.imshow("crop",cropped_frame)
else:
pass
cv2.imshow('img', frame)
key = cv2.waitKey(1) & 0xff
if key == ord('q'):
break

#free resources
cap.release()
cv2.destroyAllWindows()
lpr_model.free()

## join barrier thread & release serial connection
if barrier_thread:
barrier_running = False
barrier_thread.join()
free_serial()

## join tcp thread & release tcp socket
if tcp_thread:
tcp_running = False
tcp_thread.join()
free_tcp_socket()
free_serial()
free_tcp_sosket(tcp_client)
tcp_running = False
tcp_thread.join()
polling_running = False
polling_thread.join()
2 changes: 1 addition & 1 deletion control/tcp_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def free_tcp_sosket(socket):
socket.close()

def get_car_num_list():
resp = requests.get('http://10.10.14.2:4000/car-number-list')
resp = requests.get('http://10.10.14.220:8000/car-number-list')
print("carnum is ", resp.text)

result = resp.text
Expand Down

0 comments on commit 513e5ec

Please sign in to comment.