Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- README.md +1 -1
- myapp.py +54 -0
- requirements.txt +5 -0
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: MyGradioApp
|
3 |
-
app_file:
|
4 |
sdk: gradio
|
5 |
sdk_version: 4.19.1
|
6 |
---
|
|
|
1 |
---
|
2 |
title: MyGradioApp
|
3 |
+
app_file: myapp.py
|
4 |
sdk: gradio
|
5 |
sdk_version: 4.19.1
|
6 |
---
|
myapp.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
2 |
+
from PIL import Image, ImageDraw
|
3 |
+
|
4 |
+
import requests
|
5 |
+
import torch
|
6 |
+
import numpy as np
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# using the pre-trained model for image processing
|
10 |
+
image_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
11 |
+
|
12 |
+
# using the pre-trained model for object detection
|
13 |
+
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
14 |
+
|
15 |
+
def detect_objects(image):
|
16 |
+
# convert image from NumPy array to PIL format
|
17 |
+
image = Image.fromarray(image)
|
18 |
+
|
19 |
+
# process the image
|
20 |
+
inputs = image_processor(images = image,
|
21 |
+
return_tensors = "pt")
|
22 |
+
outputs = model(**inputs)
|
23 |
+
|
24 |
+
# create the target size in the format of (height,width)
|
25 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
26 |
+
|
27 |
+
# detect objects in image
|
28 |
+
results = image_processor.post_process_object_detection(
|
29 |
+
outputs,
|
30 |
+
target_sizes = target_sizes,
|
31 |
+
threshold = 0.9)[0]
|
32 |
+
draw = ImageDraw.Draw(image)
|
33 |
+
|
34 |
+
for score, label, box in zip(results["scores"], results["labels"],
|
35 |
+
results["boxes"]):
|
36 |
+
box = [round(i, 2) for i in box.tolist()]
|
37 |
+
# draw bounding box around object
|
38 |
+
draw.rectangle(box,
|
39 |
+
outline="yellow",
|
40 |
+
width=2)
|
41 |
+
# display the object label
|
42 |
+
draw.text((box[0], box[1]-10),
|
43 |
+
model.config.id2label[label.item()],
|
44 |
+
fill="white")
|
45 |
+
return image
|
46 |
+
|
47 |
+
demo = gr.Interface(detect_objects,
|
48 |
+
inputs = gr.Image(width = 300, height = 300),
|
49 |
+
# indicate the size of image to be passed in
|
50 |
+
outputs = gr.Image(width = 300, height= 300),
|
51 |
+
# indicate the size of image to be returned
|
52 |
+
)
|
53 |
+
demo.launch()
|
54 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers==4.38.1
|
2 |
+
torch==2.2.1
|
3 |
+
timm==0.9.16
|
4 |
+
gradio==4.19.2
|
5 |
+
|