File size: 2,514 Bytes
3c2f163
 
655fdba
6815466
3c2f163
 
6815466
3c2f163
d11be0b
6ae34d7
 
197e2b4
 
 
 
 
 
 
655fdba
 
 
6ae34d7
457e283
 
 
d11be0b
457e283
d11be0b
457e283
 
 
d11be0b
655fdba
 
 
d11be0b
655fdba
 
 
 
d11be0b
457e283
 
 
 
 
655fdba
 
457e283
655fdba
f4c386c
 
d11be0b
f4c386c
 
d11be0b
6815466
6046ae7
 
6815466
 
 
d11be0b
655fdba
 
 
fcaf1cf
bd2ade6
4476613
4fe620d
eb8d222
52e95bc
eb8d222
bd2ade6
 
 
 
d11be0b
457e283
fcaf1cf
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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

#
# Create a directory for storing data
if not os.path.exists("data"):
    os.makedirs("data")



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 = r"D:\number_plate\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'])

   # Check if the CSV file exists
    file_path = os.path.join("data", "data.csv")
    if os.path.exists(file_path):
        df.to_csv(file_path, mode='a', header=False, index=False)  # Append to existing file
    else:
        df.to_csv(file_path, index=False)  # Create new file
    
    # Print some debug information
    st.write("CSV File Path:", file_path)
    st.write("CSV File Exists:", os.path.exists(file_path))

    # Print recognized text
    st.write("Recognized Text:",text)

if bytes_data is None:
    st.stop()