xangcastle commited on
Commit
ab952d9
·
1 Parent(s): c683b90

add sorter and improve detections

Browse files
Files changed (3) hide show
  1. .gitignore +3 -1
  2. app.py +15 -11
  3. detector/utils.py +44 -0
.gitignore CHANGED
@@ -1 +1,3 @@
1
- .idea
 
 
 
1
+ .idea
2
+ plates
3
+ flagged
app.py CHANGED
@@ -2,7 +2,8 @@ import gradio as gr
2
  import numpy as np
3
  import cv2
4
  from norfair import Detection, Tracker, Video
5
- from detector.utils import detect_plates, detect_chars, imcrop
 
6
 
7
  DISTANCE_THRESHOLD_BBOX: float = 0.7
8
  DISTANCE_THRESHOLD_CENTROID: int = 30
@@ -31,15 +32,17 @@ def yolo_to_norfair(yolo_detections):
31
 
32
 
33
  def fn_image(foto):
34
- plates = detect_plates(foto)
35
  plates_text = []
36
- if len(plates) > 0:
37
- for plate in plates:
38
- p1, p2, crop = plate
 
 
 
39
  if len(crop) > 0:
40
- cv2.rectangle(foto, p1, p2, (0, 0, 255), 2)
41
- text, crop = detect_chars(crop)
42
- cv2.putText(foto, text, p1, cv2.FONT_HERSHEY_SIMPLEX, 4, (0, 255, 0), 5)
43
  plates_text.append(text)
44
  return foto, plates_text
45
 
@@ -62,12 +65,10 @@ def fn_video(video, initial_time, duration):
62
  ret, frame = cap.read()
63
  if not ret:
64
  break
 
65
  except Exception as e:
66
  print(e)
67
  continue
68
- # num_frames += 1
69
- # if num_frames % 3 != 0:
70
- # continue
71
  if num_frames < min_frame:
72
  num_frames += 1
73
  continue
@@ -82,6 +83,8 @@ def fn_video(video, initial_time, duration):
82
  crop = imcrop(frame, bbox)
83
  text = detect_chars(crop)
84
  plates[obj.id] = text
 
 
85
 
86
  cv2.rectangle(
87
  frame,
@@ -90,6 +93,7 @@ def fn_video(video, initial_time, duration):
90
  (0, 255, 0),
91
  2,
92
  )
 
93
  cv2.putText(
94
  frame,
95
  plates[obj.id],
 
2
  import numpy as np
3
  import cv2
4
  from norfair import Detection, Tracker, Video
5
+ from detector.utils import detect_plates, detect_chars, imcrop, send_request, draw_text
6
+ from threading import Thread
7
 
8
  DISTANCE_THRESHOLD_BBOX: float = 0.7
9
  DISTANCE_THRESHOLD_CENTROID: int = 30
 
32
 
33
 
34
  def fn_image(foto):
 
35
  plates_text = []
36
+ plates = detect_plates(foto)
37
+ records = plates.pandas().xyxy[0].to_dict(orient='records')
38
+ if records:
39
+ for plate in records:
40
+ xi, yi, xf, yf = int(plate['xmin']), int(plate['ymin']), int(plate['xmax']), int(plate['ymax'])
41
+ crop = imcrop(foto, (xi, yi, xf, yf))
42
  if len(crop) > 0:
43
+ cv2.rectangle(foto, (xi, yi), (xf, yf), (0, 255, 0), 2)
44
+ text = detect_chars(crop)
45
+ draw_text(foto, text, (xi, yi))
46
  plates_text.append(text)
47
  return foto, plates_text
48
 
 
65
  ret, frame = cap.read()
66
  if not ret:
67
  break
68
+ frame_copy = frame.copy()
69
  except Exception as e:
70
  print(e)
71
  continue
 
 
 
72
  if num_frames < min_frame:
73
  num_frames += 1
74
  continue
 
83
  crop = imcrop(frame, bbox)
84
  text = detect_chars(crop)
85
  plates[obj.id] = text
86
+ thread = Thread(target=send_request, args=(frame_copy, text, bbox))
87
+ thread.start()
88
 
89
  cv2.rectangle(
90
  frame,
 
93
  (0, 255, 0),
94
  2,
95
  )
96
+ draw_text(frame, plates[obj.id], (bbox[0], bbox[1]))
97
  cv2.putText(
98
  frame,
99
  plates[obj.id],
detector/utils.py CHANGED
@@ -2,6 +2,9 @@ import torch
2
  import numpy as np
3
  import cv2
4
  import os
 
 
 
5
 
6
  BASE_DIR = os.path.abspath(os.getcwd())
7
 
@@ -42,3 +45,44 @@ def detect_chars(img):
42
  records = sorted(records, key=lambda d: d['xmin'])
43
  text = ''.join([i.get('name') for i in records])
44
  return text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import numpy as np
3
  import cv2
4
  import os
5
+ from datetime import datetime
6
+ from json import dumps
7
+ import requests
8
 
9
  BASE_DIR = os.path.abspath(os.getcwd())
10
 
 
45
  records = sorted(records, key=lambda d: d['xmin'])
46
  text = ''.join([i.get('name') for i in records])
47
  return text
48
+
49
+
50
+ def draw_text(img, text,
51
+ pos=(0, 0),
52
+ font_scale=1,
53
+ font_thickness=2,
54
+ text_color=(0, 255, 0),
55
+ text_color_bg=(0, 0, 0)
56
+ ):
57
+ x, y = pos
58
+ text_size, _ = cv2.getTextSize(text, 0, font_scale, font_thickness)
59
+ text_w, text_h = text_size
60
+ cv2.rectangle(img, pos, (x + text_w, y - text_h), text_color_bg, -1)
61
+ cv2.putText(img, text, (x, y), 0, font_scale, text_color, font_thickness)
62
+
63
+
64
+ def send_request(frame, text, bbox):
65
+ cv2.rectangle(
66
+ frame,
67
+ (bbox[0], bbox[1]),
68
+ (bbox[2], bbox[3]),
69
+ (0, 255, 0),
70
+ 2,
71
+ )
72
+ draw_text(frame, text, (bbox[0], bbox[1]))
73
+ url = "https://api.prevantec.com/toll-plates"
74
+ data = {
75
+ "number": text,
76
+ "camera": "camera_1",
77
+ "spot_on": str(datetime.now()),
78
+ }
79
+ if not os.path.exists(os.path.join(BASE_DIR, 'plates')):
80
+ os.makedirs(os.path.join(BASE_DIR, 'plates'))
81
+ filename = os.path.join(BASE_DIR, 'plates', f'{text}.jpg')
82
+ cv2.imwrite(filename, frame)
83
+ payload = {'data': dumps(data)}
84
+ files = [
85
+ ('files', (f'{text}.jpg', open(filename, 'rb'), 'image/jpg'))
86
+ ]
87
+ headers = {}
88
+ requests.request("POST", url, headers=headers, data=payload, files=files)