Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -75,7 +75,7 @@ def extract_weight(img):
|
|
75 |
try:
|
76 |
if img is None:
|
77 |
logging.error("No image provided for OCR")
|
78 |
-
return "Not detected", 0.0
|
79 |
|
80 |
# Convert PIL image to OpenCV format
|
81 |
img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
@@ -99,30 +99,36 @@ def extract_weight(img):
|
|
99 |
if weight_float >= 0: # Only accept valid weights
|
100 |
confidence = 95.0 # Assume high confidence if we have a valid weight
|
101 |
logging.info(f"Weight detected: {weight} (Confidence: {confidence:.2f}%)")
|
102 |
-
return weight, confidence
|
103 |
except ValueError:
|
104 |
logging.warning(f"Invalid number format: {weight}")
|
105 |
|
106 |
logging.error("OCR failed to detect a valid weight")
|
107 |
-
return "Not detected", 0.0
|
108 |
except Exception as e:
|
109 |
logging.error(f"OCR processing failed: {str(e)}")
|
110 |
-
return "Not detected", 0.0
|
111 |
|
112 |
def process_image(img):
|
113 |
"""Process uploaded or captured image and extract weight."""
|
114 |
if img is None:
|
115 |
logging.error("No image provided")
|
116 |
-
return "No image uploaded", None,
|
117 |
|
118 |
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
|
119 |
-
weight, confidence = extract_weight(img)
|
120 |
|
121 |
if weight == "Not detected" or confidence < 95.0:
|
122 |
logging.warning(f"Weight detection failed: {weight} (Confidence: {confidence:.2f}%)")
|
123 |
-
return f"{weight} (Confidence: {confidence:.2f}%)", ist_time,
|
124 |
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
# Gradio Interface
|
128 |
with gr.Blocks(title="โ๏ธ Auto Weight Logger") as demo:
|
@@ -135,7 +141,7 @@ with gr.Blocks(title="โ๏ธ Auto Weight Logger") as demo:
|
|
135 |
|
136 |
with gr.Row():
|
137 |
timestamp = gr.Textbox(label="๐ Captured At (IST)")
|
138 |
-
snapshot = gr.Image(label="๐ธ Snapshot Image")
|
139 |
|
140 |
submit = gr.Button("๐ Detect Weight")
|
141 |
submit.click(
|
|
|
75 |
try:
|
76 |
if img is None:
|
77 |
logging.error("No image provided for OCR")
|
78 |
+
return "Not detected", 0.0, None
|
79 |
|
80 |
# Convert PIL image to OpenCV format
|
81 |
img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
|
|
99 |
if weight_float >= 0: # Only accept valid weights
|
100 |
confidence = 95.0 # Assume high confidence if we have a valid weight
|
101 |
logging.info(f"Weight detected: {weight} (Confidence: {confidence:.2f}%)")
|
102 |
+
return weight, confidence, processed_img
|
103 |
except ValueError:
|
104 |
logging.warning(f"Invalid number format: {weight}")
|
105 |
|
106 |
logging.error("OCR failed to detect a valid weight")
|
107 |
+
return "Not detected", 0.0, None
|
108 |
except Exception as e:
|
109 |
logging.error(f"OCR processing failed: {str(e)}")
|
110 |
+
return "Not detected", 0.0, None
|
111 |
|
112 |
def process_image(img):
|
113 |
"""Process uploaded or captured image and extract weight."""
|
114 |
if img is None:
|
115 |
logging.error("No image provided")
|
116 |
+
return "No image uploaded", None, gr.update(visible=False), gr.update(visible=False)
|
117 |
|
118 |
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
|
119 |
+
weight, confidence, processed_img = extract_weight(img)
|
120 |
|
121 |
if weight == "Not detected" or confidence < 95.0:
|
122 |
logging.warning(f"Weight detection failed: {weight} (Confidence: {confidence:.2f}%)")
|
123 |
+
return f"{weight} (Confidence: {confidence:.2f}%)", ist_time, gr.update(visible=True), gr.update(visible=False)
|
124 |
|
125 |
+
# Convert processed image back to PIL and encode as base64
|
126 |
+
pil_image = Image.fromarray(processed_img)
|
127 |
+
buffered = io.BytesIO()
|
128 |
+
pil_image.save(buffered, format="PNG")
|
129 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
130 |
+
|
131 |
+
return f"{weight} kg (Confidence: {confidence:.2f}%)", ist_time, img_base64, gr.update(visible=True)
|
132 |
|
133 |
# Gradio Interface
|
134 |
with gr.Blocks(title="โ๏ธ Auto Weight Logger") as demo:
|
|
|
141 |
|
142 |
with gr.Row():
|
143 |
timestamp = gr.Textbox(label="๐ Captured At (IST)")
|
144 |
+
snapshot = gr.Image(label="๐ธ Snapshot Image", type="auto")
|
145 |
|
146 |
submit = gr.Button("๐ Detect Weight")
|
147 |
submit.click(
|