Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,6 @@ def resize_image(img, max_size_mb=5):
|
|
| 35 |
if size_mb <= max_size_mb:
|
| 36 |
return img, img_bytes.getvalue()
|
| 37 |
|
| 38 |
-
# Resize proportionally
|
| 39 |
scale = 0.9
|
| 40 |
while size_mb > max_size_mb:
|
| 41 |
w, h = img.size
|
|
@@ -58,13 +57,12 @@ def process_image(img):
|
|
| 58 |
img, img_bytes = resize_image(img)
|
| 59 |
weight, confidence = extract_weight_from_image(img)
|
| 60 |
|
| 61 |
-
if weight == "Not detected" or confidence <
|
| 62 |
-
return f"{weight} (Confidence: {confidence}%)", ist_time, img, None, gr.update(visible=True), gr.update(visible=False)
|
| 63 |
|
| 64 |
-
# Encode image for preview
|
| 65 |
img_buffer = io.BytesIO(img_bytes)
|
| 66 |
img_base64 = base64.b64encode(img_buffer.getvalue()).decode()
|
| 67 |
-
return f"{weight} kg (Confidence: {confidence}%)", ist_time, img, img_base64, gr.update(visible=True), gr.update(visible=True)
|
| 68 |
|
| 69 |
def save_to_salesforce(weight_text, img_base64):
|
| 70 |
try:
|
|
@@ -72,11 +70,9 @@ def save_to_salesforce(weight_text, img_base64):
|
|
| 72 |
if sf is None:
|
| 73 |
return "Failed to connect to Salesforce"
|
| 74 |
|
| 75 |
-
# Extract weight from text (remove "kg" and confidence)
|
| 76 |
weight = float(weight_text.split(" ")[0])
|
| 77 |
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S")
|
| 78 |
|
| 79 |
-
# Create custom object record (adjust object and fields as needed)
|
| 80 |
record = {
|
| 81 |
"Name": f"Weight_Log_{ist_time}",
|
| 82 |
"Weight__c": weight,
|
|
@@ -123,7 +119,7 @@ with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
|
|
| 123 |
- Upload a clear, well-lit image of a digital weight scale display.
|
| 124 |
- Ensure the image is < 5MB (automatically resized if larger).
|
| 125 |
- Review the detected weight and click 'Confirm and Save to Salesforce' to log the data.
|
| 126 |
-
-
|
| 127 |
""")
|
| 128 |
|
| 129 |
demo.launch()
|
|
|
|
| 35 |
if size_mb <= max_size_mb:
|
| 36 |
return img, img_bytes.getvalue()
|
| 37 |
|
|
|
|
| 38 |
scale = 0.9
|
| 39 |
while size_mb > max_size_mb:
|
| 40 |
w, h = img.size
|
|
|
|
| 57 |
img, img_bytes = resize_image(img)
|
| 58 |
weight, confidence = extract_weight_from_image(img)
|
| 59 |
|
| 60 |
+
if weight == "Not detected" or confidence < 80:
|
| 61 |
+
return f"{weight} (Confidence: {confidence:.2f}%)", ist_time, img, None, gr.update(visible=True), gr.update(visible=False)
|
| 62 |
|
|
|
|
| 63 |
img_buffer = io.BytesIO(img_bytes)
|
| 64 |
img_base64 = base64.b64encode(img_buffer.getvalue()).decode()
|
| 65 |
+
return f"{weight} kg (Confidence: {confidence:.2f}%)", ist_time, img, img_base64, gr.update(visible=True), gr.update(visible=True)
|
| 66 |
|
| 67 |
def save_to_salesforce(weight_text, img_base64):
|
| 68 |
try:
|
|
|
|
| 70 |
if sf is None:
|
| 71 |
return "Failed to connect to Salesforce"
|
| 72 |
|
|
|
|
| 73 |
weight = float(weight_text.split(" ")[0])
|
| 74 |
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S")
|
| 75 |
|
|
|
|
| 76 |
record = {
|
| 77 |
"Name": f"Weight_Log_{ist_time}",
|
| 78 |
"Weight__c": weight,
|
|
|
|
| 119 |
- Upload a clear, well-lit image of a digital weight scale display.
|
| 120 |
- Ensure the image is < 5MB (automatically resized if larger).
|
| 121 |
- Review the detected weight and click 'Confirm and Save to Salesforce' to log the data.
|
| 122 |
+
- Works on desktop and mobile browsers.
|
| 123 |
""")
|
| 124 |
|
| 125 |
demo.launch()
|