krishnamishra8848 commited on
Commit
2e77031
·
verified ·
1 Parent(s): 41b657d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -37,15 +37,29 @@ full_plate_model, character_model = load_models()
37
  # Function to detect and crop license plates
38
  def detect_and_crop_license_plate(image):
39
  img_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
40
- results = full_plate_model(img_bgr)
41
-
42
  cropped_images = []
 
43
  for result in results:
44
  if hasattr(result, 'boxes') and result.boxes is not None:
45
  for box in result.boxes.xyxy:
46
  x1, y1, x2, y2 = map(int, box)
47
- cropped_image = img_bgr[y1:y2, x1:x2]
48
- cropped_images.append(cropped_image)
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  return cropped_images, img_bgr
51
 
 
37
  # Function to detect and crop license plates
38
  def detect_and_crop_license_plate(image):
39
  img_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
40
+ results = full_plate_model(img_bgr, conf=0.5) # Use a higher confidence threshold
41
+
42
  cropped_images = []
43
+ h, w, _ = img_bgr.shape
44
  for result in results:
45
  if hasattr(result, 'boxes') and result.boxes is not None:
46
  for box in result.boxes.xyxy:
47
  x1, y1, x2, y2 = map(int, box)
48
+
49
+ # Add padding to the bounding box
50
+ padding = 10
51
+ x1 = max(0, x1 - padding)
52
+ y1 = max(0, y1 - padding)
53
+ x2 = min(w, x2 + padding)
54
+ y2 = min(h, y2 + padding)
55
+
56
+ # Avoid extremely small detections
57
+ if (x2 - x1) > 20 and (y2 - y1) > 20:
58
+ cropped_image = img_bgr[y1:y2, x1:x2]
59
+
60
+ # Resize to a consistent size
61
+ cropped_image = cv2.resize(cropped_image, (224, 224), interpolation=cv2.INTER_LINEAR)
62
+ cropped_images.append(cropped_image)
63
 
64
  return cropped_images, img_bgr
65