File size: 1,771 Bytes
bf810c1
 
3fd1dbe
69d5da6
 
 
10099e3
 
c7f7c0f
69d5da6
82fec7c
69d5da6
 
 
 
 
 
 
 
 
d86f0cd
 
522da43
69d5da6
 
 
82fec7c
69d5da6
 
 
f322a04
 
b03bf1e
69d5da6
 
bf810c1
 
 
 
 
 
 
 
 
 
d93bc11
b453b37
 
bf810c1
 
 
3afec0e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

from transformers import pipeline
import streamlit as st
import torch
from PIL import Image
import requests
# Load model directly
from transformers import AutoImageProcessor, AutoModelForObjectDetection
from transformers import AutoImageProcessor, DetrForObjectDetection
    
def model():  
    
    url = "http://images.cocodataset.org/val2017/000000039769.jpg"
    image = Image.open(requests.get(url, stream=True).raw)
    
    image_processor = AutoImageProcessor.from_pretrained("facebook/detr-resnet-50")
    model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
    
    inputs = image_processor(images=image, return_tensors="pt")
    outputs = model(**inputs)

    # display image
    st.image(image, caption='Animal Images')
    
    # convert outputs (bounding boxes and class logits) to Pascal VOC format (xmin, ymin, xmax, ymax)
    target_sizes = torch.tensor([image.size[::-1]])
    results = image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0]
    
    for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
        box = [round(i, 2) for i in box.tolist()]
        st.write(f"Detected {model.config.id2label[label.item()]} with confidence ")
        st.write(f"{round(score.item(), 3)} at location {box}")
            
     
   

def main():
    st.title("Hugging Face Model Demo")

    # Create an input text box
    input_text = st.text_input("Enter your text", "")

    # Create a button to trigger model inference
    if st.button("Analyze"):
        # Perform inference using the loaded model
        result=model()
        #st.write("Prediction:", result[0]['label'], "| Score:", result[0]['score'])
        

if __name__ == "__main__":
    main()