From d75d688d1e8fe01835465b22115234256c588b1a Mon Sep 17 00:00:00 2001 From: johnng0805 Date: Wed, 1 Jun 2022 00:02:11 +0700 Subject: [PATCH] Add post request --- .gitignore | 3 +++ detect.py | 27 +++++++++++++++++++++++++-- utils/image_crop.py | 2 ++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 69a00843ea42..c8737a6e80e4 100755 --- a/.gitignore +++ b/.gitignore @@ -254,3 +254,6 @@ com_crashlytics_export_strings.xml crashlytics.properties crashlytics-build.properties fabric.properties + +# videos folder +videos/ diff --git a/detect.py b/detect.py index b96123bc5cde..00ede86b6dce 100644 --- a/detect.py +++ b/detect.py @@ -24,6 +24,7 @@ yolov5s_edgetpu.tflite # TensorFlow Edge TPU """ +import requests from utils.openalpr import Alpr from utils.torch_utils import select_device, time_sync from utils.plots import Annotator, colors, save_one_box @@ -40,6 +41,9 @@ import torch import torch.backends.cudnn as cudnn +from dotenv import load_dotenv +load_dotenv() + FILE = Path(__file__).resolve() ROOT = FILE.parents[0] # YOLOv5 root directory if str(ROOT) not in sys.path: @@ -104,6 +108,13 @@ def run( alpr.set_top_n(1) alpr.set_detect_region(False) + # Declare API Endpoint + API_ENDPOINT = os.environ['API_ENDPOINT'] + print('Application Backend: ' + API_ENDPOINT) + + # Previous plate value + prev_plate = '' + # Dataloader if webcam: view_img = check_imshow() @@ -182,13 +193,25 @@ def run( # save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True) c = int(cls) if c == 0: - label = None imCrop = crop(xyxy, imc) plate_num = alpr.recognize_ndarray(imCrop) if (plate_num['results'] != []): plate = plate_num['results'][0]['plate'] - print(plate) + + if plate and plate != prev_plate: + data = {'plate_num': plate} + + try: + r = requests.post(API_ENDPOINT, json=data, timeout=1.5) + prev_plate = plate + print(r.text) + except requests.Timeout: + print('Connection timeout') + pass + except requests.ConnectionError: + print('Connection error') + pass # Stream results im0 = annotator.result() diff --git a/utils/image_crop.py b/utils/image_crop.py index 70fb87ff237f..126a53154817 100644 --- a/utils/image_crop.py +++ b/utils/image_crop.py @@ -19,6 +19,8 @@ def crop(xyxy, im, square=False, gain=1.02, pad=10, BGR=False): crop = im[int(xyxy[0, 1]):int(xyxy[0, 3]), int(xyxy[0, 0]):int(xyxy[0, 2]), ::(1 if BGR else -1)] + # crop = cv2.resize(crop, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC) + gray = cv2.cvtColor(crop, cv2.COLOR_RGB2GRAY) medBlur = cv2.medianBlur(gray, 3)