arsath-sm commited on
Commit
6907110
1 Parent(s): ab634f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -27
app.py CHANGED
@@ -12,26 +12,20 @@ def load_model():
12
 
13
  ort_session = load_model()
14
 
 
 
 
15
  def preprocess_image(image, target_size=(640, 640)):
16
- # Convert PIL Image to numpy array if necessary
17
  if isinstance(image, Image.Image):
18
  image = np.array(image)
19
-
20
- # Convert RGB to BGR
21
  image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
22
-
23
- # Resize image
24
  image = cv2.resize(image, target_size)
25
- # Normalize
26
  image = image.astype(np.float32) / 255.0
27
- # Transpose for ONNX input
28
  image = np.transpose(image, (2, 0, 1))
29
- # Add batch dimension
30
  image = np.expand_dims(image, axis=0)
31
  return image
32
 
33
  def postprocess_results(output, image_shape, confidence_threshold=0.25, iou_threshold=0.45):
34
- # Handle different possible output formats
35
  if isinstance(output, (list, tuple)):
36
  predictions = output[0]
37
  elif isinstance(output, np.ndarray):
@@ -39,31 +33,24 @@ def postprocess_results(output, image_shape, confidence_threshold=0.25, iou_thre
39
  else:
40
  raise ValueError(f"Unexpected output type: {type(output)}")
41
 
42
- # Reshape if necessary
43
  if len(predictions.shape) == 4:
44
  predictions = predictions.squeeze((0, 1))
45
  elif len(predictions.shape) == 3:
46
  predictions = predictions.squeeze(0)
47
 
48
- # Extract boxes, scores, and class_ids
49
  boxes = predictions[:, :4]
50
  scores = predictions[:, 4]
51
  class_ids = predictions[:, 5]
52
 
53
- # Filter by confidence
54
  mask = scores > confidence_threshold
55
  boxes = boxes[mask]
56
  scores = scores[mask]
57
  class_ids = class_ids[mask]
58
 
59
- # Convert boxes from [x, y, w, h] to [x1, y1, x2, y2]
60
  boxes[:, 2:] += boxes[:, :2]
61
-
62
- # Scale boxes to image size
63
  boxes[:, [0, 2]] *= image_shape[1]
64
  boxes[:, [1, 3]] *= image_shape[0]
65
 
66
- # Apply NMS
67
  indices = cv2.dnn.NMSBoxes(boxes.tolist(), scores.tolist(), confidence_threshold, iou_threshold)
68
 
69
  results = []
@@ -80,29 +67,26 @@ def process_image(image):
80
  orig_image = image.copy()
81
  processed_image = preprocess_image(image)
82
 
83
- # Run inference
84
  inputs = {ort_session.get_inputs()[0].name: processed_image}
85
  outputs = ort_session.run(None, inputs)
86
 
87
  results = postprocess_results(outputs, image.shape)
88
 
89
- # Draw bounding boxes on the image
90
  for x1, y1, x2, y2, score, class_id in results:
91
- cv2.rectangle(orig_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
92
- label = f"License Plate: {score:.2f}"
93
- cv2.putText(orig_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
 
94
 
95
  return cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
96
 
97
  def process_video(video_path):
98
  cap = cv2.VideoCapture(video_path)
99
 
100
- # Get video properties
101
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
102
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
103
  fps = int(cap.get(cv2.CAP_PROP_FPS))
104
 
105
- # Create a temporary file to store the processed video
106
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
107
  out = cv2.VideoWriter(temp_file.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
108
 
@@ -119,7 +103,7 @@ def process_video(video_path):
119
 
120
  return temp_file.name
121
 
122
- st.title("License Plate Detection")
123
 
124
  uploaded_file = st.file_uploader("Choose an image or video file", type=["jpg", "jpeg", "png", "mp4"])
125
 
@@ -130,7 +114,7 @@ if uploaded_file is not None:
130
  image = Image.open(uploaded_file)
131
  st.image(image, caption="Uploaded Image", use_column_width=True)
132
 
133
- if st.button("Detect License Plates"):
134
  processed_image = process_image(np.array(image))
135
  st.image(processed_image, caption="Processed Image", use_column_width=True)
136
 
@@ -140,8 +124,8 @@ if uploaded_file is not None:
140
 
141
  st.video(tfile.name)
142
 
143
- if st.button("Detect License Plates"):
144
  processed_video = process_video(tfile.name)
145
  st.video(processed_video)
146
 
147
- st.write("Upload an image or video to detect license plates.")
 
12
 
13
  ort_session = load_model()
14
 
15
+ # Define class names (update this based on your model's classes)
16
+ CLASS_NAMES = ['car', 'license_plate']
17
+
18
  def preprocess_image(image, target_size=(640, 640)):
 
19
  if isinstance(image, Image.Image):
20
  image = np.array(image)
 
 
21
  image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
 
 
22
  image = cv2.resize(image, target_size)
 
23
  image = image.astype(np.float32) / 255.0
 
24
  image = np.transpose(image, (2, 0, 1))
 
25
  image = np.expand_dims(image, axis=0)
26
  return image
27
 
28
  def postprocess_results(output, image_shape, confidence_threshold=0.25, iou_threshold=0.45):
 
29
  if isinstance(output, (list, tuple)):
30
  predictions = output[0]
31
  elif isinstance(output, np.ndarray):
 
33
  else:
34
  raise ValueError(f"Unexpected output type: {type(output)}")
35
 
 
36
  if len(predictions.shape) == 4:
37
  predictions = predictions.squeeze((0, 1))
38
  elif len(predictions.shape) == 3:
39
  predictions = predictions.squeeze(0)
40
 
 
41
  boxes = predictions[:, :4]
42
  scores = predictions[:, 4]
43
  class_ids = predictions[:, 5]
44
 
 
45
  mask = scores > confidence_threshold
46
  boxes = boxes[mask]
47
  scores = scores[mask]
48
  class_ids = class_ids[mask]
49
 
 
50
  boxes[:, 2:] += boxes[:, :2]
 
 
51
  boxes[:, [0, 2]] *= image_shape[1]
52
  boxes[:, [1, 3]] *= image_shape[0]
53
 
 
54
  indices = cv2.dnn.NMSBoxes(boxes.tolist(), scores.tolist(), confidence_threshold, iou_threshold)
55
 
56
  results = []
 
67
  orig_image = image.copy()
68
  processed_image = preprocess_image(image)
69
 
 
70
  inputs = {ort_session.get_inputs()[0].name: processed_image}
71
  outputs = ort_session.run(None, inputs)
72
 
73
  results = postprocess_results(outputs, image.shape)
74
 
 
75
  for x1, y1, x2, y2, score, class_id in results:
76
+ color = (0, 255, 0) if CLASS_NAMES[class_id] == 'car' else (255, 0, 0)
77
+ cv2.rectangle(orig_image, (x1, y1), (x2, y2), color, 2)
78
+ label = f"{CLASS_NAMES[class_id]}: {score:.2f}"
79
+ cv2.putText(orig_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
80
 
81
  return cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
82
 
83
  def process_video(video_path):
84
  cap = cv2.VideoCapture(video_path)
85
 
 
86
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
87
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
88
  fps = int(cap.get(cv2.CAP_PROP_FPS))
89
 
 
90
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
91
  out = cv2.VideoWriter(temp_file.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
92
 
 
103
 
104
  return temp_file.name
105
 
106
+ st.title("Vehicle and License Plate Detection")
107
 
108
  uploaded_file = st.file_uploader("Choose an image or video file", type=["jpg", "jpeg", "png", "mp4"])
109
 
 
114
  image = Image.open(uploaded_file)
115
  st.image(image, caption="Uploaded Image", use_column_width=True)
116
 
117
+ if st.button("Detect Objects"):
118
  processed_image = process_image(np.array(image))
119
  st.image(processed_image, caption="Processed Image", use_column_width=True)
120
 
 
124
 
125
  st.video(tfile.name)
126
 
127
+ if st.button("Detect Objects"):
128
  processed_video = process_video(tfile.name)
129
  st.video(processed_video)
130
 
131
+ st.write("Upload an image or video to detect vehicles and license plates.")