krishnamishra8848 commited on
Commit
2fc3066
·
verified ·
1 Parent(s): ba9d2c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -38,8 +38,8 @@ def load_models():
38
  # Load models
39
  full_plate_model, character_model, recognition_model = load_models()
40
 
41
- # Function to detect license plates and prepare for character recognition
42
- def process_license_plate(image):
43
  img_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
44
  results = full_plate_model(img_bgr)
45
 
@@ -52,24 +52,18 @@ def process_license_plate(image):
52
 
53
  return detected_image, img_bgr
54
 
55
- # Function to detect and recognize characters directly from the original image
56
- def detect_and_recognize_characters(image):
57
- # Detect characters
58
  results = character_model(image)
 
59
  character_crops = []
60
  for result in results:
61
  if hasattr(result, 'boxes') and result.boxes is not None:
62
  for box in result.boxes.xyxy:
63
  x1, y1, x2, y2 = map(int, box)
 
64
  character_crops.append(image[y1:y2, x1:x2])
65
-
66
- # Recognize characters
67
- if character_crops:
68
- recognized_characters = recognize_characters(character_crops)
69
- else:
70
- recognized_characters = []
71
-
72
- return recognized_characters
73
 
74
  # Function to recognize characters
75
  def recognize_characters(character_crops):
@@ -98,17 +92,22 @@ if uploaded_file is not None:
98
  # Load image
99
  image = Image.open(uploaded_file)
100
 
101
- # Process the image for license plate detection
102
  with st.spinner("Processing image..."):
103
- detected_image, original_image = process_license_plate(image)
104
 
105
- # Show the detected image with bounding boxes
106
  st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB), caption="Detected License Plates", use_container_width=True)
107
 
108
- # Perform character detection and recognition on the original image
109
- recognized_characters = detect_and_recognize_characters(original_image)
 
 
 
110
 
111
- if recognized_characters:
 
 
112
  st.write("Recognized Characters:", "".join(recognized_characters))
113
  else:
114
  st.write("No characters detected in the image.")
 
38
  # Load models
39
  full_plate_model, character_model, recognition_model = load_models()
40
 
41
+ # Function to detect license plates
42
+ def detect_license_plate(image):
43
  img_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
44
  results = full_plate_model(img_bgr)
45
 
 
52
 
53
  return detected_image, img_bgr
54
 
55
+ # Function to detect characters
56
+ def detect_characters(image):
 
57
  results = character_model(image)
58
+ detected_image = image.copy()
59
  character_crops = []
60
  for result in results:
61
  if hasattr(result, 'boxes') and result.boxes is not None:
62
  for box in result.boxes.xyxy:
63
  x1, y1, x2, y2 = map(int, box)
64
+ cv2.rectangle(detected_image, (x1, y1), (x2, y2), (0, 255, 0), 2) # Draw bounding box
65
  character_crops.append(image[y1:y2, x1:x2])
66
+ return detected_image, character_crops
 
 
 
 
 
 
 
67
 
68
  # Function to recognize characters
69
  def recognize_characters(character_crops):
 
92
  # Load image
93
  image = Image.open(uploaded_file)
94
 
95
+ # Detect license plates
96
  with st.spinner("Processing image..."):
97
+ detected_image, original_image = detect_license_plate(image)
98
 
99
+ # Show the detected license plate image with bounding boxes
100
  st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB), caption="Detected License Plates", use_container_width=True)
101
 
102
+ # Detect characters on the original image
103
+ character_detected_image, character_crops = detect_characters(original_image)
104
+
105
+ # Show the detected characters with bounding boxes
106
+ st.image(cv2.cvtColor(character_detected_image, cv2.COLOR_BGR2RGB), caption="Detected Characters", use_container_width=True)
107
 
108
+ # Recognize characters from the detected character crops
109
+ if character_crops:
110
+ recognized_characters = recognize_characters(character_crops)
111
  st.write("Recognized Characters:", "".join(recognized_characters))
112
  else:
113
  st.write("No characters detected in the image.")