File size: 1,958 Bytes
b7cecb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import matplotlib as plt
import cv2
import numpy as np
from PIL import Image #Image Processing
harcascade = "haarcascade_russian_plate_number.xml"
min_area = 500
count = 0
import easyocr


st.image("anpr.jpg",use_column_width="always")
st.markdown("<h1 style='text-align: center; color: green;'>SnapText 😎</h1>", unsafe_allow_html=True)

# st.title(':blue[SnapText]')
st.subheader('Welcome to :blue[SnapText ] !! ')
st.subheader("Upload license plate photos of vehicles, and let us extract and rewrite the text for you. ")
st.divider()
uploaded_file = st.file_uploader("Upload your file here...", type=['jpg'])
# path="plate_0.jpg"
# img = cv.imread(uploaded_file)
if uploaded_file is not None:
    with st.spinner("Extracting Text and License PLate"):
        file_bytes = np.asarray(bytearray(uploaded_file.read()),dtype=np.uint8)
        img=cv2.imdecode(file_bytes,1)
        st.image(img,caption="Uploaded Raw Image")
        img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        plate_cascade = cv2.CascadeClassifier(harcascade)
        plates = plate_cascade.detectMultiScale(img_gray, 1.1, 4)
        for (x, y, w, h) in plates:
            area = w * h

            if area > min_area:
                cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
                cv2.putText(img, "Number Plate", (x, y - 5), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 0, 255), 2)

                img_roi = img[y: y + h, x: x + w]

                # Save the cropped and annotated image
                cv2.imwrite("plate_0.jpg", img_roi)
                
                count += 1
                image = Image.open('plate_0.jpg')
                st.image(image, caption='Extracted License Plate')
                reader = easyocr.Reader(['en'])
                img = cv2.imread('plate_0.jpg')
                result = reader.readtext(img)
                st.success("Extracted Text: "+ result[0][-2].upper())