Sourudra commited on
Commit
77e4161
·
verified ·
1 Parent(s): feec7d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -23
app.py CHANGED
@@ -23,9 +23,9 @@ def load_easyocr_reader():
23
  yolo_model = load_yolo_model()
24
  ocr_reader = load_easyocr_reader()
25
 
26
- # Function to process the uploaded image
27
  def process_image(image, confidence_threshold=0.5):
28
- # Perform license plate detection using YOLO
29
  results = yolo_model(image, conf=confidence_threshold)
30
  annotated_image = cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB)
31
  st.image(annotated_image, caption="Detected License Plate(s)", use_container_width=True)
@@ -46,59 +46,83 @@ def process_image(image, confidence_threshold=0.5):
46
  text_results = ocr_reader.readtext(cropped_image_rgb, detail=0)
47
  detected_text = " ".join(text_results)
48
  st.write(f"**Extracted Text (Plate {i+1}):** {detected_text}")
 
49
 
50
  # Function to process video and detect license plates
51
- def process_video(video_path, confidence_threshold=0.5):
 
52
  cap = cv2.VideoCapture(video_path)
53
-
 
 
 
 
 
 
 
 
54
  if not cap.isOpened():
55
  st.error("Error opening video stream or file")
56
  return
57
-
58
  while cap.isOpened():
59
  ret, frame = cap.read()
60
  if not ret:
61
  break
62
-
63
  results = yolo_model(frame, conf=confidence_threshold)
64
  annotated_frame = cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB)
65
 
66
- st.image(annotated_frame, caption="Processed Video Frame", use_container_width=True)
67
-
68
  for result in results:
69
  boxes = result.boxes.xyxy.cpu().numpy().astype(int)
70
  for i, box in enumerate(boxes):
71
  x1, y1, x2, y2 = box
72
  cropped_plate = frame[y1:y2, x1:x2]
73
  cropped_rgb = cv2.cvtColor(cropped_plate, cv2.COLOR_BGR2RGB)
74
-
75
  # Perform OCR on the cropped image
76
  text_results = ocr_reader.readtext(cropped_rgb, detail=0)
77
  detected_text = " ".join(text_results)
 
 
78
  cv2.putText(annotated_frame, detected_text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
79
 
80
- st.image(annotated_frame, caption="Video Frame with OCR", use_container_width=True)
 
81
 
82
  cap.release()
 
 
 
 
 
 
 
83
 
84
  # Sidebar input for file upload
85
- file = st.file_uploader("Upload an Image or Video", type=["jpg", "jpeg", "png", "mp4", "avi", "mov"])
86
 
87
- if file is not None:
88
- # Check file type
89
- file_type = file.type
90
 
91
- # Process image
92
- if file_type in ["image/jpeg", "image/png", "image/jpg"]:
93
- image = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)
94
- process_image(image)
 
95
 
96
- # Process video
97
- elif file_type in ["video/mp4", "video/avi", "video/mov"]:
98
- video_bytes = file.read()
99
- with open("/tmp/uploaded_video.mp4", "wb") as f:
 
100
  f.write(video_bytes)
101
- process_video("/tmp/uploaded_video.mp4")
 
 
 
102
 
103
  st.markdown("---")
104
  st.info("**Note:** This application uses EasyOCR for text recognition. Results may vary depending on image/video quality and lighting conditions.")
 
23
  yolo_model = load_yolo_model()
24
  ocr_reader = load_easyocr_reader()
25
 
26
+ # Function to process image and detect license plates
27
  def process_image(image, confidence_threshold=0.5):
28
+ # Perform license plate detection
29
  results = yolo_model(image, conf=confidence_threshold)
30
  annotated_image = cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB)
31
  st.image(annotated_image, caption="Detected License Plate(s)", use_container_width=True)
 
46
  text_results = ocr_reader.readtext(cropped_image_rgb, detail=0)
47
  detected_text = " ".join(text_results)
48
  st.write(f"**Extracted Text (Plate {i+1}):** {detected_text}")
49
+ st.write(f"**Confidence Score:** {result.boxes.conf.cpu().numpy()[i]:.2f}")
50
 
51
  # Function to process video and detect license plates
52
+ def process_video(video_path, confidence_threshold=0.5, output_path="output_video.mp4"):
53
+ # Open the video file
54
  cap = cv2.VideoCapture(video_path)
55
+
56
+ # Get video frame dimensions
57
+ frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
58
+ frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
59
+
60
+ # Create VideoWriter object to save the output video
61
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v") # Codec for mp4
62
+ out = cv2.VideoWriter(output_path, fourcc, 20.0, (frame_width, frame_height)) # 20 FPS
63
+
64
  if not cap.isOpened():
65
  st.error("Error opening video stream or file")
66
  return
67
+
68
  while cap.isOpened():
69
  ret, frame = cap.read()
70
  if not ret:
71
  break
72
+
73
  results = yolo_model(frame, conf=confidence_threshold)
74
  annotated_frame = cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB)
75
 
76
+ # Loop through detections and perform OCR
 
77
  for result in results:
78
  boxes = result.boxes.xyxy.cpu().numpy().astype(int)
79
  for i, box in enumerate(boxes):
80
  x1, y1, x2, y2 = box
81
  cropped_plate = frame[y1:y2, x1:x2]
82
  cropped_rgb = cv2.cvtColor(cropped_plate, cv2.COLOR_BGR2RGB)
83
+
84
  # Perform OCR on the cropped image
85
  text_results = ocr_reader.readtext(cropped_rgb, detail=0)
86
  detected_text = " ".join(text_results)
87
+
88
+ # Optionally add detected text on the annotated frame
89
  cv2.putText(annotated_frame, detected_text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
90
 
91
+ # Write the annotated frame to the output video
92
+ out.write(cv2.cvtColor(annotated_frame, cv2.COLOR_RGB2BGR))
93
 
94
  cap.release()
95
+ out.release()
96
+
97
+ st.success(f"Video processing complete. Output video saved to {output_path}")
98
+
99
+ # Provide a download link for the processed video
100
+ with open(output_path, "rb") as f:
101
+ st.download_button(label="Download Processed Video", data=f, file_name=output_path)
102
 
103
  # Sidebar input for file upload
104
+ uploaded_file = st.file_uploader("Upload an Image or Video", type=["mp4", "avi", "mov", "jpg", "jpeg", "png"])
105
 
106
+ if uploaded_file is not None:
107
+ # Check if it's an image or video
108
+ file_type = uploaded_file.type
109
 
110
+ if file_type.startswith("image"):
111
+ # Read and process the image
112
+ image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1)
113
+ confidence_threshold = st.sidebar.slider("Confidence Threshold", 0.0, 1.0, 0.5, 0.01)
114
+ process_image(image, confidence_threshold)
115
 
116
+ elif file_type.startswith("video"):
117
+ # Save the uploaded video to a temporary file
118
+ video_bytes = uploaded_file.read()
119
+ video_path = "/tmp/uploaded_video.mp4"
120
+ with open(video_path, "wb") as f:
121
  f.write(video_bytes)
122
+
123
+ # Process the video and save the output
124
+ output_path = "/tmp/output_video.mp4"
125
+ process_video(video_path, confidence_threshold=0.5, output_path=output_path)
126
 
127
  st.markdown("---")
128
  st.info("**Note:** This application uses EasyOCR for text recognition. Results may vary depending on image/video quality and lighting conditions.")