SocialAI commited on
Commit
a8effe4
·
verified ·
1 Parent(s): 65542b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -29
app.py CHANGED
@@ -1,26 +1,29 @@
 
 
1
  import gradio as gr
2
  from ultralytics import YOLO
3
  import cv2
4
  import numpy as np
5
- import pytesseract
6
  import tempfile
7
  import os
8
  import subprocess
9
 
10
- # Install Tesseract OCR if not present (for Hugging Face Spaces)
11
- if not os.path.exists('/usr/bin/tesseract'):
12
- subprocess.run(['apt-get', 'update'], check=True)
13
- subprocess.run(['apt-get', 'install', '-y', 'tesseract-ocr'], check=True)
14
-
15
- # Set Tesseract path explicitly
16
- pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
17
 
18
- # Load your YOLO model
19
- model = YOLO('best.pt') # Replace with your custom model if needed
20
  img_dim = (640, 640)
21
 
22
- def predict(image, conf_threshold, iou_threshold):
 
 
 
 
 
23
 
 
24
  # Resizing
25
  image = image.resize(img_dim)
26
 
@@ -40,7 +43,7 @@ def predict(image, conf_threshold, iou_threshold):
40
  annotated_image = results[0].plot()
41
 
42
  # Perform OCR on detected objects
43
- ocr_results = []
44
  for box in results[0].boxes.xyxy.cpu().numpy():
45
  x1, y1, x2, y2 = map(int, box)
46
  cropped = image[y1:y2, x1:x2]
@@ -48,38 +51,36 @@ def predict(image, conf_threshold, iou_threshold):
48
  # Skip if the cropped region is too small
49
  if cropped.size == 0:
50
  continue
51
-
52
- # Preprocess for better OCR
53
- gray = cv2.cvtColor(cropped, cv2.COLOR_BGR2GRAY)
54
- gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
55
-
56
- # Perform OCR
57
- text = pytesseract.image_to_string(gray, config='--psm 7')
58
- clean_text = ''.join(c for c in text.strip() if c.isalnum())
 
 
59
 
60
- if clean_text:
61
- ocr_results.append(clean_text)
62
  # Add text to annotated image
63
- cv2.putText(annotated_image, clean_text, (x1, y1-10),
64
  cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
65
 
66
  # Convert back to RGB for Gradio display
67
- annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
68
-
69
- # Format OCR results
70
- ocr_text = "Detected text:\n" + "\n".join(ocr_results) if ocr_results else "No text detected"
71
 
72
  return annotated_image, ocr_text
 
73
 
74
  # Create Gradio interface
75
  with gr.Blocks() as demo:
76
- gr.Markdown("# YOLO Object Detection with OCR")
77
  with gr.Row():
78
  with gr.Column():
79
  input_image = gr.Image(label="Input Image", type="pil")
80
  conf_slider = gr.Slider(0, 1, value=0.25, label="Confidence Threshold")
81
  iou_slider = gr.Slider(0, 1, value=0.45, label="IOU Threshold")
82
- submit_btn = gr.Button("Detect Objects")
83
  with gr.Column():
84
  output_image = gr.Image(label="Detected Objects")
85
  ocr_output = gr.Textbox(label="OCR Results")
 
1
+ from typing import get_args
2
+ from PIL import Image
3
  import gradio as gr
4
  from ultralytics import YOLO
5
  import cv2
6
  import numpy as np
 
7
  import tempfile
8
  import os
9
  import subprocess
10
 
11
+ from fast_alpr import ALPR
12
+ from fast_alpr.default_detector import PlateDetectorModel
13
+ from fast_alpr.default_ocr import OcrModel
 
 
 
 
14
 
15
+ # Loading YOLO model
16
+ model = YOLO('best.pt')
17
  img_dim = (640, 640)
18
 
19
+ # Default models for plate recognition
20
+ DETECTOR_MODELS = list(get_args(PlateDetectorModel))
21
+ OCR_MODELS = list(get_args(OcrModel))
22
+ # Put global OCR first
23
+ OCR_MODELS.remove("global-plates-mobile-vit-v2-model")
24
+ OCR_MODELS.insert(0, "global-plates-mobile-vit-v2-model")
25
 
26
+ def predict(image, conf_threshold, iou_threshold):
27
  # Resizing
28
  image = image.resize(img_dim)
29
 
 
43
  annotated_image = results[0].plot()
44
 
45
  # Perform OCR on detected objects
46
+ ocr_text= ""
47
  for box in results[0].boxes.xyxy.cpu().numpy():
48
  x1, y1, x2, y2 = map(int, box)
49
  cropped = image[y1:y2, x1:x2]
 
51
  # Skip if the cropped region is too small
52
  if cropped.size == 0:
53
  continue
54
+ # Apply detector for plate region
55
+ alpr = ALPR(detector_model=DETECTOR_MODELS[0], ocr_model=OCR_MODELS[0])
56
+ alpr_results = alpr.predict(cropped)
57
+
58
+ if alpr_results:
59
+ res = alpr_results[0]
60
+ # Access the detection and OCR attributes from ALPRResult
61
+ plate_text = res.ocr.text if res.ocr else "N/A"
62
+ plate_confidence = res.ocr.confidence if res.ocr else 0.0
63
+ ocr_text += f"- Detected Plate: {plate_text} with confidence {plate_confidence:.2f}\n"
64
 
 
 
65
  # Add text to annotated image
66
+ cv2.putText(annotated_image, plate_text, (x1, y1-10),
67
  cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
68
 
69
  # Convert back to RGB for Gradio display
70
+ annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
 
 
 
71
 
72
  return annotated_image, ocr_text
73
+
74
 
75
  # Create Gradio interface
76
  with gr.Blocks() as demo:
77
+ gr.Markdown("# MIA-Yolov8 for peruvian plate recognition")
78
  with gr.Row():
79
  with gr.Column():
80
  input_image = gr.Image(label="Input Image", type="pil")
81
  conf_slider = gr.Slider(0, 1, value=0.25, label="Confidence Threshold")
82
  iou_slider = gr.Slider(0, 1, value=0.45, label="IOU Threshold")
83
+ submit_btn = gr.Button("Run model")
84
  with gr.Column():
85
  output_image = gr.Image(label="Detected Objects")
86
  ocr_output = gr.Textbox(label="OCR Results")