Spaces:
Runtime error
Runtime error
error fix
Browse files
app.py
CHANGED
@@ -23,24 +23,35 @@ def respond(input_image):
|
|
23 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
24 |
cv2.imwrite(temp_file.name, input_image)
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
33 |
)
|
34 |
-
)
|
35 |
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
classes
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
return result_image
|
46 |
|
|
|
23 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
24 |
cv2.imwrite(temp_file.name, input_image)
|
25 |
|
26 |
+
try:
|
27 |
+
DINOGPT = CustomDetectionModel(
|
28 |
+
detection_model=GroundingDINO(
|
29 |
+
CaptionOntology({dino_prompt: dino_prompt})
|
30 |
+
),
|
31 |
+
classification_model=GPT4V(
|
32 |
+
CaptionOntology({k: k for k in gpt_prompt.split(", ")}),
|
33 |
+
api_key=api_key # Use the hardcoded API key
|
34 |
+
)
|
35 |
)
|
|
|
36 |
|
37 |
+
results = DINOGPT.predict(temp_file.name)
|
38 |
|
39 |
+
# Assuming the results are a list of dictionaries with 'class' keys
|
40 |
+
# Extracting class names from results
|
41 |
+
predicted_classes = [result['class'] for result in results]
|
42 |
+
# Filtering the ontology prompts to include only those that match the predicted classes
|
43 |
+
filtered_ontology = {k: v for k, v in DINOGPT.classification_model.ontology.prompts().items() if k in predicted_classes}
|
44 |
+
|
45 |
+
result_image = plot(
|
46 |
+
image=cv2.imread(temp_file.name),
|
47 |
+
detections=results,
|
48 |
+
classes=list(filtered_ontology.keys()), # Use the filtered ontology
|
49 |
+
raw=True
|
50 |
+
)
|
51 |
+
except ValueError as e:
|
52 |
+
print("An error occurred:", e)
|
53 |
+
# Handle the error appropriately, perhaps by returning an error message or default image
|
54 |
+
result_image = None # Replace with your error handling logic
|
55 |
|
56 |
return result_image
|
57 |
|