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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -30
app.py CHANGED
@@ -38,25 +38,23 @@ def load_models():
38
  # Load models
39
  full_plate_model, character_model, recognition_model = load_models()
40
 
41
- # Function to detect and crop license plates
42
- def detect_and_crop_license_plate(image):
43
  img_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
44
  results = full_plate_model(img_bgr)
45
 
46
  detected_image = img_bgr.copy()
47
- cropped_images = []
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_image = img_bgr[y1:y2, x1:x2]
54
- cropped_images.append(cropped_image)
55
 
56
- return cropped_images, detected_image
57
 
58
- # Function to detect and crop characters
59
- def detect_and_crop_characters(image):
 
60
  results = character_model(image)
61
  character_crops = []
62
  for result in results:
@@ -64,7 +62,14 @@ def detect_and_crop_characters(image):
64
  for box in result.boxes.xyxy:
65
  x1, y1, x2, y2 = map(int, box)
66
  character_crops.append(image[y1:y2, x1:x2])
67
- return character_crops
 
 
 
 
 
 
 
68
 
69
  # Function to recognize characters
70
  def recognize_characters(character_crops):
@@ -93,31 +98,19 @@ if uploaded_file is not None:
93
  # Load image
94
  image = Image.open(uploaded_file)
95
 
96
- # Detect license plates
97
  with st.spinner("Processing image..."):
98
- cropped_plates, detected_image = detect_and_crop_license_plate(image)
99
 
100
- if cropped_plates:
101
- st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB), caption="Detected License Plates", use_container_width=True)
102
- st.write(f"Detected {len(cropped_plates)} license plate(s).")
103
 
104
- for idx, cropped_plate in enumerate(cropped_plates, 1):
105
- st.write(f"Processing License Plate {idx}:")
106
- character_crops = detect_and_crop_characters(cropped_plate)
107
 
108
- if character_crops:
109
- recognized_characters = recognize_characters(character_crops)
110
- st.write("Recognized Characters:", "".join(recognized_characters))
111
- else:
112
- st.write("No characters detected in this license plate.")
113
  else:
114
- st.write("No license plates detected. Running character detection on the full image.")
115
- character_crops = detect_and_crop_characters(cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR))
116
-
117
- if character_crops:
118
- recognized_characters = recognize_characters(character_crops)
119
- st.write("Recognized Characters:", "".join(recognized_characters))
120
- else:
121
- st.write("No characters detected in the full image.")
122
 
123
  st.success("Processing complete!")
 
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
 
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 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:
 
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
  # 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.")
 
 
 
 
 
 
 
115
 
116
  st.success("Processing complete!")