Skip to content

Commit

Permalink
Add post request
Browse files Browse the repository at this point in the history
  • Loading branch information
johnng0805 committed May 31, 2022
1 parent c593226 commit d75d688
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,6 @@ com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# videos folder
videos/
27 changes: 25 additions & 2 deletions detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions utils/image_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d75d688

Please sign in to comment.