Spaces:
Sleeping
Sleeping
| 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() |