Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,29 +14,29 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
|
|
| 14 |
|
| 15 |
# Configure Tesseract path (ensure it’s correctly set to your Tesseract installation)
|
| 16 |
try:
|
| 17 |
-
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' #
|
| 18 |
-
pytesseract.get_tesseract_version() #
|
| 19 |
-
logging.info("Tesseract is configured
|
| 20 |
except Exception as e:
|
| 21 |
logging.error(f"Tesseract not found or misconfigured: {str(e)}")
|
| 22 |
|
| 23 |
-
# Improved Image Preprocessing function
|
| 24 |
def preprocess_image(img_cv):
|
| 25 |
"""Enhance the image to improve OCR performance."""
|
| 26 |
try:
|
| 27 |
# Convert to grayscale
|
| 28 |
gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
|
| 29 |
-
|
| 30 |
-
# Increase contrast
|
| 31 |
contrast = cv2.equalizeHist(gray)
|
| 32 |
|
| 33 |
# Apply Gaussian blur to reduce noise
|
| 34 |
blurred = cv2.GaussianBlur(contrast, (5, 5), 0)
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
|
| 38 |
|
| 39 |
-
# Sharpening the image
|
| 40 |
sharpened = cv2.filter2D(thresh, -1, np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]))
|
| 41 |
|
| 42 |
return sharpened
|
|
@@ -44,7 +44,7 @@ def preprocess_image(img_cv):
|
|
| 44 |
logging.error(f"Image preprocessing failed: {str(e)}")
|
| 45 |
return img_cv
|
| 46 |
|
| 47 |
-
# Function to extract weight using OCR
|
| 48 |
def extract_weight(img):
|
| 49 |
"""Extract weight using Tesseract OCR, focusing on digits and decimals."""
|
| 50 |
try:
|
|
@@ -52,26 +52,31 @@ def extract_weight(img):
|
|
| 52 |
logging.error("No image provided for OCR")
|
| 53 |
return "Not detected", 0.0, None
|
| 54 |
|
|
|
|
| 55 |
img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
|
|
|
|
|
|
| 56 |
processed_img = preprocess_image(img_cv)
|
| 57 |
|
| 58 |
-
# Show processed image
|
| 59 |
debug_img = Image.fromarray(processed_img)
|
| 60 |
-
debug_img.show()
|
| 61 |
|
| 62 |
-
# Tesseract
|
| 63 |
custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.'
|
| 64 |
-
|
|
|
|
| 65 |
text = pytesseract.image_to_string(processed_img, config=custom_config)
|
| 66 |
logging.info(f"OCR result: '{text}'")
|
| 67 |
|
|
|
|
| 68 |
weight = ''.join(filter(lambda x: x in '0123456789.', text.strip()))
|
| 69 |
|
| 70 |
if weight:
|
| 71 |
try:
|
| 72 |
weight_float = float(weight)
|
| 73 |
if weight_float >= 0:
|
| 74 |
-
confidence = 95.0 #
|
| 75 |
logging.info(f"Weight detected: {weight} (Confidence: {confidence:.2f}%)")
|
| 76 |
return weight, confidence, processed_img
|
| 77 |
except ValueError:
|
|
@@ -85,28 +90,29 @@ def extract_weight(img):
|
|
| 85 |
|
| 86 |
# Main function to process uploaded image and display results
|
| 87 |
def process_image(img):
|
| 88 |
-
"""Process the uploaded image, extract weight, and
|
| 89 |
if img is None:
|
| 90 |
logging.error("No image uploaded")
|
| 91 |
return "No image uploaded", None, gr.update(visible=False), gr.update(visible=False)
|
| 92 |
|
| 93 |
-
# Get
|
| 94 |
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
|
| 95 |
|
|
|
|
| 96 |
weight, confidence, processed_img = extract_weight(img)
|
| 97 |
|
| 98 |
-
# If
|
| 99 |
if weight == "Not detected" or confidence < 95.0:
|
| 100 |
logging.warning(f"Weight detection failed: {weight} (Confidence: {confidence:.2f}%)")
|
| 101 |
return f"{weight} (Confidence: {confidence:.2f}%)", ist_time, gr.update(visible=True), gr.update(visible=False)
|
| 102 |
|
| 103 |
-
# Convert processed image to base64 for displaying
|
| 104 |
pil_image = Image.fromarray(processed_img)
|
| 105 |
buffered = io.BytesIO()
|
| 106 |
pil_image.save(buffered, format="PNG")
|
| 107 |
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
| 108 |
|
| 109 |
-
# Return the detected weight and
|
| 110 |
return f"{weight} kg (Confidence: {confidence:.2f}%)", ist_time, img_base64, gr.update(visible=True)
|
| 111 |
|
| 112 |
# Gradio Interface Setup for Hugging Face
|
|
|
|
| 14 |
|
| 15 |
# Configure Tesseract path (ensure it’s correctly set to your Tesseract installation)
|
| 16 |
try:
|
| 17 |
+
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' # Adjust path if necessary
|
| 18 |
+
pytesseract.get_tesseract_version() # Test Tesseract installation
|
| 19 |
+
logging.info("Tesseract is properly configured.")
|
| 20 |
except Exception as e:
|
| 21 |
logging.error(f"Tesseract not found or misconfigured: {str(e)}")
|
| 22 |
|
| 23 |
+
# Improved Image Preprocessing function for OCR
|
| 24 |
def preprocess_image(img_cv):
|
| 25 |
"""Enhance the image to improve OCR performance."""
|
| 26 |
try:
|
| 27 |
# Convert to grayscale
|
| 28 |
gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
|
| 29 |
+
|
| 30 |
+
# Increase contrast using histogram equalization
|
| 31 |
contrast = cv2.equalizeHist(gray)
|
| 32 |
|
| 33 |
# Apply Gaussian blur to reduce noise
|
| 34 |
blurred = cv2.GaussianBlur(contrast, (5, 5), 0)
|
| 35 |
+
|
| 36 |
+
# Apply adaptive thresholding to binarize the image
|
| 37 |
thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
|
| 38 |
|
| 39 |
+
# Sharpening the image to bring out more details
|
| 40 |
sharpened = cv2.filter2D(thresh, -1, np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]))
|
| 41 |
|
| 42 |
return sharpened
|
|
|
|
| 44 |
logging.error(f"Image preprocessing failed: {str(e)}")
|
| 45 |
return img_cv
|
| 46 |
|
| 47 |
+
# Function to extract weight from image using OCR
|
| 48 |
def extract_weight(img):
|
| 49 |
"""Extract weight using Tesseract OCR, focusing on digits and decimals."""
|
| 50 |
try:
|
|
|
|
| 52 |
logging.error("No image provided for OCR")
|
| 53 |
return "Not detected", 0.0, None
|
| 54 |
|
| 55 |
+
# Convert the PIL image to OpenCV format
|
| 56 |
img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
| 57 |
+
|
| 58 |
+
# Preprocess the image for better OCR results
|
| 59 |
processed_img = preprocess_image(img_cv)
|
| 60 |
|
| 61 |
+
# Debug: Show the processed image to verify preprocessing
|
| 62 |
debug_img = Image.fromarray(processed_img)
|
| 63 |
+
debug_img.show() # This will open the processed image for debugging purposes
|
| 64 |
|
| 65 |
+
# Configure Tesseract to detect only digits and decimals
|
| 66 |
custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.'
|
| 67 |
+
|
| 68 |
+
# Use Tesseract OCR to extract text
|
| 69 |
text = pytesseract.image_to_string(processed_img, config=custom_config)
|
| 70 |
logging.info(f"OCR result: '{text}'")
|
| 71 |
|
| 72 |
+
# Extract the weight (numbers and decimal)
|
| 73 |
weight = ''.join(filter(lambda x: x in '0123456789.', text.strip()))
|
| 74 |
|
| 75 |
if weight:
|
| 76 |
try:
|
| 77 |
weight_float = float(weight)
|
| 78 |
if weight_float >= 0:
|
| 79 |
+
confidence = 95.0 # Assume high confidence if we detect a valid weight
|
| 80 |
logging.info(f"Weight detected: {weight} (Confidence: {confidence:.2f}%)")
|
| 81 |
return weight, confidence, processed_img
|
| 82 |
except ValueError:
|
|
|
|
| 90 |
|
| 91 |
# Main function to process uploaded image and display results
|
| 92 |
def process_image(img):
|
| 93 |
+
"""Process the uploaded image, extract weight, and return results."""
|
| 94 |
if img is None:
|
| 95 |
logging.error("No image uploaded")
|
| 96 |
return "No image uploaded", None, gr.update(visible=False), gr.update(visible=False)
|
| 97 |
|
| 98 |
+
# Get timestamp for IST (Indian Standard Time)
|
| 99 |
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
|
| 100 |
|
| 101 |
+
# Call the function to extract weight and confidence
|
| 102 |
weight, confidence, processed_img = extract_weight(img)
|
| 103 |
|
| 104 |
+
# If OCR fails to detect weight
|
| 105 |
if weight == "Not detected" or confidence < 95.0:
|
| 106 |
logging.warning(f"Weight detection failed: {weight} (Confidence: {confidence:.2f}%)")
|
| 107 |
return f"{weight} (Confidence: {confidence:.2f}%)", ist_time, gr.update(visible=True), gr.update(visible=False)
|
| 108 |
|
| 109 |
+
# Convert the processed image to base64 format for displaying
|
| 110 |
pil_image = Image.fromarray(processed_img)
|
| 111 |
buffered = io.BytesIO()
|
| 112 |
pil_image.save(buffered, format="PNG")
|
| 113 |
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
| 114 |
|
| 115 |
+
# Return the detected weight, timestamp, and base64 image for Gradio
|
| 116 |
return f"{weight} kg (Confidence: {confidence:.2f}%)", ist_time, img_base64, gr.update(visible=True)
|
| 117 |
|
| 118 |
# Gradio Interface Setup for Hugging Face
|