File size: 2,104 Bytes
3c2f163
 
655fdba
6815466
3c2f163
 
6815466
3c2f163
d11be0b
6ae34d7
 
655fdba
 
 
6ae34d7
457e283
 
 
d11be0b
457e283
d11be0b
457e283
 
 
d11be0b
655fdba
 
 
d11be0b
655fdba
 
 
 
d11be0b
457e283
 
 
 
 
655fdba
 
457e283
655fdba
f4c386c
 
d11be0b
f4c386c
 
d11be0b
6815466
ccf2576
6815466
 
 
 
d11be0b
655fdba
 
 
457e283
655fdba
d11be0b
655fdba
d11be0b
457e283
 
655fdba
457e283
6815466
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 numpy as np
import cv2
import imutils
import pytesseract
import pandas as pd
import time
import os

from PIL import Image
import streamlit as st

bytes_data = None

img_file_buffer = st.camera_input("Take a picture")
if img_file_buffer is not None:
    test_image = Image.open(img_file_buffer)
    st.image(test_image, use_column_width=True)
    image = np.asarray(test_image)

    image = imutils.resize(image, width=500)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.bilateralFilter(gray, 11, 17, 17)
    edged = cv2.Canny(gray, 170, 200)

    cnts, new = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    image1 = image.copy()
    cv2.drawContours(image1, cnts, -1, (0, 255, 0), 3)

    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:30]
    screenCnt = None
    image2 = image.copy()
    cv2.drawContours(image2, cnts, -1, (0, 0, 255), 3)

    for c in cnts:
        perimeter = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.018 * perimeter, True)
        if len(approx) == 4:
            screenCnt = approx
            x, y, w, h = cv2.boundingRect(c)
            new_img = image[y:y + h, x:x + w]
            break

    image_with_contours = image.copy()
    cv2.drawContours(image_with_contours, [screenCnt], -1, (0, 255, 0), 3)

    # Display the image with contours
    st.image(image_with_contours, caption="Image with detected license plate")

    # Configuration for tesseract
    tesseract_path = os.path.abspath("tessract/tesseract.exe")
    pytesseract.pytesseract.tesseract_cmd = tesseract_path
    
    # Run tesseract OCR on the cropped image
    text = pytesseract.image_to_string(new_img, lang="eng")

    # Data is stored in CSV file
    raw_data = {'date': [time.asctime(time.localtime(time.time()))], 'v_number': [text]}
    df = pd.DataFrame(raw_data, columns=['date', 'v_number'])
    file_path = 'data.csv'
    header = not os.path.exists(file_path)

    df.to_csv('data.csv', mode='a', header=header, index=False)

    # Print recognized text
    st.write(text)

if bytes_data is None:
    st.stop()