Sakibrumu commited on
Commit
307c8f3
·
verified ·
1 Parent(s): e51c033

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -46
app.py CHANGED
@@ -4,65 +4,52 @@ import cv2
4
  import pytesseract
5
  import numpy as np
6
  from PIL import Image
7
- import sys
8
- import os
9
-
10
  from ultralytics import YOLO
11
 
12
  # Load model
13
- model = YOLO("/home/user/app/best.pt") # আপনি যেই path এ best.pt রেখেছেন
14
-
15
 
 
 
16
 
17
-
18
- # Frame processing function
19
  def process_frame(frame):
20
- # Resize image to 640x640
21
- frame_resized = cv2.resize(frame, (640, 640))
22
-
23
- img = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2RGB)
24
- img_tensor = torch.from_numpy(img).permute(2, 0, 1).float() / 255.0
25
- img_tensor = img_tensor.unsqueeze(0)
26
-
27
- # Run inference with the YOLO model (no need to manually apply nms)
28
- results = model(img_tensor, augment=False)
29
-
30
- # Extract results (list of detections)
31
- detections = results.xywh[0] # YOLO's detection results
32
 
33
  extracted_texts = []
34
  confidences = []
35
 
36
  for det in detections:
37
- x_center, y_center, width, height, conf, cls = det.tolist()
38
- if conf > 0.5:
39
- # Convert from YOLO format to bounding box format
40
- x1 = int((x_center - width / 2) * 640)
41
- y1 = int((y_center - height / 2) * 640)
42
- x2 = int((x_center + width / 2) * 640)
43
- y2 = int((y_center + height / 2) * 640)
44
- cls = int(cls)
45
-
46
- label_map = {0: "Analog", 1: "Digital", 2: "Non-LP"}
47
- label = label_map.get(cls, "Unknown")
48
- percent = f"{conf * 100:.2f}%"
49
-
50
- # Draw box & label
51
- cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2)
52
- cv2.putText(frame, f"{label}: {percent}", (x1, y1 - 10),
53
- cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
54
-
55
- # OCR
56
- lp_crop = frame[y1:y2, x1:x2]
57
- gray = cv2.cvtColor(lp_crop, cv2.COLOR_BGR2GRAY)
58
  text = pytesseract.image_to_string(gray, config="--psm 6 -l ben")
59
  extracted_texts.append(text.strip())
60
  confidences.append(percent)
61
 
62
- return frame, "\n".join(extracted_texts), ", ".join(confidences)
 
 
 
 
63
 
64
 
65
- # Input handler
66
  def process_input(input_file):
67
  file_path = input_file.name
68
 
@@ -77,9 +64,8 @@ def process_input(input_file):
77
  if frame is None:
78
  return None, "Invalid image", ""
79
 
80
- processed_frame, text, confidence = process_frame(frame)
81
- processed_pil = Image.fromarray(cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB))
82
- return processed_pil, text, confidence
83
 
84
  interface = gr.Interface(
85
  fn=process_input,
@@ -90,7 +76,7 @@ interface = gr.Interface(
90
  gr.Textbox(label="Confidence (%)")
91
  ],
92
  title="YOLOv10n License Plate Detector (Bangla)",
93
- description="Upload an image or video. Detects plates and extracts Bangla text using OCR (CPU)."
94
  )
95
 
96
  interface.launch()
 
4
  import pytesseract
5
  import numpy as np
6
  from PIL import Image
 
 
 
7
  from ultralytics import YOLO
8
 
9
  # Load model
10
+ model = YOLO("/home/user/app/best.pt")
 
11
 
12
+ # Label map
13
+ label_map = {0: "Analog", 1: "Digital", 2: "Non-LP"}
14
 
 
 
15
  def process_frame(frame):
16
+ # Resize to YOLO input shape
17
+ input_img = cv2.resize(frame, (640, 640))
18
+ results = model(input_img)[0]
19
+ detections = results.boxes.data.cpu().numpy()
 
 
 
 
 
 
 
 
20
 
21
  extracted_texts = []
22
  confidences = []
23
 
24
  for det in detections:
25
+ if len(det) < 6:
26
+ continue
27
+
28
+ x1, y1, x2, y2, conf, cls = det
29
+ x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
30
+ label = label_map.get(int(cls), "Unknown")
31
+ percent = f"{conf * 100:.2f}%"
32
+
33
+ # Draw box and label on image
34
+ cv2.rectangle(input_img, (x1, y1), (x2, y2), (255, 0, 0), 2)
35
+ cv2.putText(input_img, f"{label}: {percent}", (x1, y1 - 10),
36
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
37
+
38
+ # OCR
39
+ cropped = frame[y1:y2, x1:x2] # Use original frame for OCR
40
+ if cropped.size > 0:
41
+ gray = cv2.cvtColor(cropped, cv2.COLOR_BGR2GRAY)
 
 
 
 
42
  text = pytesseract.image_to_string(gray, config="--psm 6 -l ben")
43
  extracted_texts.append(text.strip())
44
  confidences.append(percent)
45
 
46
+ # Convert to PIL
47
+ annotated = cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB)
48
+ pil_img = Image.fromarray(annotated)
49
+
50
+ return pil_img, "\n".join(extracted_texts), ", ".join(confidences)
51
 
52
 
 
53
  def process_input(input_file):
54
  file_path = input_file.name
55
 
 
64
  if frame is None:
65
  return None, "Invalid image", ""
66
 
67
+ return process_frame(frame)
68
+
 
69
 
70
  interface = gr.Interface(
71
  fn=process_input,
 
76
  gr.Textbox(label="Confidence (%)")
77
  ],
78
  title="YOLOv10n License Plate Detector (Bangla)",
79
+ description="Upload an image or video. Detects license plates and extracts Bangla text using OCR."
80
  )
81
 
82
  interface.launch()