Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -34,7 +34,7 @@ def resize_image(img, max_size_mb=5):
|
|
34 |
size_mb = len(img_bytes.getvalue()) / (1024 * 1024)
|
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
|
@@ -52,14 +52,14 @@ def resize_image(img, max_size_mb=5):
|
|
52 |
def process_image(img):
|
53 |
if img is None:
|
54 |
return "No image uploaded", None, None, None, gr.update(visible=False), gr.update(visible=False)
|
55 |
-
|
56 |
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
|
57 |
img, img_bytes = resize_image(img)
|
58 |
weight, confidence = extract_weight_from_image(img)
|
59 |
-
|
60 |
if weight == "Not detected" or confidence < 70:
|
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)
|
@@ -69,10 +69,10 @@ def save_to_salesforce(weight_text, img_base64):
|
|
69 |
sf = connect_to_salesforce()
|
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,
|
@@ -122,4 +122,4 @@ with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
|
|
122 |
- Works on desktop and mobile browsers.
|
123 |
""")
|
124 |
|
125 |
-
demo.launch()
|
|
|
34 |
size_mb = len(img_bytes.getvalue()) / (1024 * 1024)
|
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
|
|
|
52 |
def process_image(img):
|
53 |
if img is None:
|
54 |
return "No image uploaded", None, None, None, gr.update(visible=False), gr.update(visible=False)
|
55 |
+
|
56 |
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
|
57 |
img, img_bytes = resize_image(img)
|
58 |
weight, confidence = extract_weight_from_image(img)
|
59 |
+
|
60 |
if weight == "Not detected" or confidence < 70:
|
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)
|
|
|
69 |
sf = connect_to_salesforce()
|
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,
|
|
|
122 |
- Works on desktop and mobile browsers.
|
123 |
""")
|
124 |
|
125 |
+
demo.launch()
|