Download classifier.py from HimanshuA/Project: direct link, hf CLI and curl.
- Browser
- Download file 2.93 kB
-
https://huggingface.co/spaces/HimanshuA/Project/resolve/main/classifier.py
- Command line
-
hf download hf://spaces/HimanshuA/Project/classifier.py
-
curl -L -o classifier.py https://huggingface.co/spaces/HimanshuA/Project/resolve/main/classifier.py
2.93 kB
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import cv2 | |
| import tensorflow as tf | |
| from sklearn.metrics import f1_score | |
| from tensorflow.keras.models import load_model | |
| from tensorflow.keras import optimizers | |
| from tensorflow.keras.models import Sequential | |
| from tensorflow.keras.preprocessing.image import ImageDataGenerator | |
| from tensorflow.keras.layers import Dense, Flatten, MaxPooling2D, Dropout, Conv2D | |
| import streamlit as st | |
| # Loads the data required for detecting the license plates from cascade classifier. | |
| plate_cascade = cv2.CascadeClassifier('indian_license_plate.xml') | |
| # add the path to 'india_license_plate.xml' file. | |
| model = load_model('licence_trained_model.h5', compile=False) | |
| def detect_plate(img, text=''): # the function detects and perfors blurring on the number plate. | |
| plate_img = img.copy() | |
| roi = img.copy() | |
| plate_rect = plate_cascade.detectMultiScale(plate_img, scaleFactor = 1.2, minNeighbors = 7) # detects numberplates and returns the coordinates and dimensions of detected license plate's contours. | |
| for (x,y,w,h) in plate_rect: | |
| roi_ = roi[y:y+h, x:x+w, :] # extracting the Region of Interest of license plate for blurring. | |
| plate = roi[y:y+h, x:x+w, :] | |
| cv2.rectangle(plate_img, (x+2,y), (x+w-3, y+h-5), (51,181,155), 3) # finally representing the detected contours by drawing rectangles around the edges. | |
| if text!='': | |
| plate_img = cv2.putText(plate_img, text, (x-w//2,y-h//2), | |
| cv2.FONT_HERSHEY_COMPLEX_SMALL , 0.5, (51,181,155), 1, cv2.LINE_AA) | |
| return plate_img, plate # returning the processed image. | |
| # Testing the above function | |
| def display(img, title=''): | |
| img = cv2.cvtColor(img_, cv2.COLOR_BGR2RGB) | |
| fig = plt.figure(figsize=(10,6)) | |
| ax = plt.subplot(111) | |
| ax.imshow(img) | |
| plt.axis('off') | |
| plt.title(title) | |
| plt.show() | |
| display(img,'input image') | |
| ''' | |
| def segment_characters(image) : | |
| # Preprocess cropped license plate image | |
| img_lp = cv2.resize(image, (333, 75)) | |
| img_gray_lp = cv2.cvtColor(img_lp, cv2.COLOR_BGR2GRAY) | |
| _, img_binary_lp = cv2.threshold(img_gray_lp, 200, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) | |
| img_binary_lp = cv2.erode(img_binary_lp, (3,3)) | |
| img_binary_lp = cv2.dilate(img_binary_lp, (3,3)) | |
| LP_WIDTH = img_binary_lp.shape[0] | |
| LP_HEIGHT = img_binary_lp.shape[1] | |
| # Make borders white | |
| img_binary_lp | |
| ''' | |
| # Streamlit app | |
| def run(): | |
| st.write('##### Traffic Sign Classifier') | |
| # Making Form | |
| # Create a Streamlit form | |
| with st.form(key='vehicle number plate'): | |
| # Add a file uploader to the form | |
| #img= st.file_uploader("Upload a file of one of these format .JPEG/.JPG/.PNG file", accept_multiple_files=True) | |
| img= st.file_uploader("Upload a file of one of these format .JPEG/.JPG/.PNG file", accept_multiple_files=True) | |
| if __name__ == '__main__': | |
| run() | |