Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN")
|
|
| 13 |
def RoboFlowGetOutlineBoxesPIL(pil_img):
|
| 14 |
client = InferenceHTTPClient(
|
| 15 |
api_url="https://detect.roboflow.com",
|
| 16 |
-
api_key=
|
| 17 |
)
|
| 18 |
result = client.run_workflow(
|
| 19 |
workspace_name="mathnet-mmpuo",
|
|
@@ -242,19 +242,19 @@ def drawWithAllBox_info(pil_image, box_info):
|
|
| 242 |
draw.rectangle([x-w/2, y-h/2, x+w/2, y+h/2], outline=color, width=2)
|
| 243 |
return pil_image
|
| 244 |
|
| 245 |
-
API_KEY = os.environ.get("ROBOFLOW_API_KEY")[:6]
|
| 246 |
-
test_box_info = [{'width': 15.0, 'height': 52.0, 'x': 177.5, 'y': 445.0, 'confidence': 0.7930343747138977, 'class_id': 3, 'class': 'fraction', 'detection_id': 'd181fc53-1013-4f34-9099-d37970fcf99d', 'parent_id': 'image'}, {'width': 27.0, 'height': 58.0, 'x': 419.5, 'y': 351.0, 'confidence': 0.7838523387908936, 'class_id': 3, 'class': 'fraction', 'detection_id': '6ac399cf-d08a-48b6-88d3-5afe07555fbc', 'parent_id': 'image'}, {'width': 16.0, 'height': 36.0, 'x': 62.0, 'y': 18.0, 'confidence': 0.7768579721450806, 'class_id': 3, 'class': 'fraction', 'detection_id': 'c4de7dd9-4fa6-4eee-9989-3c798a3fdb57', 'parent_id': 'image'}, {'width': 17.0, 'height': 50.0, 'x': 225.5, 'y': 416.0, 'confidence': 0.7762054204940796, 'class_id': 3, 'class': 'fraction', 'detection_id': '5d3c48f3-3954-4ede-a6c2-6e4ab2261c4b', 'parent_id': 'image'}, {'width': 20.0, 'height': 71.0, 'x': 142.0, 'y': 345.5, 'confidence': 0.7731378674507141, 'class_id': 3, 'class': 'fraction', 'detection_id': '51c58a14-891f-4b47-96f5-86a80fbb43d0', 'parent_id': 'image'}, {'width': 15.0, 'height': 51.0, 'x': 275.5, 'y': 380.5, 'confidence': 0.7697888016700745, 'class_id': 3, 'class': 'fraction', 'detection_id': '36d44410-241a-4f68-aac3-d91feca51f19', 'parent_id': 'image'}, {'width': 26.0, 'height': 51.0, 'x': 540.0, 'y': 343.5, 'confidence': 0.7660567760467529, 'class_id': 3, 'class': 'fraction', 'detection_id': '67a95be8-fae4-402c-8674-61fcfa417d65', 'parent_id': 'image'}, {'width': 16.0, 'height': 46.0, 'x': 515.0, 'y': 335.0, 'confidence': 0.7611395716667175, 'class_id': 3, 'class': 'fraction', 'detection_id': 'fdf6babe-dd21-4572-b24f-935ad8f1c2f1', 'parent_id': 'image'}, {'width': 26.0, 'height': 73.0, 'x': 375.0, 'y': 370.5, 'confidence': 0.7563880681991577, 'class_id': 3, 'class': 'fraction', 'detection_id': 'cdec0709-c078-46a4-bcc8-81099825bdb0', 'parent_id': 'image'}, {'width': 26.0, 'height': 42.0, 'x': 180.0, 'y': 21.0, 'confidence': 0.7336967587471008, 'class_id': 3, 'class': 'fraction', 'detection_id': '70204282-66cf-413e-b182-9843ba9bd2a0', 'parent_id': 'image'}, {'width': 27.0, 'height': 43.0, 'x': 458.5, 'y': 338.5, 'confidence': 0.7322718501091003, 'class_id': 3, 'class': 'fraction', 'detection_id': '4fc2b634-302d-4566-b066-a87826f6bf50', 'parent_id': 'image'}, {'width': 29.0, 'height': 50.0, 'x': 423.5, 'y': 25.0, 'confidence': 0.7266790866851807, 'class_id': 3, 'class': 'fraction', 'detection_id': '08e8f5fc-8607-4d33-804d-718c5de06ff8', 'parent_id': 'image'}, {'width': 18.0, 'height': 38.0, 'x': 25.0, 'y': 20.0, 'confidence': 0.7199405431747437, 'class_id': 3, 'class': 'fraction', 'detection_id': 'd26c35ea-3ca9-48fc-93c0-0ca969316a70', 'parent_id': 'image'}, {'width': 7.0, 'height': 33.0, 'x': 451.5, 'y': 300.5, 'confidence': 0.7156490087509155, 'class_id': 9, 'class': 'tick', 'detection_id': '5a960e53-923c-4c59-a385-c2cc66909c2a', 'parent_id': 'image'}, {'width': 7.0, 'height': 59.0, 'x': 533.5, 'y': 289.5, 'confidence': 0.6673337817192078, 'class_id': 9, 'class': 'tick', 'detection_id': 'fcc07b36-c269-4289-92a8-899cdcfc1edd', 'parent_id': 'image'}, {'width': 15.0, 'height': 44.0, 'x': 304.5, 'y': 306.0, 'confidence': 0.6520688533782959, 'class_id': 3, 'class': 'fraction', 'detection_id': '2931f552-e8a7-44ba-8c3e-ef47305f948e', 'parent_id': 'image'}, {'width': 26.0, 'height': 50.0, 'x': 486.0, 'y': 336.0, 'confidence': 0.6388527154922485, 'class_id': 3, 'class': 'fraction', 'detection_id': '88f47b50-aced-4dac-8f30-b32d4f71ae28', 'parent_id': 'image'}, {'width': 6.0, 'height': 41.0, 'x': 505.0, 'y': 291.5, 'confidence': 0.6247831583023071, 'class_id': 9, 'class': 'tick', 'detection_id': 'd9c2b390-6c3d-4799-aad1-9a0deb9b55c9', 'parent_id': 'image'}, {'width': 7.0, 'height': 60.0, 'x': 375.5, 'y': 303.0, 'confidence': 0.5316932201385498, 'class_id': 9, 'class': 'tick', 'detection_id': 'e5ef672f-79c1-4460-a8d0-f7296929eb37', 'parent_id': 'image'}, {'width': 21.0, 'height': 26.0, 'x': 102.5, 'y': 13.0, 'confidence': 0.4673563241958618, 'class_id': 3, 'class': 'fraction', 'detection_id': 'a788581d-1090-4bfe-b309-088fbde0f3e9', 'parent_id': 'image'}, {'width': 9.0, 'height': 36.0, 'x': 480.5, 'y': 297.0, 'confidence': 0.4122121334075928, 'class_id': 9, 'class': 'tick', 'detection_id': '2ed9427c-3715-421d-bdbf-33b560869781', 'parent_id': 'image'}, {'width': 15.0, 'height': 34.0, 'x': 372.5, 'y': 352.0, 'confidence': 0.8663275241851807, 'class_id': 11, 'class': 'zero', 'detection_id': '52783f2f-d709-44a4-b5d8-accad996427a', 'parent_id': 'image'}, {'width': 16.0, 'height': 35.0, 'x': 381.0, 'y': 386.5, 'confidence': 0.7994610071182251, 'class_id': 1, 'class': 'five', 'detection_id': '1d8b762f-44d2-496d-9408-08504b42363b', 'parent_id': 'image'}, {'width': 16.0, 'height': 30.0, 'x': 142.0, 'y': 363.0, 'confidence': 0.7864396572113037, 'class_id': 1, 'class': 'five', 'detection_id': '76bea9a4-e970-407b-895b-3ea3740e4c62', 'parent_id': 'image'}, {'width': 21.0, 'height': 68.0, 'x': 237.5, 'y': 289.0, 'confidence': 0.7825816869735718, 'class_id': 5, 'class': 'one', 'detection_id': 'e2a02e70-3238-4abd-b0c2-d1b7e34a3ad6', 'parent_id': 'image'}, {'width': 14.0, 'height': 33.0, 'x': 547.0, 'y': 352.5, 'confidence': 0.7726041674613953, 'class_id': 1, 'class': 'five', 'detection_id': '6421741e-1012-4176-85fb-5e079496503c', 'parent_id': 'image'}, {'width': 20.0, 'height': 62.0, 'x': 455.0, 'y': 523.0, 'confidence': 0.7658700942993164, 'class_id': 1, 'class': 'five', 'detection_id': 'f5de812f-367b-49de-8741-6d83180dbb07', 'parent_id': 'image'}, {'width': 16.0, 'height': 36.0, 'x': 427.0, 'y': 358.0, 'confidence': 0.7415089011192322, 'class_id': 1, 'class': 'five', 'detection_id': 'fcc60aa2-3fbd-49d0-b6b5-c2325868b6e2', 'parent_id': 'image'}, {'width': 12.0, 'height': 22.0, 'x': 226.0, 'y': 402.0, 'confidence': 0.6791027784347534, 'class_id': 5, 'class': 'one', 'detection_id': '0e62a2d5-a205-4a57-bfea-f0c806f510cd', 'parent_id': 'image'}, {'width': 12.0, 'height': 24.0, 'x': 516.0, 'y': 345.0, 'confidence': 0.6720584034919739, 'class_id': 10, 'class': 'two', 'detection_id': '61cc1063-848d-4b3f-bfc2-7ce5ea3d85f2', 'parent_id': 'image'}, {'width': 21.0, 'height': 38.0, 'x': 424.5, 'y': 31.0, 'confidence': 0.668595552444458, 'class_id': 7, 'class': 'six', 'detection_id': '98675c7c-9122-4292-b2dc-bc124505fce4', 'parent_id': 'image'}, {'width': 11.0, 'height': 28.0, 'x': 412.5, 'y': 336.0, 'confidence': 0.6583085060119629, 'class_id': 5, 'class': 'one', 'detection_id': 'df4fec26-4620-4c27-8907-c3dece30a4b3', 'parent_id': 'image'}, {'width': 16.0, 'height': 33.0, 'x': 185.0, 'y': 22.5, 'confidence': 0.6334441900253296, 'class_id': 7, 'class': 'six', 'detection_id': '6b185865-c922-497b-b7a8-09eea68ca213', 'parent_id': 'image'}, {'width': 16.0, 'height': 30.0, 'x': 25.0, 'y': 23.0, 'confidence': 0.6150357723236084, 'class_id': 7, 'class': 'six', 'detection_id': '7484f5e8-4e5e-45b4-a3a3-b36b29166ee9', 'parent_id': 'image'}, {'width': 12.0, 'height': 24.0, 'x': 276.0, 'y': 368.0, 'confidence': 0.6141548156738281, 'class_id': 5, 'class': 'one', 'detection_id': 'aea44c20-dc98-4c4c-ac17-0e07a8c842dc', 'parent_id': 'image'}, {'width': 8.0, 'height': 21.0, 'x': 454.0, 'y': 349.5, 'confidence': 0.6118046045303345, 'class_id': 5, 'class': 'one', 'detection_id': '38207a67-c933-44b1-9d5b-009d56bab5af', 'parent_id': 'image'}, {'width': 12.0, 'height': 22.0, 'x': 452.0, 'y': 329.0, 'confidence': 0.5846502780914307, 'class_id': 10, 'class': 'two', 'detection_id': '9c65a5cf-8a28-449e-bf9c-fcb4ee372d68', 'parent_id': 'image'}, {'width': 14.0, 'height': 64.0, 'x': 420.0, 'y': 530.0, 'confidence': 0.5587379336357117, 'class_id': 5, 'class': 'one', 'detection_id': 'd8b21523-bf78-438e-8121-b824a36282fc', 'parent_id': 'image'}, {'width': 11.0, 'height': 22.0, 'x': 176.5, 'y': 429.0, 'confidence': 0.5346711874008179, 'class_id': 5, 'class': 'one', 'detection_id': 'c1b5a70a-b34c-4672-bd25-9da2551c1669', 'parent_id': 'image'}, {'width': 12.0, 'height': 23.0, 'x': 480.0, 'y': 323.5, 'confidence': 0.5155420303344727, 'class_id': 8, 'class': 'three', 'detection_id': '25bd3172-15d6-4094-a575-f7a4e29d09a2', 'parent_id': 'image'}, {'width': 13.0, 'height': 26.0, 'x': 491.5, 'y': 345.0, 'confidence': 0.5098029971122742, 'class_id': 10, 'class': 'two', 'detection_id': 'a02a8634-43fd-4bfa-b2f5-f8e0fd962805', 'parent_id': 'image'}, {'width': 11.0, 'height': 12.0, 'x': 62.5, 'y': 6.0, 'confidence': 0.5087376832962036, 'class_id': 5, 'class': 'one', 'detection_id': '335bb2f8-b189-4db1-9e34-9218ae1df15d', 'parent_id': 'image'}, {'width': 15.0, 'height': 24.0, 'x': 224.5, 'y': 428.0, 'confidence': 0.5028042793273926, 'class_id': 1, 'class': 'five', 'detection_id': '56f2b9a6-c5ac-49c6-8b9e-84809c92f664', 'parent_id': 'image'}, {'width': 12.0, 'height': 24.0, 'x': 465.0, 'y': 347.0, 'confidence': 0.49521008133888245, 'class_id': 10, 'class': 'two', 'detection_id': 'e70da7be-de44-45d1-b526-d2381ef583f6', 'parent_id': 'image'}, {'width': 7.0, 'height': 21.0, 'x': 481.5, 'y': 347.5, 'confidence': 0.4925478398799896, 'class_id': 5, 'class': 'one', 'detection_id': '1f37da04-c773-4bac-bb41-41479f456866', 'parent_id': 'image'}, {'width': 15.0, 'height': 28.0, 'x': 141.5, 'y': 327.0, 'confidence': 0.49186599254608154, 'class_id': 5, 'class': 'one', 'detection_id': '334dff8b-e571-45ab-bda7-45b45767e779', 'parent_id': 'image'}, {'width': 6.0, 'height': 20.0, 'x': 507.0, 'y': 346.0, 'confidence': 0.45653682947158813, 'class_id': 5, 'class': 'one', 'detection_id': '58add23f-f410-4a80-b25c-d08741b367cd', 'parent_id': 'image'}, {'width': 11.0, 'height': 19.0, 'x': 62.5, 'y': 25.5, 'confidence': 0.4403974115848541, 'class_id': 1, 'class': 'five', 'detection_id': 'fc64f8a1-6e5b-41f4-915f-37097bbdc159', 'parent_id': 'image'}, {'width': 10.0, 'height': 16.0, 'x': 305.0, 'y': 292.0, 'confidence': 0.4379864037036896, 'class_id': 5, 'class': 'one', 'detection_id': '72dcb2cc-37a4-46eb-9966-5fb5e6a50a05', 'parent_id': 'image'}, {'width': 12.0, 'height': 24.0, 'x': 178.0, 'y': 456.0, 'confidence': 0.4371793270111084, 'class_id': 1, 'class': 'five', 'detection_id': 'e85e2bcd-b18f-4524-8b2e-50e5f662833e', 'parent_id': 'image'}, {'width': 12.0, 'height': 19.0, 'x': 534.0, 'y': 327.5, 'confidence': 0.40896445512771606, 'class_id': 0, 'class': 'eight', 'detection_id': '5cc34662-2775-4a8a-8b7b-4e9eee2ed305', 'parent_id': 'image'}, {'width': 10.0, 'height': 19.0, 'x': 275.0, 'y': 392.5, 'confidence': 0.40710344910621643, 'class_id': 1, 'class': 'five', 'detection_id': 'e476831d-f545-4830-96c1-0fb2b29031ce', 'parent_id': 'image'}]
|
| 247 |
-
|
| 248 |
-
def greet(name):
|
| 249 |
-
return "Helloooo " + name + "!!" + sys.version + "_" + API_KEY
|
| 250 |
|
| 251 |
def process_image(image):
|
| 252 |
if image is None:
|
| 253 |
return None, "", "", None
|
| 254 |
pil_image = image.copy() if hasattr(image, 'copy') else Image.fromarray(image)
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
return boxed_img, textual, json_str
|
| 259 |
|
| 260 |
|
|
|
|
| 13 |
def RoboFlowGetOutlineBoxesPIL(pil_img):
|
| 14 |
client = InferenceHTTPClient(
|
| 15 |
api_url="https://detect.roboflow.com",
|
| 16 |
+
api_key= API_KEY
|
| 17 |
)
|
| 18 |
result = client.run_workflow(
|
| 19 |
workspace_name="mathnet-mmpuo",
|
|
|
|
| 242 |
draw.rectangle([x-w/2, y-h/2, x+w/2, y+h/2], outline=color, width=2)
|
| 243 |
return pil_image
|
| 244 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def process_image(image):
|
| 247 |
if image is None:
|
| 248 |
return None, "", "", None
|
| 249 |
pil_image = image.copy() if hasattr(image, 'copy') else Image.fromarray(image)
|
| 250 |
+
roboflow_ret = RoboFlowGetOutlineBoxesPIL(pil_image)
|
| 251 |
+
all_box_info = parse_roboflow_result(roboflow_ret, classes)
|
| 252 |
+
del roboflow_ret
|
| 253 |
+
kept_box_info = filter_overlapping_boxes(all_box_info)
|
| 254 |
+
del all_box_info
|
| 255 |
+
boxed_img = drawWithAllBox_info(pil_image, kept_box_info)
|
| 256 |
+
textual = generate_textual_description(kept_box_info)
|
| 257 |
+
json_str = json.dumps(kept_box_info, indent=2)
|
| 258 |
return boxed_img, textual, json_str
|
| 259 |
|
| 260 |
|