Spaces:
Runtime error
Runtime error
update confidefnce, remove debugging code
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ import gradio as gr
|
|
8 |
import numpy as np
|
9 |
import requests
|
10 |
import supervision as sv
|
11 |
-
from inference_sdk import InferenceHTTPClient
|
12 |
from openai import OpenAI
|
13 |
|
14 |
CLIENT = InferenceHTTPClient(
|
@@ -16,13 +16,13 @@ CLIENT = InferenceHTTPClient(
|
|
16 |
api_key=os.environ["ROBOFLOW_API_KEY"],
|
17 |
)
|
18 |
|
|
|
|
|
19 |
openai_client = OpenAI()
|
20 |
|
21 |
|
22 |
def process_mask(region, task_id):
|
23 |
-
region = cv2.rotate(region, cv2.
|
24 |
-
|
25 |
-
cv2.imwrite(f"region_{task_id}.jpg", region)
|
26 |
|
27 |
# change channels
|
28 |
region = cv2.cvtColor(region, cv2.COLOR_BGR2RGB)
|
@@ -84,7 +84,8 @@ def process_book_with_google_books(book):
|
|
84 |
# define function that accepts an image
|
85 |
def detect_books(image):
|
86 |
# infer on a local image
|
87 |
-
|
|
|
88 |
results = sv.Detections.from_inference(results)
|
89 |
|
90 |
mask_annotator = sv.MaskAnnotator()
|
@@ -101,7 +102,7 @@ def detect_books(image):
|
|
101 |
|
102 |
print("Calculated masks...")
|
103 |
|
104 |
-
with concurrent.futures.ThreadPoolExecutor(
|
105 |
tasks = [
|
106 |
executor.submit(process_mask, region, task_id)
|
107 |
for task_id, region in enumerate(masks_isolated)
|
@@ -140,9 +141,15 @@ def detect_books(image):
|
|
140 |
for title, author, isbn, polygon_list, xyxy, link in zip(
|
141 |
books, authors, isbns, polygons, results.xyxy, links
|
142 |
)
|
143 |
-
if "sorry" not in title.lower() and "NULL" not in title
|
144 |
]
|
145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
width, height = image.shape[1], image.shape[0]
|
147 |
|
148 |
svg = f"""<div class="image-container"><img src="image.jpeg" height="{height}" width="{width}">
|
@@ -178,7 +185,10 @@ def detect_books(image):
|
|
178 |
"WIDTH", str(width)
|
179 |
)
|
180 |
|
181 |
-
|
|
|
|
|
|
|
182 |
|
183 |
|
184 |
iface = gr.Interface(
|
@@ -193,7 +203,7 @@ iface = gr.Interface(
|
|
193 |
# outputs should be an image and a list of text
|
194 |
outputs=[
|
195 |
gr.components.Textbox(label="Detected Books", info="The detected books."),
|
196 |
-
gr.components.Image(label="Annotated Image"
|
197 |
gr.components.Textbox(label="ISBNs", info="The ISBNs of the detected books."),
|
198 |
gr.components.Textbox(label="SVG", info="Copy-paste this code onto a web page to create a clickable bookshelf. NB: This code doesn't scale to different screen resolutions."),
|
199 |
],
|
|
|
8 |
import numpy as np
|
9 |
import requests
|
10 |
import supervision as sv
|
11 |
+
from inference_sdk import InferenceHTTPClient, InferenceConfiguration
|
12 |
from openai import OpenAI
|
13 |
|
14 |
CLIENT = InferenceHTTPClient(
|
|
|
16 |
api_key=os.environ["ROBOFLOW_API_KEY"],
|
17 |
)
|
18 |
|
19 |
+
custom_configuration = InferenceConfiguration(confidence_threshold=0.3)
|
20 |
+
|
21 |
openai_client = OpenAI()
|
22 |
|
23 |
|
24 |
def process_mask(region, task_id):
|
25 |
+
region = cv2.rotate(region, cv2.ROTATE_90_COUNTERCLOCKWISE)
|
|
|
|
|
26 |
|
27 |
# change channels
|
28 |
region = cv2.cvtColor(region, cv2.COLOR_BGR2RGB)
|
|
|
84 |
# define function that accepts an image
|
85 |
def detect_books(image):
|
86 |
# infer on a local image
|
87 |
+
with CLIENT.use_configuration(custom_configuration):
|
88 |
+
results = CLIENT.infer(image, model_id="open-shelves/6")
|
89 |
results = sv.Detections.from_inference(results)
|
90 |
|
91 |
mask_annotator = sv.MaskAnnotator()
|
|
|
102 |
|
103 |
print("Calculated masks...")
|
104 |
|
105 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
106 |
tasks = [
|
107 |
executor.submit(process_mask, region, task_id)
|
108 |
for task_id, region in enumerate(masks_isolated)
|
|
|
141 |
for title, author, isbn, polygon_list, xyxy, link in zip(
|
142 |
books, authors, isbns, polygons, results.xyxy, links
|
143 |
)
|
144 |
+
if "sorry" not in title.lower() and "NULL" not in title and "cannot" not in title and "can't" not in title
|
145 |
]
|
146 |
|
147 |
+
# order annotations by x0
|
148 |
+
annotations = sorted(annotations, key=lambda x: x["xyxy"][0])
|
149 |
+
|
150 |
+
books = [annotation["title"] for annotation in annotations]
|
151 |
+
isbns = [annotation["isbn"] for annotation in annotations]
|
152 |
+
|
153 |
width, height = image.shape[1], image.shape[0]
|
154 |
|
155 |
svg = f"""<div class="image-container"><img src="image.jpeg" height="{height}" width="{width}">
|
|
|
185 |
"WIDTH", str(width)
|
186 |
)
|
187 |
|
188 |
+
books = ", ".join(books)
|
189 |
+
isbns = ", ".join(isbns)
|
190 |
+
|
191 |
+
return books, annotated_image, isbns, svg
|
192 |
|
193 |
|
194 |
iface = gr.Interface(
|
|
|
203 |
# outputs should be an image and a list of text
|
204 |
outputs=[
|
205 |
gr.components.Textbox(label="Detected Books", info="The detected books."),
|
206 |
+
gr.components.Image(label="Annotated Image"),
|
207 |
gr.components.Textbox(label="ISBNs", info="The ISBNs of the detected books."),
|
208 |
gr.components.Textbox(label="SVG", info="Copy-paste this code onto a web page to create a clickable bookshelf. NB: This code doesn't scale to different screen resolutions."),
|
209 |
],
|