GRATITUD3 commited on
Commit
92dbca0
·
1 Parent(s): 864c682
Files changed (1) hide show
  1. app.py +26 -15
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
- DINOGPT = CustomDetectionModel(
27
- detection_model=GroundingDINO(
28
- CaptionOntology({dino_prompt: dino_prompt})
29
- ),
30
- classification_model=GPT4V(
31
- CaptionOntology({k: k for k in gpt_prompt.split(", ")}),
32
- api_key=api_key # Use the hardcoded API key
 
 
33
  )
34
- )
35
 
36
- results = DINOGPT.predict(temp_file.name)
37
 
38
- result_image = plot(
39
- image=cv2.imread(temp_file.name),
40
- detections=results,
41
- classes=gpt_prompt.split(", "),
42
- raw=True
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