File size: 2,573 Bytes
733b6a1
 
 
 
 
 
 
0ec591c
733b6a1
 
 
 
0ec591c
733b6a1
 
 
 
 
 
0ec591c
733b6a1
 
 
 
 
 
 
 
 
361e064
 
 
 
733b6a1
 
 
 
 
0ec591c
 
 
 
 
 
 
 
 
 
733b6a1
0ec591c
 
 
 
 
 
733b6a1
0ec591c
 
 
733b6a1
0ec591c
 
 
 
733b6a1
0ec591c
733b6a1
0ec591c
 
 
 
 
733b6a1
 
0ec591c
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
70
71
72
73
74
75
import streamlit as st
from PIL import Image
from transformers import TrOCRProcessor, VisionEncoderDecoderModel, VitsModel, AutoTokenizer
import torch
import yolov5

# Load YOLOv5 model
# @st.cache(allow_output_mutation=True)
def load_model():
    return yolov5.load('keremberke/yolov5m-license-plate')

# Load TR-OCR model
# @st.cache(allow_output_mutation=True)
def load_ocr_model():
    processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
    model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
    return processor, model

# Load TTS model
# @st.cache(allow_output_mutation=True)
def load_tts_model():
    model = VitsModel.from_pretrained("facebook/mms-tts-eng")
    tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-eng")
    return model, tokenizer

# Main function for Streamlit app
def main():
    st.title("License Plate Recognition App")

    # Static test image
    test_image_path = "test_image.jpg"
    test_image = Image.open(test_image_path)

    # Upload file
    uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
    
    if uploaded_file is not None:
        img = Image.open(uploaded_file)
    else:
        img = test_image

    st.image(img, caption='Image', use_column_width=True)

    if st.button("Run Inference"):
        # Load models on startup
        model = load_model()
        processor, ocr_model = load_ocr_model()
        tts_model, tokenizer = load_tts_model()

        results = model(img, size=640)
        # results.show()
        predictions = results.pred[0]
        boxes = predictions[:, :4] # x1, y1, x2, y2
        scores = predictions[:, 4]
        categories = predictions[:, 5]

        # Crop the image of the license plate
        cropped_image = img.crop(tuple(results.xyxy[0][0, :4].squeeze().tolist()[:4]))
        st.image(cropped_image, caption='Plate detected')

        # Extract text from the image
        pixel_values = processor(cropped_image, return_tensors="pt").pixel_values
        generated_ids = ocr_model.generate(pixel_values)
        generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]

        st.write("Detected License Plate Text:", generated_text)

        # Convert the text to audio
        inputs = tokenizer(generated_text, return_tensors="pt")
        with torch.no_grad():
            output = tts_model(**inputs).waveform
        st.audio(output.numpy(), format="audio/wav", sample_rate=tts_model.config.sampling_rate)

if __name__ == "__main__":
    main()