Vikas01's picture
Update app.py
ccf2576
raw
history blame
2.1 kB
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()