Spaces:
Runtime error
Runtime error
Commit ·
76d2092
1
Parent(s): c668c43
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,15 @@
|
|
| 1 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
|
|
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
import requests
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def predict_bounding_boxes(imageurl:str):
|
| 11 |
try:
|
|
@@ -13,15 +17,19 @@ def predict_bounding_boxes(imageurl:str):
|
|
| 13 |
response.raise_for_status()
|
| 14 |
|
| 15 |
image_data = Image.open(response.raw)
|
| 16 |
-
inputs =
|
| 17 |
-
outputs =
|
| 18 |
|
| 19 |
target_sizes = torch.tensor([image_data.size[::-1]])
|
| 20 |
-
results =
|
| 21 |
|
| 22 |
-
detections = [{"score": score.item(), "label":
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
except Exception as e:
|
| 27 |
|
|
@@ -29,4 +37,4 @@ def predict_bounding_boxes(imageurl:str):
|
|
| 29 |
|
| 30 |
app = gr.Interface(fn=predict_bounding_boxes, inputs="text", outputs="json")
|
| 31 |
app.api = True
|
| 32 |
-
app.launch(share=True)
|
|
|
|
| 1 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 2 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
import requests
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
+
box_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
| 9 |
+
box_model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
| 10 |
+
|
| 11 |
+
caption_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 12 |
+
caption_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 13 |
|
| 14 |
def predict_bounding_boxes(imageurl:str):
|
| 15 |
try:
|
|
|
|
| 17 |
response.raise_for_status()
|
| 18 |
|
| 19 |
image_data = Image.open(response.raw)
|
| 20 |
+
inputs = box_processor(images=image_data, return_tensors="pt")
|
| 21 |
+
outputs = box_model(**inputs)
|
| 22 |
|
| 23 |
target_sizes = torch.tensor([image_data.size[::-1]])
|
| 24 |
+
results = box_processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
| 25 |
|
| 26 |
+
detections = [{"score": score.item(), "label": box_model.config.id2label[label.item()], "box": box.tolist()} for score, label, box in zip(results["scores"], results["labels"], results["boxes"])]
|
| 27 |
|
| 28 |
+
raw_image = image_data.convert('RGB')
|
| 29 |
+
inputs = caption_processor(raw_image, return_tensors="pt")
|
| 30 |
+
out = caption_model.generate(**inputs)
|
| 31 |
+
label = caption_processor.decode(out[0], skip_special_tokens=True)
|
| 32 |
+
return {"label": label, "detections": detections}
|
| 33 |
|
| 34 |
except Exception as e:
|
| 35 |
|
|
|
|
| 37 |
|
| 38 |
app = gr.Interface(fn=predict_bounding_boxes, inputs="text", outputs="json")
|
| 39 |
app.api = True
|
| 40 |
+
app.launch(share=True)
|