Commit
·
f5b4b81
1
Parent(s):
0b5b4b1
app v1
Browse files- README.md +14 -8
- app.py +60 -0
- bear.jpg +0 -0
- boat.jpg +0 -0
- install-deps.sh +3 -0
- puppy.jpg +0 -0
- requirements.txt +17 -0
- result.png +0 -0
- run.sh +3 -0
README.md
CHANGED
@@ -1,13 +1,19 @@
|
|
1 |
---
|
2 |
-
title: Diego
|
3 |
-
emoji:
|
4 |
-
colorFrom: green
|
5 |
-
colorTo: pink
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
|
|
|
|
|
|
|
|
8 |
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
license: unlicense
|
11 |
---
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Diego's LLM Image to Labeled Image
|
3 |
+
emoji: 🖼️
|
|
|
|
|
4 |
sdk: gradio
|
5 |
+
sdk_version: 4.24.0
|
6 |
+
license: cc-by-nc-sa-4.0
|
7 |
+
short_description: Diego's LLM Image to Labeled Image. Classify an image and draw the label on the image.
|
8 |
+
colorFrom: green
|
9 |
+
colorTo: black
|
10 |
app_file: app.py
|
|
|
|
|
11 |
---
|
12 |
|
13 |
+
### Result
|
14 |
+
* Given a image
|
15 |
+
* Will detect what the image is
|
16 |
+
* Print the label and the score on the image
|
17 |
+
* Uses the LLM model vit-base-patch16-224
|
18 |
+
|
19 |
+
<img src='result.png' />
|
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
import requests
|
3 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import matplotlib.patches as patches
|
6 |
+
import gradio as gr
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
def classify_and_label_image(image_array):
|
10 |
+
# Convert numpy array to PIL Image
|
11 |
+
image = Image.fromarray(np.uint8(image_array))
|
12 |
+
|
13 |
+
# Load pre-trained model and feature extractor
|
14 |
+
model = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224')
|
15 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained('google/vit-base-patch16-224')
|
16 |
+
|
17 |
+
# Preprocess the image
|
18 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
19 |
+
|
20 |
+
# Make prediction
|
21 |
+
outputs = model(**inputs)
|
22 |
+
logits = outputs.logits
|
23 |
+
predicted_class_idx = logits.argmax(-1).item()
|
24 |
+
|
25 |
+
# Get label of the predicted class
|
26 |
+
labels = model.config.id2label
|
27 |
+
predicted_class_label = labels[predicted_class_idx]
|
28 |
+
|
29 |
+
# Calculate prediction probability
|
30 |
+
probabilities = logits.softmax(dim=-1)
|
31 |
+
predicted_class_prob = probabilities[0, predicted_class_idx].item()
|
32 |
+
|
33 |
+
# Draw bounding box and label on the image
|
34 |
+
fig, ax = plt.subplots()
|
35 |
+
ax.imshow(image)
|
36 |
+
rect = patches.Rectangle((50, 50), 100, 100, linewidth=1, edgecolor='r', facecolor='none')
|
37 |
+
ax.add_patch(rect)
|
38 |
+
plt.text(50, 40, f'{predicted_class_label} {predicted_class_prob * 100:.2f}%', color='r')
|
39 |
+
|
40 |
+
# Convert the figure to a numpy array
|
41 |
+
fig.canvas.draw()
|
42 |
+
img_arr = np.array(fig.canvas.renderer.buffer_rgba())
|
43 |
+
|
44 |
+
# Close the figure to free up memory
|
45 |
+
plt.close(fig)
|
46 |
+
|
47 |
+
# Return the image array
|
48 |
+
return img_arr
|
49 |
+
|
50 |
+
examples = [
|
51 |
+
["bear.jpg"],
|
52 |
+
["puppy.jpg"],
|
53 |
+
["boat.jpg"]
|
54 |
+
]
|
55 |
+
gr.Interface(fn=classify_and_label_image, title="Diego's LLM Image to Labeled Image",
|
56 |
+
description="Classify an image and draw the label on the image.",
|
57 |
+
examples=examples,
|
58 |
+
inputs="image",
|
59 |
+
outputs="image")\
|
60 |
+
.launch(share=False, server_port=8080)
|
bear.jpg
ADDED
![]() |
boat.jpg
ADDED
![]() |
install-deps.sh
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
/bin/pip install -r requirements.txt
|
puppy.jpg
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
transformers
|
3 |
+
sentence-transformers
|
4 |
+
seaborn
|
5 |
+
torch
|
6 |
+
torchvision
|
7 |
+
matplotlib
|
8 |
+
pandas
|
9 |
+
scikit-learn
|
10 |
+
nltk
|
11 |
+
gensim
|
12 |
+
tensorflow
|
13 |
+
keras
|
14 |
+
opencv-python
|
15 |
+
fastapi
|
16 |
+
uvicorn
|
17 |
+
gradio
|
result.png
ADDED
![]() |
run.sh
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
/bin/python src/main.py
|