krishnamishra8848 commited on
Commit
31fec7c
·
verified ·
1 Parent(s): 691ebd8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -54
app.py CHANGED
@@ -3,32 +3,23 @@ import numpy as np
3
  from tensorflow.keras.models import load_model
4
  from PIL import Image
5
  import requests
6
- from ultralytics import YOLO
7
- import cv2
8
 
9
- # Cache the character recognition model
10
  @st.cache_resource
11
- def load_character_model():
 
12
  url = "https://huggingface.co/krishnamishra8848/Devanagari_Character_Recognition/resolve/main/saved_model.keras"
13
  response = requests.get(url)
14
  with open("saved_model.keras", "wb") as f:
15
  f.write(response.content)
16
- return load_model("saved_model.keras")
 
 
17
 
18
- # Cache the YOLO detection model
19
- @st.cache_resource
20
- def load_detection_model():
21
- weights_path = "https://huggingface.co/krishnamishra8848/Nepal-Vehicle-License-Plate-Detection/resolve/main/last.pt"
22
- response = requests.get(weights_path)
23
- with open("last.pt", "wb") as f:
24
- f.write(response.content)
25
- return YOLO("last.pt")
26
 
27
- # Load models
28
- character_model = load_character_model()
29
- detection_model = load_detection_model()
30
-
31
- # Nepali character mapping
32
  label_mapping = [
33
  "क", "ख", "ग", "घ", "ङ", "च", "छ", "ज", "झ", "ञ",
34
  "ट", "ठ", "ड", "ढ", "ण", "त", "थ", "द", "ध", "न",
@@ -38,48 +29,27 @@ label_mapping = [
38
  ]
39
 
40
  # Streamlit App
41
- st.title("Bounding Box Text Recognition")
42
- st.write("Upload an image containing Devanagari text, and the model will detect bounding boxes and predict text.")
43
 
44
- # File uploader
45
  uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
46
 
47
  if uploaded_file is not None:
48
  try:
49
- # Load and preprocess the image
50
- img = Image.open(uploaded_file).convert("RGB")
51
- img_array = np.array(img)
52
- img_bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR) # Convert to OpenCV format
53
-
54
- # Detect bounding boxes with YOLO
55
- results = detection_model(img_bgr)
56
-
57
- # Initialize recognized text
58
- recognized_text = ""
59
-
60
- # Iterate through detected bounding boxes
61
- for result in results:
62
- if hasattr(result, 'boxes') and result.boxes is not None:
63
- for box in result.boxes.xyxy:
64
- x1, y1, x2, y2 = map(int, box) # Extract bounding box coordinates
65
- cropped_img = img_bgr[y1:y2, x1:x2] # Crop the detected region
66
-
67
- # Preprocess the cropped image
68
- cropped_resized = cv2.resize(cropped_img, (32, 32), interpolation=cv2.INTER_AREA)
69
- cropped_gray = cv2.cvtColor(cropped_resized, cv2.COLOR_BGR2GRAY)
70
- cropped_normalized = cropped_gray.astype("float32") / 255.0
71
- cropped_input = cropped_normalized.reshape(1, 32, 32, 1)
72
-
73
- # Predict text for the cropped region
74
- prediction = character_model.predict(cropped_input)
75
- predicted_index = np.argmax(prediction)
76
- predicted_character = label_mapping[predicted_index]
77
 
78
- # Append to the recognized text
79
- recognized_text += predicted_character
 
 
80
 
81
- # Display the recognized text
82
- st.success(f"Recognized Text: {recognized_text}")
83
 
84
  except Exception as e:
85
- st.error(f"An error occurred: {e}")
 
3
  from tensorflow.keras.models import load_model
4
  from PIL import Image
5
  import requests
 
 
6
 
7
+ # Cache the model with st.cache_resource
8
  @st.cache_resource
9
+ def load_model_from_hf():
10
+ # Download the model from Hugging Face
11
  url = "https://huggingface.co/krishnamishra8848/Devanagari_Character_Recognition/resolve/main/saved_model.keras"
12
  response = requests.get(url)
13
  with open("saved_model.keras", "wb") as f:
14
  f.write(response.content)
15
+ # Load the model
16
+ model = load_model("saved_model.keras")
17
+ return model
18
 
19
+ # Load the model
20
+ model = load_model_from_hf()
 
 
 
 
 
 
21
 
22
+ # Nepali characters mapping
 
 
 
 
23
  label_mapping = [
24
  "क", "ख", "ग", "घ", "ङ", "च", "छ", "ज", "झ", "ञ",
25
  "ट", "ठ", "ड", "ढ", "ण", "त", "थ", "द", "ध", "न",
 
29
  ]
30
 
31
  # Streamlit App
32
+ st.title("Devanagari Character Recognition")
33
+ st.write("Upload an image of a Devanagari character or digit, and the model will predict it.")
34
 
35
+ # File uploader for user to upload images
36
  uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
37
 
38
  if uploaded_file is not None:
39
  try:
40
+ # Preprocess the image
41
+ img = Image.open(uploaded_file).convert("L") # Convert to grayscale
42
+ img_resized = img.resize((32, 32)) # Resize to 32x32
43
+ img_array = np.array(img_resized).astype("float32") / 255.0 # Normalize pixel values
44
+ img_input = img_array.reshape(1, 32, 32, 1) # Reshape for the model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
+ # Make prediction
47
+ prediction = model.predict(img_input)
48
+ predicted_class_index = np.argmax(prediction)
49
+ predicted_character = label_mapping[predicted_class_index]
50
 
51
+ # Display the predicted character
52
+ st.success(f"Predicted Character: {predicted_character}")
53
 
54
  except Exception as e:
55
+ st.error(f"An error occurred: {e}")