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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -11
app.py CHANGED
@@ -44,13 +44,16 @@ def detect_license_plate(image):
44
  results = full_plate_model(img_bgr)
45
 
46
  detected_image = img_bgr.copy()
 
47
  for result in results:
48
  if hasattr(result, 'boxes') and result.boxes is not None:
49
  for box in result.boxes.xyxy:
50
  x1, y1, x2, y2 = map(int, box)
51
  cv2.rectangle(detected_image, (x1, y1), (x2, y2), (255, 0, 0), 2) # Draw bounding box
 
 
52
 
53
- return detected_image, img_bgr
54
 
55
  # Function to detect characters
56
  def detect_characters(image):
@@ -94,22 +97,44 @@ if uploaded_file is not None:
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.")
114
 
115
  st.success("Processing complete!")
 
44
  results = full_plate_model(img_bgr)
45
 
46
  detected_image = img_bgr.copy()
47
+ cropped_plates = []
48
  for result in results:
49
  if hasattr(result, 'boxes') and result.boxes is not None:
50
  for box in result.boxes.xyxy:
51
  x1, y1, x2, y2 = map(int, box)
52
  cv2.rectangle(detected_image, (x1, y1), (x2, y2), (255, 0, 0), 2) # Draw bounding box
53
+ cropped_plate = img_bgr[y1:y2, x1:x2]
54
+ cropped_plates.append(cropped_plate)
55
 
56
+ return detected_image, img_bgr, cropped_plates
57
 
58
  # Function to detect characters
59
  def detect_characters(image):
 
97
 
98
  # Detect license plates
99
  with st.spinner("Processing image..."):
100
+ detected_image, original_image, cropped_plates = detect_license_plate(image)
101
 
102
  # Show the detected license plate image with bounding boxes
103
  st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB), caption="Detected License Plates", use_container_width=True)
104
 
105
+ # Initialize variables for comparison
106
+ max_characters = []
107
+ best_recognition_method = ""
108
 
109
+ # Process original image for character detection
110
+ char_detected_img, character_crops_original = detect_characters(original_image)
111
 
112
+ # Recognize characters from original image
113
+ recognized_original = recognize_characters(character_crops_original)
114
+
115
+ if recognized_original:
116
+ if len(recognized_original) > len(max_characters):
117
+ max_characters = recognized_original
118
+ best_recognition_method = "Original Image"
119
+
120
+ # Process cropped plates for character detection
121
+ for idx, cropped_plate in enumerate(cropped_plates, 1):
122
+ st.write(f"Processing Cropped Plate {idx}:")
123
+ cropped_char_detected_img, character_crops_cropped = detect_characters(cropped_plate)
124
+ recognized_cropped = recognize_characters(character_crops_cropped)
125
+
126
+ if recognized_cropped:
127
+ if len(recognized_cropped) > len(max_characters):
128
+ max_characters = recognized_cropped
129
+ best_recognition_method = f"Cropped Plate {idx}"
130
+
131
+ # Show the result
132
+ st.image(cv2.cvtColor(char_detected_img, cv2.COLOR_BGR2RGB), caption="Detected Characters from Original Image", use_container_width=True)
133
+
134
+ if best_recognition_method:
135
+ st.write(f"Best Recognition Method: {best_recognition_method}")
136
+ st.write("Recognized Characters:", "".join(max_characters))
137
  else:
138
+ st.write("No characters detected in any method.")
139
 
140
  st.success("Processing complete!")