-
Notifications
You must be signed in to change notification settings - Fork 1
/
ocr_app.py
69 lines (52 loc) · 2.47 KB
/
ocr_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import streamlit as st
from PIL import Image
from utils import crop_LowerPart_Plate, detect_text_easyocr, detect_text_yolo, process_video_with_plate_detection
yolo_model = 'models/yolo11m_car_plate_trained.pt'
ocr_yolo_model = 'models/yolo11m_car_plate_ocr.pt'
# Streamlit app interface
st.title("Car Plate Detection and OCR")
# Upload an image
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png"])
if uploaded_file is not None:
# Save uploaded image temporarily for processing
image_path = f"/tmp/{uploaded_file.name}"
image = Image.open(uploaded_file)
image.save(image_path)
# Display the uploaded image
st.image(image, caption='Uploaded Image', use_column_width=True)
# Crop the plate from the image
st.write("Cropping the car plate...")
cropped_image = crop_LowerPart_Plate(yolo_model, image_path)
if cropped_image:
st.image(cropped_image, caption="Cropped Car Plate", use_column_width=True)
# Perform OCR on the cropped image
st.spinner("Performing OCR on the cropped image...")
easyocr_image, easyocr_text = detect_text_easyocr(cropped_image)
yolo_results, nums, chars = detect_text_yolo(ocr_yolo_model, cropped_image)
st.subheader("YOLO OCR")
st.image(yolo_results, caption="YOLO OCR", use_column_width=True)
st.write(f"Detected Numbers: {nums} and detected Chars: {chars}")
st.subheader("Easy OCR")
st.spinner("Performing Easy OCR on the image...")
# Display OCR results
st.image(easyocr_image, caption="EasyOCR", use_column_width=True)
if easyocr_text:
st.write("Detected Text:")
for text, confidence in easyocr_text:
st.write(f"Text: {text}, Confidence: {confidence}")
else:
st.write("No text detected.")
else:
st.write("No car plate detected in the image.")
# Streamlit application for video uploading and processing
st.title("Car Plate Detection from Video")
# Upload video file
uploaded_video = st.file_uploader("Upload a video", type=["mp4", "avi", "mov"])
# Process the video if a file is uploaded
if uploaded_video is not None:
st.video(uploaded_video)
with st.spinner("Processing video..."):
# Process the video and get the path to the saved output file
processed_video_path = process_video_with_plate_detection(uploaded_video, yolo_model, ocr_yolo_model)
# Display the processed video
st.video(processed_video_path)